简体   繁体   中英

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,

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM