简体   繁体   中英

How to call Spring MVC Portlet Controller method by selecting value from a dropdown in jsp

I want to call controller method after user selects a value from the dropdown in JSP. But not getting how to move forward. My jsp looks like this:

<script type="text/javascript">
function repopulate(){
alert(document.getElementById('test').value);
}</script>

<form:select id="test" path="billingOverview.msisdnNumber"  onChange="repopulate()">
                    <form:option value="" label="All" />
                    <c:forEach var = "billingOver" items = "${billingOverview.prepaidBillingInfo}">
                    <form:option value="${billingOver.msisdn}" ><c:out value="${billingOver.msisdn}"/></form:option>
                    </c:forEach>
</form:select>

Dropdown is coming fine and when i am selecting any value, i am getting that value in the alert too. Now i am not able to call controller method and not able to pass this selected value. I am using spring portlet mvc. Someone please help me on this.

You are already getting selected value in alert, so you can use following code in place of alert in your code -

var parameter = document.getElementById('test').value;
window.location.href = controllerURL+"?param="+parameter ;  

where controllerURL can be like this

http://localhost:8080/applicationname/yourFunction

and your controller should accept parameter like this -

@RequestMapping(value="/yourFunction" method = RequestMethod.GET)
public String getParam(@RequestParam("param") int param, ModelMap model) {
   // method implementation
}

You haven't mentioned you need to get some response from server and update the form or just call the controller and take user to another view.

If you want to get a response and update the form you will have to use ajax, add jquery api in your project . check spring mvc showcase for different capabilities of mvc.

Learn ajax it would be a good tool in your armor. a sample ajax call:

    <script type="text/javascript" >
    function repopulate(){

        var formdata = $('#test').serialize();            
            $.ajax({
            url: <controller_url>,
            type:"POST", //or GET
            data: formdata
            }).done(function(data,textStatus,response) {
               alert(response.responseText);
            });
    }
    </script>

if you just want to call the controller put controller url in form "action" attribute.

<form:select id="test" path="billingOverview.msisdnNumber"  action="Controller_url">

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