简体   繁体   中英

Passing value via URL to controller from JSP in Spring MVC

Created an application. Page A displays a droplist containing list of values. If user clicks on particular account, will display a chart. We have similar button right to the acccount droplist. Those are separate JSP's. If user clicks on Page A, then selected account name should move to those 4 jsp page. I have tried via URL. But not getting. Please help.

JSP 1

<a id="priorityCallAnalysis" class="item"> <button type ="button" onclick="getPriorityCall()">Priority </button> </a>
<form:form action="somepage" method="post" commandName="somedata"
                id="taskDetails" enctype="multipart/form-data">
                <div class="row">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Choose Account*</label>
                            <form:select path="accountName" class="form-control"
                                id="accountName" onchange="getDashboard()">

                                <form:option value="" label="--select--"></form:option>
                                <c:forEach items="${accountList}" var="accountName">
                                    <form:option value="${accountName}" label="${accountName}"></form:option>
                                </c:forEach>


                            </form:select>
                        </div>
                    </div>
                </div>
                </form:form>

function getPriorityCall()
{
    var accountName = $("#accountName").val();
    alert(accountName);
    window.location="priorityCall.html?accountName="+accountName+"";
} 

Controller

@RequestMapping(value="/priorityCall")
    public ModelAndView priorityCall(Map<String, Object> model,@RequestParam ("accountName") String accName)
    {
        System.out.println("entry");
        SampleBean template = new SampleBean ();
        model.put("template ", template );
        List<String> accountList = Service.getAccountList();
        model.put("accountList", accountList);
        model.put("accName", accName);

        return new ModelAndView("analByPrior","","");
    }

JSP : analByPrior

<form:form action="#" method="post" commandName="somedata"
                id="taskDetails" enctype="multipart/form-data">
                <div class="row">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Choose Account*</label>
                            <form:select path="accountName" class="form-control"
                            id="accountName" onchange="getAssignmentGroups()">
                            <form:option value="" label="--select--"></form:option>
                            <c:forEach items="${accountList}" var="accName">
                        <form:option value="${accName}" selected="true"> ${accName}</form:option>
                            </c:forEach>

                        </form:select>
                        </div>
                    </div>
                </div>
            </form:form>

UPDATE While clicking on Priority Button, this controller is getting called, instead of PriorityCall.. I dont know y..

@RequestMapping("/priorityCallAnalysis")
    public String someAction(@ModelAttribute("accountName") TicketInfo data, Map<String, Object> map,
                                    HttpServletRequest request) {

        TicketInfo somedata = new TicketInfo();
            map.put("somedata",somedata);
            System.out.println(somedata);
           System.out.println("acc=" + request.getParameter("accountName"));

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

            return "analysisByPriority";
       }

First in the .jsp file you can use a global variable to store context path and use this variable as prefix in all the relative paths.

<script>var context = "${pageContext.request.contextPath}"</script>

Now within your js function use the context path and call the controller.

function getPriorityCall(){
  var accountName = $("#accountName").val();
  alert("contextPath: "+context); // will print your {appName}
  window.location=context+"/priorityCall?accountName="+accountName;
}

So your URI looks like below in your request call.

http://localhost:8080/{appName}/priorityCall?accountName={acountName}

PS:

  • benefit of use this ${pageContext.request.contextPath} is if you change your appName later you dont need to change it in views. It will automatically get the latest contextPath.
  • If you are using Firefow browser install the firebug add-on and Use it for to verify your requests. So you can validate your URI with params.(If its chrome use Inspect Element to validate URI)

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