zoukankan      html  css  js  c++  java
  • Spring MVC请求参数绑定

    所谓请求参数绑定,就是在控制器方法中,将请求参数绑定到方法参数上

    • @RequestParam

    绑定单个请求参数到方法参数上

    @RequestParam("id") Integer id

    将请求参数名为id的变量,绑定到id参数上,如果不知道@RequestParam值,默认从请求参数中取和变量名相同的请求参数

    @RequestParam(value = "id",required = false) Integer id

    可以通过指定required参数来指定是否必须有这个请求参数,默认是true的。如果true了,那么当出现无法正常参数绑定的话,就会报错。

    • @PathVariable

    绑定URI中的模板变量到方法参数上

    @PathVariable("id") String id
    <a href="${pageContext.request.contextPath}/testModule/123/save.do">URI模板变量值绑定</a>
     1 package org.zln.myWeb.controller;
     2 
     3 import org.apache.logging.log4j.LogManager;
     4 import org.apache.logging.log4j.Logger;
     5 import org.springframework.stereotype.Controller;
     6 import org.springframework.ui.Model;
     7 import org.springframework.web.bind.annotation.PathVariable;
     8 import org.springframework.web.bind.annotation.RequestMapping;
     9 
    10 /**
    11  * Created by sherry on 16/8/18.
    12  *
    13  */
    14 @Controller
    15 @RequestMapping("/testModule/**")
    16 public class T00_TestController {
    17 
    18     private Logger logger = LogManager.getLogger();
    19 
    20     @RequestMapping("/{num}/save.do")
    21     public String testPathVariable(@PathVariable("num") String num, Model model){
    22         logger.info("模板变量:"+num);
    23         model.addAttribute("num",num);
    24         return "testPathVariable";
    25     }
    26 }
    控制器
    模板变量的值:<c:out value="${num}"/><br/>
    • @CookieValue

    绑定Cookie值

    • @RequestHeader

    绑定请求头

    请求参数如果是一个普通的POJO,带有setter方法,Spring MVC会自动将表单参数注入到POJO的成员变量中

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
    <!DOCTYPE HTML>
    <html>
    <head>
        <title>Title</title>
        <meta charset="UTF-8"/>
    </head>
    <body>
    
        <form action="${pageContext.request.contextPath}/testModule/testModelValue.do" name="testModelValue">
            <table>
                <thead>演示@ModelValue</thead>
                <tbody>
                    <tr>
                        <td>姓名:</td>
                        <td>
                            <input type="text" placeholder="输入姓名..." name="name"/>
                        </td>
                    </tr>
                    <tr>
                        <td>年龄:</td>
                        <td>
                            <input type="text" placeholder="输入年龄..." name="age"/>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="submit" value="提交请求" />
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    
    </body>
    </html>
    表单
        @RequestMapping("testModelValue.do")
        public String testModelValue(T00_TestModelValue t00_testModelValue){
            logger.info("参数对象:"+t00_testModelValue);
            return "testModelValueUI";
        }
    处理方法
    • @SessionAttribute

    绑定请求到session范围

    • @RequestBody

    将内容区数据绑定到方法变量,并自动进行类型转换

    • @RequestPart

    绑定 "multipart/data" 类型的数据,除了能绑定 @RequestParam能绑定的请求参数外,还能绑定上传的文件

    一般请求参数绑定到方法参数上,也可以使用 request 类型的数据去获取,但是还是建议使用特定的注解将请求参数绑定到方法变量上,因为这样使用起来更简单。

  • 相关阅读:
    【Maven】安装配置、目录结构、配置文件、常见命令
    【Maven】基础概念、仓库、构建与部属
    【float】与【position】汇总
    【CSS】定义元素的对齐方式
    【CSS】元素样式
    【CSS】绝对定位和相对定位
    网页常见布局
    php--常用的时间处理函数
    16位cpu下主引导扇区及用户程序的编写
    浅谈pageobject模式
  • 原文地址:https://www.cnblogs.com/sherrykid/p/5785860.html
Copyright © 2011-2022 走看看