Home> Java> javaTutorial> body text

How does the controller of the java ssm framework pass parameters to the page?

PHPz
Release: 2023-05-15 19:22:04
forward
1105 people have browsed it

The ssm controller passes parameters to the page

Use Map to pass parameters

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 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"; }
Copy after login

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.

 ${operation eq 'add'?'添加':(operation eq 'edit'?'编辑':'查看')}应用系统
Copy after login

Use PrintWriter to pass parameters

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}"); } }
Copy after login

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); } } });
    Copy after login
ssm framework obtains the parameters passed by the page

Receives the name age through @RequestParam

Parameters, and can be empty

@RequestParam(value = "age",required = false)
Copy after login

How does the controller of the java ssm framework pass parameters to the page?

Passed @PathVariable

How does the controller of the java ssm framework pass parameters to the page?##Passed @RequestBody –Not applicable to Get Request

How does the controller of the java ssm framework pass parameters to the page?

    1.@RequestBody receives a request body, only one @RequestBody can exist, and receives all request parameters - once After receiving
  • 2. If you pass an object or array, it must be converted to Json format\or a pure string first
  • 3.@RequestBody No Suitable for Get requests
  • Receive date type: @DateTimeFormat\@JsonFormat

  • @DateTimeFormat

    Usage scenario: page When the date format is passed directly, use this annotation to receive it directly;

  • @JsonFormat

    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

  • Usage:

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!