Passing parameters from JSP to Controller in Spring MVC

96,141

Solution 1

Just remove the "path" from the jsp input tag and use HttpServletRequest to retrieve the remaining data.

For example I have a bean like

public class SomeData {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Then in the jsp i will have the additional data fields to be send in normal html tag

<form:form method="post" action="somepage" commandName="somedata">
    <table>
    <tr>
        <td>name</td>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <td>age</td>
        <!--Notice, this is normal html tag, will not be bound to an object -->
        <td><input name="age" type="text"/></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="send"/>
        </td>
    </tr>
</table>
</form:form>

Notice, the somedata bean has the name field the age is not. So the age field is added without "path". Without the path attribute the object property wont be bound to this field.

on the Controller i will have to use the HttpServletRequest like,

@RequestMapping("/somepage")
public String someAction(@ModelAttribute("somedata") SomeData data, Map<String, Object> map,
                                HttpServletRequest request) {

       System.out.println("Name=" + data.getName() + " age=" + request.getParameter("age"));

       /* do some process and send back the data */
        map.put("somedata", data);
        map.put("age", request.getParameter("age"));

        return "somepage";
   }

while accessing the data on the view,

<table>
    <tr>
        <td>name</td>
        <td>${somedata.name}</td>
    </tr>
    <tr>
        <td>age</td>
        <td>${age}</td>
    </tr>
 </table>

somedata is the bean which provides the name property and age is explicitly set attribute by the controller.

Solution 2

If one doesn't want to create another class (bean) though it should be there, then apart from @ModelAttrbute one can also use @RequestParam.

public String someAction(@RequestParam("somedata") String data)
{
------
}
Share:
96,141

Related videos on Youtube

user514946
Author by

user514946

Updated on July 09, 2022

Comments

  • user514946
    user514946 almost 2 years

    I am trying out a sample project using Spring MVC annotated Controllers. All the examples I have found online so far bind the JSP to a particular model and the controller uses @ModelAttribute to retrive the model object in the handler method.

    How do I go about passing other parameters (not present in the Model object) from the JSP to Controller? Do I use JavaScript to do this? Also can someone clarify what the HttpServletRequest object should be used for.

    Thanks.

  • Oleksandr.Bezhan
    Oleksandr.Bezhan almost 11 years
    You can also left out @RequestParam annotation if request parameter name corresponds to method parameter name

Related