1

@ModelAttribute uses @RequestParam for each variable inside the Pojo class and @RequestParam uses request.getParameter()(Am I Correct??).

But from Spring MVC Documentation it's sure that @ModelAttribute populates the request parameters to bind to argument fields.

  • But my question is if pojo class is a generic class then it's not possible to get the parameters,Why?

    • Is it because the type is considered as Object always instead of passed argument type(Pojo).
    • Is request object/mapping of variables is not capable of handling Generics?
  • If its a Wrapper class than it will work fine because it will be sure about the param type(i.e Pojo class).

Sample Code to check:

Controller

@RequestMapping(value = "/generic", method = RequestMethod.GET)
public String generic(@ModelAttribute Generic<ParamClass> generic) {
    return null;
}

@RequestMapping(value = "/wrapper", method = RequestMethod.GET)
public String wrapper(@ModelAttribute WrapperClass wrpr) {  //wrpr contains value
    return null;
}

Wrapper Class

public class WrapperClass {
    private ParamClass paramList;
    //getter & setter
}

Generic class

public class Generic<T> {
    private T paramList;
    //getter & setter
}

Pojo Class

public class ParamClass {
    private String param;
    // getter & setter
}

Jsp form

<form action="/wrapper">
    <table>
        <tr>
            <td><input type="text" name="paramList.param" /></td>
        </tr>
    </table>
    <input type="submit" value="Submit">
</form> 

So one more question is,is it always required to have Wrapper class for each type? Is there any better solution(except having all param type variables in one class)?

Prasanna Kumar H A
  • 2,923
  • 4
  • 20
  • 46
  • Your sample won't work in either case. Also your assumptions/understanding is wrong `@ModelAttribute` and `@RequestParam` have nothing to do with each other. Spring binds on property paths and uses the parameter names for that, but that is all that there is in common. – M. Deinum Oct 18 '16 at 07:16
  • @M.Deinum I have tested the code and pasted here...For wrapper it will work for sure...[Documentation-SO]http://stackoverflow.com/questions/29370581/spring-mvc-please-explain-difference-between-requestparam-and-modelattribute – Prasanna Kumar H A Oct 18 '16 at 07:17
  • Your code won't work. The JSP will not post to the correct URL nor does the name of the input type match the path of the binding... So it simply cannot work. If it works, it appears to work but binding will at least not work. – M. Deinum Oct 18 '16 at 07:20
  • @M.Deinum updated the question with proper parameter and url – Prasanna Kumar H A Oct 18 '16 at 07:23
  • The main issue is that it is a generic typed nested path. To do binding it needs to create an instance of the nested path. But due to the nature of generics that isn't possible. – M. Deinum Oct 18 '16 at 08:03
  • @M.Deinum Why that so? we are mentioning Generic type there...which will become `private ParamClass paramList;` – Prasanna Kumar H A Oct 18 '16 at 08:10
  • 1
    As stated that is the nature of generics... Those aren't available after compile time anymore... See http://stackoverflow.com/questions/339699/java-generics-type-erasure-when-and-what-happens – M. Deinum Oct 18 '16 at 08:45
  • @M.Deinum one thing is clear now...Generics has nothing to do with except some compatible for all required class/type...Java always use casting for generics while compiling..But,is `@Model Attribute` doesn't give support to casting(indirectly supporting generics)?? and also in compiling if it comes to know the type(atleast to cast) why can't it's used request mapping – Prasanna Kumar H A Oct 18 '16 at 10:43
  • 1
    Because the type isn't there anymore... Spring has no way to dtermine the type of `T` anymore at runtime and as such cannot instantiate an instance of that type using reflection... And thus cannot do binding. – M. Deinum Oct 18 '16 at 10:48
  • Ok Convert this to answer(with documentation reference) and any best solution to avoid creating multiple wrapper classes(all param type variables in one class) – Prasanna Kumar H A Oct 18 '16 at 11:16

0 Answers0