Home  >  Article  >  Java  >  How does the controller of the java ssm framework pass parameters to the page?

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

PHPz
PHPzforward
2023-05-15 19:22:041124browse

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<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 &#39;add&#39;?&#39;添加&#39;:(operation eq
                &#39;edit&#39;?&#39;编辑&#39;:&#39;查看&#39;)}应用系统</title>

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

2. Page code writing

  • ##success: The result in function(result) is The content in writer.write() returned by the background

  • $.ajax({
            type : &#39;POST&#39;,
            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 page

Receives the name age through @RequestParam

Parameters, and can be empty

@RequestParam(value = "age",required = false)

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete