1

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.

wax_lyrical
  • 922
  • 1
  • 10
  • 22
  • It's just to ensure, did you set the object attribute 'productAdminWebDTO' in your controller? I think if not then how you are expecting value for 'productDTO' field. Does your JSP page displaying the value for 'productAdminWebDTO.productDTO.productPrice'? – ABM Mahbubor Rahman Jan 24 '17 at 18:21

3 Answers3

0

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

e.g. 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 .

Redlab
  • 3,110
  • 19
  • 17
  • Thanks, but I don't understand your answer, which seems to relate the presentation of the JSP page. The JSP page renders fine. The fields in the JSP page are all populated. It's in the POST back to the server in the controller that I get problems, when I'm trying to pick up the form values - all fields are null except "success" String. – wax_lyrical Jan 24 '17 at 13:46
  • What you suggested breaks the JSP: Invalid property 'productAdminWebDTO' of bean class [ProductAdminWebDTO]: Bean property 'productAdminWebDTO' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type setter? – wax_lyrical Jan 24 '17 at 13:52
  • productAdminWebDTO is already set as modelAttribute in your form element so that should not be in the value of path. I'll adapt – Redlab Jan 24 '17 at 14:21
  • Thanks for adapting- I did try your suggestion. Nothing, sadly, has changed. The JSP still displays as before, but all fields in the ProductAdminWebDTO are null still, except the flat string. Is this, as I originally asked to do with Spring not understanding these complex objects and only understanding Strings? – wax_lyrical Jan 24 '17 at 14:44
  • we don't see all code in ProductAdminWebDTO but it is possible spring tries to set the data in productDTO that is `null` in your ProductAdminWebDTO . You might need to instantiate a `new ProductDTO();` _List are normally instantiated by spring itself_ – Redlab Jan 24 '17 at 15:39
  • remove content='application/json' from the form:form element. – Redlab Jan 24 '17 at 16:29
0

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

Community
  • 1
  • 1
John
  • 1
  • i did not see the content="application/json" .. if you want your controller to accept json you should also defined it there. http://stackoverflow.com/questions/17835545/spring-mvc-not-able-to-submit-form-data-in-json-format – Redlab Jan 24 '17 at 16:23
  • then again, i'm not even sure that form:form supports that? next to that, does any browser support submitting a form as application/json ? http://stackoverflow.com/questions/38017123/is-form-enctype-appication-json-available – Redlab Jan 24 '17 at 16:27
  • I think this might be a red-herring. I'd forgotten about setting the content explicitly, as it works either way. So I've removed this from the post question. In all cases a basic string is converted, but still none of the complex types. – wax_lyrical Jan 24 '17 at 17:33
0

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.

wax_lyrical
  • 922
  • 1
  • 10
  • 22
  • happy for you that you got it working. Should you have posted more code it might have been resolved sooner and we would not have looked for solution in the wrong direction. – Redlab Jan 25 '17 at 12:43