我有以下表格
public class myForm
{
private String code; (getter and setter)
private Map<String, Map<String, Object>> map(getter and setter)
}
我可以轻松填充代码属性,但我不知道如何继续填充 map ,我什至不知道是否可能......
这是我的 Spring 表单
<form:form commandName="myForm" action="${PostUrl}" method="POST" >
<input type="hidden" path="code" value="78967" />
<input type="submit" value="Submit"/>
</form:form>
我将知道第一个映射的键,我将知道第二个映射的键,用户只会输入第二个映射的值。
为了尽可能清楚地说明这是在java中我希望对我的表单做什么
Map<String, Map<String, Object>> map1 = new HashMap<String, map<String,
Object>>();
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("DatePickerLabel", DatePickedByTheUser)
map1.put("DATEPICKER", map2)
最佳答案
因为有最好的选择,你可以通过内部 bean 的帮助来做到这一点。
public class myForm
{
private String code; (getter and setter)
private Map<String,InnerBeanObject> map(getter and setter)
}
如果您使用内部 bean,您可以进行 setter 注入(inject)来填充您的
Map<String, Object>
如果您使用 xml bean 配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="MyFormBean" class="Myform">
<property name="code" >
<property name="map">
<map>
<entry key="Key " value-ref="innerBean"></entry>
</map>
</property>
</bean>
<bean id="innerBean" class="InnerBean">
<property name="InnerBeanMap">
<map>
//get ur bean map key and value
</map>
</bean>
</beans>
如果您使用的是早期版本的 spring.by 注释,Java 代码也有类似的方式@bean
关于spring - 如何在Spring表单内的 map 内填充 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50586642/