spring - 如何在Spring表单内的 map 内填充 map ?

标签 spring forms spring-mvc jsp

我有以下表格

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/

相关文章:

spring - Spring MVC中的多 View 解析器

重复来自 spring boot rest Controller 的 JSON 响应

jquery - 在所有浏览器中使用 &lt;input type ="file"> 时如何使用上传文件的 JQUERY 获取完整路径,为什么 IE、Chrome 返回 "c:/fakepath/filname"?

jquery:阻止表单提交

java - 使用 Spring、Tomcat 和 Java,如何使用 HTTP header 重定向请求?

java - Spring BasicProcessingFilter 迁移到 Spring Security 4

java - 将java类数据转成JSON格式?

javascript - 错误 415。使用 Spring 提供 REST 服务 - AngularJs

java - 为什么 spring boot angularjs gateway 应用程序不能从 ui 应用程序读取?

从 JSON 或类似数据生成/呈现动态 HTML 表单的 JavaScript?