Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters from JSP to Controller in Spring MVC

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.

like image 638
user514946 Avatar asked Apr 08 '11 03:04

user514946


People also ask

How can we call controller action from JSP in spring?

Well, in your JSP page, you need to create an html form/button or any other UI control that sends a request to your Controller class. If that's what you are trying to do and its not working then please post the code for the jsp page as well. I used filter for that work in jsp ..

How Pass value from Javascript to controller in Spring MVC?

Add the variable in your form domain class with @Transient annotation so that spring wont look for matching element to your database table. e.g. And then set $("#countrySelection"). value(countrySelection); using your jquery.


2 Answers

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.

like image 121
kalyan Avatar answered Oct 12 '22 15:10

kalyan


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)
{
------
}
like image 26
user2380185 Avatar answered Oct 12 '22 16:10

user2380185