Announcement Announcement Module
Collapse
No announcement yet.
ModelAttribute in multiple page Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ModelAttribute in multiple page

    Hi

    I have got an application, and I would like to display a form in every page ( Search Form )
    In this SearchForm I would like to use ModelAttribute. Everything works fine, If I place to every controller the ModelAttribute method definiton (Spring docs)

    But this pattern is not so good, how can I use a ModelAttribute in my whole application (in lots of Controllers)

    My example:

    SearchForm in every page on left top layout like this:
    Code:
    <form:form action="/search" modelAttribute="searchModel">
       <form:input type="text" path="field1" /> 
       ...
    </form:form>
    And I have SearchDTO like this:
    Code:
    public class SearchDTO implements Serializable {
       private String field1;
          //... Other fields, and getters and setters ..
       }
    }
    In Controller where I would like to process the search form looks like:
    Code:
    @Controller
    public class SearchFormProcessController {
        @RequestMapping(value = "/search", method = RequestMethod.POST)
        public ModelAndView doFormProcess( @ModelAttribute("searchModel") SearchDTO searchDTO , final BindingResult result) {
                // Validating, and create ModelAndView...
        }
    }
    Spring documentation said, I must define the modelAttribute method, which said to jsp, and controller how this modell looks like. I define it in SearchFormProcessController:

    Code:
    @Controller
    public class SearchFormProcessController {
    
        @ModelAttribute("searchModel")
        public SearchDTO searchDTO() {
            return new SearchDTO();
        }
    
        @RequestMapping(value = "/search", method = RequestMethod.POST)
        public ModelAndView doFormProcess( @ModelAttribute("searchModel") SearchDTO searchDTO , final BindingResult result) {
            // Validating, and create ModelAndView...
        }
    }
    But in every page got this exception: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'searchModel' available as request attribute

    If I added the ModelAttribute method define to every controller, the app works. But this is not a valid usecase! How can I do it otherway?

    Thanks!
Working...
X