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
Related Q & A:
- In Visual Studio 2012 or 2013, how can I register a DLL file on Windows 7 64-bit computer using a Custom Action command?Best solution by Stack Overflow
- How can I inject a constant into a directive's controller?Best solution by stackoverflow.com
- How can I separate a video into two parts?Best solution by Yahoo! Answers
- How can I remove a picture's watermark using Matlab's image processing toolbox?Best solution by Yahoo! Answers
- How can I use a controller with PC games?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.