其他分享
首页 > 其他分享> > Spring MVC学习02--请求参数接收

Spring MVC学习02--请求参数接收

作者:互联网

新建实体类,User和UserList

 1 package com.yas.entity;
 2 
 3 import java.util.Date;
 4 
 5 public class User {
 6     private Integer id;
 7     private String name;
 8     private Boolean gender;
 9     private Date birth;
10 
11     public User() {
12     }
13 
14     public User(Integer id, String name, Boolean gender, Date birth) {
15         this.id = id;
16         this.name = name;
17         this.gender = gender;
18         this.birth = birth;
19     }
20 
21     public Integer getId() {
22         return id;
23     }
24 
25     public void setId(Integer id) {
26         this.id = id;
27     }
28 
29     public String getName() {
30         return name;
31     }
32 
33     public void setName(String name) {
34         this.name = name;
35     }
36 
37     public Boolean getGender() {
38         return gender;
39     }
40 
41     public void setGender(Boolean gender) {
42         this.gender = gender;
43     }
44 
45     public Date getBirth() {
46         return birth;
47     }
48 
49     public void setBirth(Date birth) {
50         this.birth = birth;
51     }
52 
53     @Override
54     public String toString() {
55         return "User{" +
56                 "id=" + id +
57                 ", name='" + name + '\'' +
58                 ", gender=" + gender +
59                 ", birth=" + birth +
60                 '}';
61     }
62 }
 1 package com.yas.entity;
 2 
 3 import java.util.ArrayList;
 4 public class UserList {
 5     private ArrayList<User> users;
 6 
 7     public ArrayList<User> getUsers() {
 8         return users;
 9     }
10 
11     public void setUsers(ArrayList<User> users) {
12         this.users = users;
13     }
14 }

 

建立用于测试的Controller:ParamController 

 1 package com.yas.controller;
 2 
 3 import com.yas.entity.User;
 4 import com.yas.entity.UserList;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.PathVariable;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestParam;
 9 
10 import java.util.Date;
11 
12 @Controller
13 @RequestMapping("/param")
14 public class ParamController {
15 
16     // http://localhost:8080/param/test1?id=1&name=zhangsan&gender=true&birth=2021/10/26
17     @RequestMapping("/test1")
18     public String test1(@RequestParam("id") Integer id,
19                         @RequestParam("name") String name,
20                         @RequestParam("gender") Boolean gender,
21                         @RequestParam("birth") Date birth){
22         System.out.println(id);
23         System.out.println(name);
24         System.out.println(gender);
25         System.out.println(birth);
26 
27         return "hello";
28     }
29 
30     // http://localhost:8080/param/test2?id=1&name=zhangsan&gender=true&birth=2021/10/26
31     @RequestMapping("/test2")
32     public String test2(User user){
33         System.out.println(user.getId());
34         System.out.println(user.getName());
35         System.out.println(user.getGender());
36         System.out.println(user.getBirth());
37 
38         return "hello";
39     }
40 
41     // http://localhost:8080/param/test3?hobby=football&hobby=basketball&hobby=volleyball
42     @RequestMapping("/test3")
43     public String test3(@RequestParam("hobby") String[] hobby){
44         for(String h : hobby){
45             System.out.println(h);
46         }
47         return "hello";
48     }
49 
50     // http://localhost:8080/param.jsp
51     @RequestMapping("/test4")
52     public String test4(UserList userList){
53         if(userList!=null) {
54             for (User user : userList.getUsers()) {
55                 System.out.println(user.toString());
56             }
57         }
58         return "hello";
59     }
60 
61     // http://localhost:8080/param/test5/1
62     @RequestMapping("/test5/{id}")
63     public String test5(@PathVariable("id") Integer id){
64         System.out.println(id);
65         return "hello";
66     }
67 
68     // http://localhost:8080/param/test6/1/zhangsan
69     @RequestMapping("/test6/{id}/{name}")
70     public String test5(@PathVariable("id") Integer id,
71                         @PathVariable("name") String name){
72         System.out.println(id);
73         System.out.println(name);
74         return "hello";
75     }
76 }

 

建立测试用jsp页面

<%--
  Created by IntelliJ IDEA.
  User: yanga
  Date: 2021/10/26
  Time: 11:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>prarm.jsp</title>
</head>
<body>
<%--    <form action="${pageContext.request.contextPath}/param/test3">--%>
<%--        <input type="checkbox" name="hobby" value="football"> 足球--%>
<%--        <input type="checkbox" name="hobby" value="basketball"> 篮球--%>
<%--        <input type="checkbox" name="hobby" value="volleyball"> 排球--%>

<%--        <input type="submit" value="提交">--%>
<%--    </form>--%>

<form action="${pageContext.request.contextPath}/param/test4" method="post">
    id:<input type="text" name="users[0].id" value="1"><br>
    name:<input type="text" name="users[0].name" value="zhangsan"><br>
    gender:<input type="text" name="users[0].gender" value="true"><br>
    <hr>
    id:<input type="text" name="users[1].id" value="2"><br>
    name:<input type="text" name="users[1].name" value="lisi"><br>
    gender:<input type="text" name="users[1].gender" value="false"><br>

    <input type="submit" value="提交">
</form>

</body>
</html>

 

修改web.xml,添加post请求包含中文的编码处理:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc.xml</param-value>
        </init-param>
        <!-- <load-on-startup>1</load-on-startup> -->
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 配置springMVC编码过滤器 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- 设置过滤器中的属性值 -->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <!-- 启动过滤器 -->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <!-- 过滤所有请求 -->
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 

标签:02,name,--,Spring,id,birth,gender,public,String
来源: https://www.cnblogs.com/asenyang/p/15465202.html