前言
Spring MVC支持对多种类型的请求参数进行封装
- 基本类型
- 包装类型
- JavaBean
- 数组类型
- 字符串类型
- 集合类型
因为主要是为了学习参数绑定的内容,所以有的方法,我就没有返回视图,大家不用在意浏览器的页面报404,关键是传参问题
1.基本数据类型(涉及乱码问题)
设计表单页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/springmvc01/param" method="post">
用户名:<input type="text" name="username"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
success.jsp(路径webapp/jsp/success.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
传参成功
</body>
</html>
编写控制器接收参数
@Controller
public class ParamController {
@RequestMapping("/param")
public String save(String username,Integer age){
System.out.println("用户名:"+username);
System.out.println("年龄:"+age);
return "success";
}
}
这里要注意的是,控制器接收参数的形参名称必须和表单的name属性保持一致,否则会接收失败!
Spring MVC.xml配置
<?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.zyh.controller"></context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--给逻辑视图加上前缀和后缀 -->
<!--前缀-->
<property name="prefix" value="/jsp/"></property>
<!--后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--开启注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
我们发现出现了乱码问题,这时我们可以配置Spring MVC提供字符编码过滤器来解决问题。
<!--字符编码过滤器-->
<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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.包装类型
@RequestMapping("/packageType")
public void packageType(Integer id){
System.out.println("id:"+id);
}
3.通过@RequestParam获取参数
如果我们的前端参数名称和后端方法参数名称不一致,就无法获取到对应的参数值,为了解决前后端参数名称不一致的问题,SpringMVC框架提供了【@RequestParam】注解,解决参数名称不一致的问题。
4.字符串类型
String类型的参数,如果我们没有传参的话,默认是null
5.数组类型
6.JavaBean类型
准备一个实体类
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
7.List类型
集合类型的数据要使用JavaBean进行封装
public class User {
private Integer id;
private String username;
private String password;
private List<Integer> ids;
public List<Integer> getIds() {
return ids;
}
public void setIds(List<Integer> ids) {
this.ids = ids;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", ids=" + ids +
'}';
}
}
list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/data" method="post">
ids0:<input type="text" name="ids[0]">
ids1:<input type="text" name="ids[1]">
<input type="submit" value="提交">
</form>
</body>
</html>
public class Phone {
private String num;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
@Override
public String toString() {
return "Phone{" +
"num='" + num + '\'' +
'}';
}
}