基于Spring MVC实现HttpServletRequest、基本数据类型、Java Bean、数组、List、Map、JSON方式的参数传递。
1. 创建项目
选择Maven快速构建web项目,项目名称为case13-springmvc02。
2. 配置Maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wfit.springmvc</groupId>
<artifactId>springmvc02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--spring mvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.25</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
</dependencies>
</project>
3. 更新Maven仓库
4. 创建java和resources目录
src.main路径下,执行new – Directory操作,选择java、resources后,执行回车键。
5. 创建Spring MVC配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启注解-->
<context:component-scan base-package="com.wfit"/>
<!--启用mvc-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
6. 配置web.xml
在web.xml中配置DispatcherServlet。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<!--解决POST中文乱码问题 过滤器-->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置前端控制器DispatcherServlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载SpringMVC文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--启动容器时加载servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--表示拦截所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
7. 创建Student实体类
src.main.java.com.wfit.entity目录下创建Student.java。
public class Student {
public String id;
public String name;
public int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
8. 创建ParamController类
在src.main.java.com.wfit.param目录下创建ParamController.java。
HttpServletRequest参数传递
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* 通过HttpServletRequest接收参数
*/
@RequestMapping("/servlet")
public void servlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String name = request.getParameter("name");
System.out.println("name=" + name);
PrintWriter writer = response.getWriter();
writer.print("hello " + name);
writer.flush();
writer.close();
}
}
基本数据类型参数传递
基本数据类型以及他们的包装类。
/**
* 使用基本数据类型和String类型
*/
@RequestMapping(value = "/simple",produces = "text/plain;charset=UTF-8")
@ResponseBody
public String simple(String id,String name,int age){
String str = id + "," + name + "," + age;
System.out.println(str);
return str;
}
Java Bean参数传递
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* JavaBean传递参数
*/
@RequestMapping(value = "/bean",method = RequestMethod.POST,
produces = "text/plain;charset=UTF-8")
@ResponseBody
public String bean(Student student){
System.out.println(student);
return student.toString();
}
}
数组参数传递
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* 数组参数
*/
@RequestMapping(value = "/array",method = RequestMethod.POST)
@ResponseBody
public String array(String[] hobbies){
for(String hobby : hobbies){
System.out.println(hobby);
}
return "success";
}
}
List参数传递
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* List参数
*/
@RequestMapping(value = "/list",method = RequestMethod.POST)
@ResponseBody
public String list(@RequestParam List<String> hobbyList){
hobbyList.forEach(hobby -> System.out.println(hobby));
return "success";
}
}
Map参数传递
@Controller
@RequestMapping("/param")
public class ParamController {
/**
* Map参数
*/
@RequestMapping(value = "/map",method = RequestMethod.POST)
@ResponseBody
public String map(@RequestParam Map<String,Object> map){
System.out.println(map);
return "success";
}
}
9. 创建JsonController类
在src.main.java.com.wfit目录下创建JsonController类,实现JSON参数转成Java Bean、Map、List等引用类型。
Json Bean参数传递
@Controller
@RequestMapping("/json")
public class JsonController {
/**
* json bean
*/
@RequestMapping(value = "/bean",method = RequestMethod.POST)
@ResponseBody
public Object bean(@RequestBody Student student){
System.out.println(student);
Map<String,Object> map = new HashMap<String,Object>();
map.put("code",200);
map.put("msg","success");
map.put("data",student.toString());
return map;
}
}
Json Map参数传递
@Controller
@RequestMapping("/json")
public class JsonController {
/**
* json map
*/
@RequestMapping(value = "/map",method = RequestMethod.POST)
@ResponseBody
public Object map(@RequestBody Map<String,Object> jsonMap){
System.out.println(jsonMap);
Map<String,Object> map = new HashMap<String,Object>();
map.put("code",200);
map.put("msg","success");
map.put("data",jsonMap);
return map;
}
}
Json List参数传递
@Controller
@RequestMapping("/json")
public class JsonController {
/**
* json list
*/
@RequestMapping("/list")
@ResponseBody
public Object list(@RequestBody List<Student> studentList){
System.out.println(studentList);
Map<String,Object> map = new HashMap<String,Object>();
map.put("code",200);
map.put("msg","success");
map.put("data",studentList);
return map;
}
}