Results 1 to 8 of 8

Thread: @ModelAttribute is there a way to bind it to generic/? instead of a static model type

  1. #1
    Join Date
    Nov 2011
    Posts
    3

    Default @ModelAttribute is there a way to bind it to generic/? instead of a static model type

    @ModelAttribute is there a way to bind it to generic/? instead of a static model type?

    For instance if the signature in my controller is:

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<String> postMV(@ModelAttribute("SomeTestClass") SomeTestClass test, BindingResult result, Model model, SessionStatus status )


    Is it possible to make "SomeTestClass" dynamic? I have 10+ beans that will map to form fields on 1 page and I don't want to create 10 different methods in my controller. Is it possible to populate all 10+ different beans with 1 method in a controller using @ModelAttribute annotation?

    The beans are just POJOs and don't extended a common base class.

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,791

    Default

    Hello

    If these 10 beans even has a relation of hierarchy, is complex know what is the subclass, I mean you must do a check of the type into 9 if/else statement.

    I have 10+ beans that will map to form fields on 1 page
    Each POJO or Entity should belong or has its own JSP file.

    Is illogic has 10 differents POJO and create a unique JSP file that represent these 10 beans. Has no sense

    Seems you must create 10 controllers.

    If you can post some code to get a better idea would be nice
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    Is it possible to make "SomeTestClass" dynamic? I have 10+ beans that will map to form fields on 1 page and I don't want to create 10 different methods in my controller.
    It should be possible but as you sample is right now, Spring MVC doesn't know which subclass to create. Add an @ModelAttribute method to help instantiate the right subclass:

    Code:
    @ModelAttribute
    public SomeTestClass createSomeTestInstance(...) {
       // Create and return correct instance of SomeTestClass ...
    }
    Given the above Spring MVC will use your method to populate the model and then it will simply access it from there by the time it gets to the @RequestMapping method.

  4. #4
    Join Date
    Nov 2011
    Posts
    3

    Default

    Well all the beans are going to be used in the same CRUD operations via JPA/Hibernate... so they all share the same DAO and will be manipulated on in the same way. That is why I did not want to be redundant and declare 10 different controllers if all the beans will be acted upon in the same pattern... if there was only a way to populate them via @ModelAttribute. Now if this is not possible or "illogical" that is fine... I just thought there was a way to achieve this instead of having Many Pages to : Many controllers... I was looking for a 1:1 Page to Controller pattern.

    also I'm actually using freemarker (instead of jsp) and in freemarker I am able to generate dynamic form elements on 1 page from 1 controller but apparently to go the other direction and dynamically generate models based on the contents of the form fields on that single page is not possible.
    Last edited by airduster; Dec 14th, 2011 at 11:33 AM.

  5. #5
    Join Date
    Dec 2011
    Posts
    1

    Default

    how about trying a wrapper bean that wraps the 10+ beans that you have and use it as your modelattribute.

  6. #6
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    @airduster I provided an answer above (comment #3). Perhaps you didn't see it since you comment came later but didn't acknowledge it?

  7. #7
    Join Date
    Nov 2011
    Posts
    3

    Default

    @Rossen S.

    In the documentation section 13.11.6:

    13.11.6. Providing a link to data from the model with @ModelAttribute

    @ModelAttribute has two usage scenarios in controllers. When placed on a method parameter, @ModelAttribute is used to map a model attribute to the specific, annotated method parameter (see the processSubmit() method below). This is how the controller gets a reference to the object holding the data entered in the form. In addition, the parameter can be declared as the specific type of the form backing object rather than as a generic java.lang.Object, thus increasing type safety.

    @ModelAttribute is also used at the method level to provide reference data for the model (see the populatePetTypes() method below). For this usage the method signature can contain the same types as documented above for the @RequestMapping annotation.

    Note: @ModelAttribute annotated methods will be executed before the chosen @RequestMapping annotated handler method. They effectively pre-populate the implicit model with specific attributes, often loaded from a database. Such an attribute can then already be accessed through @ModelAttribute annotated handler method parameters in the chosen handler method, potentially with binding and validation applied to it.
    Your solution would work if I was loading values from a database but I need to HTTP/POST from form fields to populate each model which requires me to use the @ModelAttribute annotation on the parameter as oppose to the method level which is what you proposed. Unless something I am overlooking? Is there a way to HTTP/POST to a model using @ModelAttribute at the method level? If yes could you provide an application example?

  8. #8
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    airduster, it doesn't matter where the object is loaded from. Typically you would have an @ModelAttribute method argument (say named "foo") on the @RequestMapping method and it will be instantiated for you using the default constructor if it isn't already present in the model.

    Now you could also add an @ModelAttribute method that returns a Foo or adds Foo to the model. Since this method is executed before all @RequestMapping methods, by the time your @RequestMapping method is invoked, the model will already contain "foo" and that will be used rather than creating a new instance.

    You'll find an example of this in the spring-mvc-showcase.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •