- 前言
- 1.基本資料類型(涉及亂碼問題)
- 2.包裝類型
- 3.通過@RequestParam擷取參數
- 4.字元串類型
- 5.數組類型
- 6.JavaBean類型
- 7.List類型
前言
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 + '\'' +
'}';
}
}