简体   繁体   English

Spring MVC Bean转换

[英]Spring MVC Bean conversion

I have a POJO that I'm using successfully in a JSP form. 我有一个POJO,我在JSP表单中成功使用。 I can display the form, and can post the result back. 我可以显示表单,并可以将结果发回。 Here is the modelAttribute bean 这是modelAttribute bean

public class FooAdminWebDTO implements Serializable {

private static final long serialVersionUID = -5296961362891142744L;

private List<FooDTO> FooDTOList;
private String success = "success"; //..etc

This is an except from my JSP page: 这是我的JSP页面之外的一个:

    <form:form method="post" class="form-horizontal" id="viewForm"
           action="${pageContext.request.contextPath}/FooAdmin/saveFoo" modelAttribute="FooAdminWebDTO">

        <div class="form-group">
        <form:label class="col-sm-2" path="FooDTO" for="FooDTO" required="true">XYZ:</form:label>
        <form:input class="col-sm-5" path="FooDTO" id="FooDTO" type="number" value="${FooAdminWebDTO.FooDTO.FooXYZ}" required="true"/>
    </div>

    <div class="form-group">
        <form:label class="col-sm-2" path="success" for="success" required="true">Success:</form:label>
        <form:input class="col-sm-5" path="success" id="success" type="text" value="SUCCESS" required="true"/>
    </div>

The problem is in my POST area. 问题出在我的POST区域。 I am successfully hitting the correct controller method: 我成功地使用了正确的控制器方法:

    @RequestMapping(value = "/FooAdmin/saveFoo", method = RequestMethod.POST)
public ModelAndView saveFooByID(@ModelAttribute("FooAdminWebDTO") FooAdminWebDTO s, BindingResult bindingResult) {
    return new ModelAndView();
}

However, as expected, I'm only seeing the contents of the success field being filled by Spring. 但是,正如预期的那样,我只看到Spring填充成功字段的内容。 The success field is a simple string. 成功字段是一个简单的字符串。 (All other complex fields are null). (所有其他复杂字段为空)。

Here is the object: FooDTO: 这是对象:FooDTO:

@JavascriptMappable
public class FooDTO implements Serializable {

private static final long serialVersionUID = -5974170234812308892L;
private String FooDescription;
private int additionQuantity;

etc.. 等等..

Please can someone guide me on how to do this? 请有人指导我如何做到这一点? I use XML for configuration, so any answers involving config, please keep this in mind. 我使用XML进行配置,所以任何涉及配置的答案,请记住这一点。 I have quite deeply nested objects, and need a way to bind my objects to the incoming POST object. 我有很深的嵌套对象,需要一种方法将我的对象绑定到传入的POST对象。

Thank you. 谢谢。

The value of your path in form:input should map to the property you'd want to populate in the object. 表单中路径的值:input应映射到要在对象中填充的属性。

eg for the input tag for ProductAdminWebDTO.ProductDTO.productPartNumber the path should be something like path="productDTO.productPartNumber" 例如,对于ProductAdminWebDTO.ProductDTO.productPartNumber的输入标记,路径应该类似于path="productDTO.productPartNumber"

you can find more info at http://docs.spring.io/spring/docs/current/spring-framework-reference/html/view.html#view-jsp-formtaglib-formtag or simple explanation of form binding . 您可以在http://docs.spring.io/spring/docs/current/spring-framework-reference/html/view.html#view-jsp-formtaglib-formtag表单绑定的简单说明中找到更多信息。 In your case you need to access nested objects that is, much like when displaying, done with a . 在您的情况下,您需要访问嵌套对象,就像显示时一样,使用.

To recap HttpMessageConverter is responsible for converting the HTTP request message to an assoicated java object. 回顾一下HttpMessageConverter负责将HTTP请求消息转换为一个相关的java对象。 In our case want to convert JSON to a java object when a request is made. 在我们的例子中,想要在发出请求时将JSON转换为java对象。 Spring will look specifically for a HttpMessageConverter associated to the mime type to perform the conversion. Spring将专门寻找与mime类型相关联的HttpMessageConverter来执行转换。

You must add jackson dependency to your project as well as to register in spring as message converter. 您必须将jackson依赖项添加到项目中,并在spring中注册为消息转换器。

Check this too: Spring 3.0 making JSON response using jackson message converter 检查一下: Spring 3.0使用jackson消息转换器进行JSON响应

Found the error. 发现错误。 It was a piece of JQuery 这是一个JQuery

     $('#viewForm input').attr("disabled", true);
    $('#viewForm select').attr("disabled", true);

I was using it elsewhere in the JSP to make the fields un-editable. 我在JSP的其他地方使用它来使字段不可编辑。 I'd forgotten to re-enable the form, which is why it wasn't submitting correctly. 我忘了重新启用表单,这就是为什么它没有正确提交。 Thanks to those who tried to help. 感谢那些试图帮助的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM