springmvc · 2019-09-05 0

SpringMVC获取请求参数

添加maven依赖

<properties>
    <servlet.version>3.0.1</servlet.version>
    <spring.version>5.1.9.RELEASE</spring.version>
</properties>

<dependencies>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${servlet.version}</version>
    </dependency>

    <!-- spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- spring-beans -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- spring-expression -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

1.形参对应请求参数

这个形参变量接受请求参数的值

如果请求中有这个参数,局部变量的值为参数的值;如果请求中没有这个参数,局部变量的值为null

相当于:username = request.getParameter("username");

//http://localhost:8080/handle11?username=abc
@ResponseBody
@RequestMapping("/handle11")
public String handle01(String username){
    return username;
}

2.@RequestParam、@RequestHeader、@CookieValue、@RequestBody

@RequestParam获取请求参数的;@RequestHeader获取请求头中某个key的值;@CookieValue获取某个cookie的值;@RequestBody获取请求体的值

@RequestParam用于GET请求获得请求行的参数,@RequestBody用于POST请求获得请求体的参数

这三个注解,有三个属性:

value:指定要获取的参数的key

required:这个参数是否是必须的,设置为true(默认)时,如果请求有没有这个参数会报错;设置为false时,会赋值null

dafault:指定默认值

@ResponseBody
@RequestMapping("/handle21")
public String handle21(@RequestParam(value = "user", required = false) String username){
    return username;
}

@ResponseBody
@RequestMapping("/handle22")
public String handle22(@RequestHeader("User-Agent") String userAgent){
    return userAgent;
}

@ResponseBody
@RequestMapping("/handle23")
public String handle23(@CookieValue("JSESSIONID") String jid){
    return jid;
}

@ResponseBody
@GetMapping("/handle24")
public Map<String, String> handle24(@RequestParam Map<String, String> map){
    return map;
}

@ResponseBody
@PostMapping("/handle25")
public Map<String, String> handle25(@RequestBody Map<String, String> map) {
    return map;
}

3.形参是JavaBean

如果形参是JavaBean,SpringMVC会自动为JavaBean进行赋值,支持级联属性

Book.java

public class Book {

    private String bookName;
    private String author;
    private Address address;

    public Book() {

    }

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + ''' +
                ", author='" + author + ''' +
                ", address=" + address +
                '}';
    }
    //set、get方法
}

Address.java

public class Address {
    private String province;
    private String city;

    public Address() {

    }

    @Override
    public String toString() {
        return "Address{" +
                "province='" + province + ''' +
                ", city='" + city + ''' +
                '}';
    }
    //get、set方法
}

请求处理方法:

@ResponseBody
@RequestMapping("/handle31")
public String handle31(Book book){
    return book.toString();
}

form表单:get、post请求都行

<form action="/handle31" method="post">
    书名:<input type="text" name="bookName"/><br/>
    作者:<input type="text" name="author"/><br/>
    省份:<input type="text" name="address.province"/><br/>
    城市:<input type="text" name="address.city"/><br/>
    <input type="submit"/>
</form>

4.形参是原生的Servlet API

可以是:

HttpServletRequest、HttpServletResponse、HttpSession、

InputStream、OutputStream、Reader、Writer、

java.sercurity.Principa、Locale(国际化有关的区域信息对象)

@ResponseBody
@RequestMapping("/handle41")
public String handle41(HttpSession session, HttpServletRequest request){
    return "success";
}