Is it possible to get jsp variable from app.js?

How can I map a single form of jsp to two different methods in controller using RequestMapping?

  • I am creating a Spring web application. I have two input fields in the homepage where the user can enter either a number or name which I am getting it as String. If the user enters number I should pass it to method A and if the user enters the name I should pass it to method B. Following is my jsp <form method="GET" > <input type="text" id="regNo" name="regNo" size="30" maxLength="50"></input> <input type="text" id="studentName" name="studentName" size="30" maxLength="50"></input> <button onclick="processInput();">Search </button> </form> <script> function processInput(){ if (document.getElementById('regNo').value !=""){ alert("Value"); $.ajax({ type : 'GET', url : "ProjectCtxt/mvc/search/s", data : { "regNo":$("#regNo").val(), success : function(result) { //show your result $("#displayArea").html(result); }} }); }else if (document.getElementById('studentName').value !=""){ $.ajax({ type : 'GET', url : "search/l", data : { "studentName":$("#studentName").val(), success : function(result) { //show your result }} }); } } </script> Following is my controller @Controller("resultController") public class ResultController { private final ResultService resultService; @Autowired public ResultController(ResultService resultService) { this.resultService = resultService; } @RequestMapping(value ="/template", method = RequestMethod.GET) public String getPersonList(ModelMap model) { return "header"; } @RequestMapping(value = "/search/s", method = RequestMethod.GET, params = { "regno" }) public String getStudentResult(@RequestParam(value = "regNo", required = true) String regNo, ModelMap model){ model.addAttribute("studentResult",resultService.getStudentResult(regNo)); return "numberResult"; } @RequestMapping(value = "/search/l/studentName={studentName}", method = RequestMethod.GET, params = { "studentName" }) public String searchStudentByCollOrDept(String studentName, ModelMap model){ model.addAttribute("nameList",resultService.getStudentList(studentName)); return "nameResult"; } } I generally use localhost:8080/ProjectCtxt/mvc/template to see homepage. I use Apache tiles. Please answer the following questions 1) Is my url in if block and else block of Javascript in JSP is good? 2) Is the RequestMapping tag written correctly in controller? 3) Since I am using Apache tiles, the jsp I have shown above is always going to stay in header.jsp. But if the controller returns nameResult or numberResult, should I have a div tag there(named displayArea in numberResult or nameResult jsp) which I should use in the above written JS function? Please help me. Thanks.

  • Answer:

    Refer to : http://www.java2s.com/Questions_And_Answers/Spring/MVC/RequestMapping.htm If the @RequestMapping annotation allows specifying a name for the mapping, we might be able to just hard-code the path into the @RequestMapping and get a map of paths from a custom HandlerMapping implementation to use in either the taglib functions or directly in the JSP. For example: Controller Code: @RequestMapping(name="loginPage", value="/login", method=GET) public String loginPage(ModelMap model) {     model.addAttribute("controllerPaths", RequestHandler.getMappedPaths());     return "login.jsp"; }  public static final String loginPage() {     return RequestHandler.getMappedPaths().get('loginPage'); } JSP Code: <%@ taglib prefix="ex" uri="http://www.example.com/jsp/jstl/functions" %> <a href="${controllerPaths('loginPage')}">Login here</a> <a href="${ex:loginPage()}">Or here</a> If we changed the mapping from /login to /signin, as long as we keep the name constant, the JSP links will not break. However, this only adds another layer of abstraction that does not offer any protection if the mapping is removed or the name changes (JSP links will be broken and won't be reported by compilers). I am hoping to either: 1. Have a single place where paths are defined that is referenced by controllers and JSPs. It would be great if that same place is able to encapsulate request param data and all other request-mapping related stuff. 2. OR somehow enable checking of path references in JSPs at compile-time or jsp-compile-time. I think having a taglib that generates URLs sort of fills this capacity if the query params change. via spring project

Deepak Pandey at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.