Spring 3.1 modelAttribute

Still working on this post…

Maddening searches to figure out how to properly use the modelAttribute keyword in the controller and the forms.

Here is a list of the mistakes I corrected to get to my goal

  • Typo: @ModelAttribute in the Java classes but modelAttribute in the jsp. Lower case “m” for jsp pages. When you have it right the id field in your rendered jsp page shows the proper model attribute name.
    <form:form modelAttribute=”madmartin” action=”/madmartin/${id}” method=”post”>
    gives html like this:
    <form id=”madmartin” action=”/madmartin/393216″ method=”post”>
    Got it from this page:  http://www.springbyexample.org/examples/spring-web-flow-subflow-webapp-jsp-example.html
    Too many other pages had it wrong. Mad +1
  • Could not use the @ModelAttribute on a method to create my object and then put data in the method processing the request. I had to put the @ModelAttribute in the argument of the method processing the request.
    I think this is the result of my misunderstanding of some feature. Idiot +1
  • Stole some code from the Spring documentation to fix a conversion issue from String to Date format I used in one of my model.
        @InitBinder
        public void initBinder(WebDataBinder binder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
        }

    Code re-use at it’s best. Copy and Paste +1

Advertisements

Published by

m5c

Java developper that loves photography and good coffee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s