Home >Java >javaTutorial >How does the controller of the java ssm framework pass parameters to the page?
Add a Map type parameter A in the controller method. The key-value pair B is put to the put method of parameter A. The key-value pair B can be obtained on the page.
1.java background code writing, the operation and application key-value pairs are put
@RequestMapping("/edit_form") public String editApplicationFormPage(Map<String, Object> map, HttpServletRequest request, String applicationId) { map.put("operation", "edit"); Application application = applicationService .getApplicationById(applicationId); if(application.getSysBigIcon()==null||application.getSysBigIcon().equals("")){ application.setSysBigIcon("/www/images/default.png"); } if(application.getSysIcon()==null||application.getSysIcon().equals("")){ application.setSysIcon("/www/images/default.png"); } if (application != null) { map.put("application", application); } return "/frame/system/application/application_form"; }
2. The page uses the key-value pairs passed from the background.
The method used is that the key-value pairs must be wrapped with ${}. For example: ${operation} and ${application.orgId}, ${operation} is the operation key-value pair that refers to the background map put, and ${application.orgId} is an object that refers to the application entity of the background map put.
<script type="text/javascript"> window.WWWROOT = "${ctx}"; window.DefaultOrgId = "<%=user.getDefaultOrgId()%>"; window.Operation = "${operation}"; window.OrgId = "${application.orgId}"; window.TaskAppId = "${application.taskAppId}"; window.MenuType = "${application.menuType}"; </script> <title>${operation eq 'add'?'添加':(operation eq 'edit'?'编辑':'查看')}应用系统</title>
Write some content to PrintWriter. Just return these contents to the page.
1. Writing background code
Add a PrintWrite type parameter writer to the controller method, and use the writer.write() method to write content. The page can return this content. The code is as follows:
@RequestMapping("/add") public void add(HttpServletRequest request, HttpServletResponse response,MenuRight menuRight, PrintWriter writer) { try{ Boolean result =menuRightService.addMenuRight(menuRight); writer.write("{\"success\":true}"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); writer.write("{\"success\":false}"); } }
2. Page code writing
##success: The result in function(result) is The content in writer.write() returned by the background
$.ajax({ type : 'POST', url : WWWROOT + "/menuRight/add", data : dat, success : function(result) { if ($.parseJSON(result).success == true) { $(stId).attr("checked", true); } else { alert("添加授权失败"); $(stId).attr("checked", false); } } });ssm framework obtains the parameters passed by the pageReceives the name age through @RequestParam Parameters, and can be empty
@RequestParam(value = "age",required = false)Passed @PathVariable
##Passed @RequestBody –Not applicable to Get Request
Usage scenario: page When the date format is passed directly, use this annotation to receive it directly;
Usage scenario: When the page passes the date format in Json format, use this annotation to receive it; Special Note: The annotation name may be different when using different Json packages
The above is the detailed content of How does the controller of the java ssm framework pass parameters to the page?. For more information, please follow other related articles on the PHP Chinese website!