Sandeep
Sandeep

Reputation: 407

Spring - Passing value to Controller from JSP GET method

Applications: Java 6, Spring MVC 3.x, JSP

I am aware of modelAttribute for passing an object when using with POST. Say, we are creating a new employee then in JSP page, following can be used

<sf:form method="POST" modelAttribute="employee">

My requirement is to pass a single text box value from JSP page to controller. Can you please suggest how we can do it?

I can guess that JSP page should have

<sf:method = "GET" ... >

but how a text box value (which will be inputted by used) be passed to controller? This value then can be used to search database.

Thanks in advance,

Upvotes: 1

Views: 774

Answers (1)

Jayant Varshney
Jayant Varshney

Reputation: 1825

You need not to use form in this case. You can pass the value as a query parameter. When user clicks on search button just read the value using Javascript or jQuery and send as a query parameter.

window.location="myurl?q="+inputBoxValue;

Get this value in the controller from the request object

request.getParameter('q');

Or you can also pass it as a path variable (only if want to go to the Controller)

window.location="myUrl/"+inputBoxValue;

Upvotes: 1

Related Questions