简体   繁体   中英

Spring MVC Bean conversion

I have a POJO that I'm using successfully in a JSP form. I can display the form, and can post the result back. Here is the 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:

    <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. 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. The success field is a simple string. (All other complex fields are null).

Here is the object: 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. I have quite deeply nested objects, and need a way to bind my objects to the incoming POST object.

Thank you.

The value of your path in form:input should map to the property you'd want to populate in the object.

eg for the input tag for ProductAdminWebDTO.ProductDTO.productPartNumber the path should be something like 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 . 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. In our case want to convert JSON to a java object when a request is made. Spring will look specifically for a HttpMessageConverter associated to the mime type to perform the conversion.

You must add jackson dependency to your project as well as to register in spring as message converter.

Check this too: Spring 3.0 making JSON response using jackson message converter

Found the error. It was a piece of 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. I'd forgotten to re-enable the form, which is why it wasn't submitting correctly. Thanks to those who tried to help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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