4.1 環境搭建 創建名為spring_mvc_demo2的新module,過程參考3.1節 4.1.1、創建請求控制器 package org.rain.controller; import org.springframework.stereotype.Controller; /** * @aut ...
4.1 環境搭建
創建名為spring_mvc_demo2的新module,過程參考3.1節
4.1.1、創建請求控制器
package org.rain.controller;
import org.springframework.stereotype.Controller;
/**
* @author liaojy
* @date 2023/10/9 - 8:32
*/
@Controller
public class TestParamController {
}
package org.rain.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author liaojy
* @date 2023/10/9 - 8:49
*/
@Controller
public class PortalController {
@RequestMapping("/")
public String portal(){
return "index";
}
}
4.1.2、創建靜態資源目錄及頁面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首頁</title>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功</title>
</head>
<body>
<h1>success.html</h1>
</body>
</html>
4.1.3、配置tomcat
4.2、通過ServletAPI獲取請求參數
4.2.1、控制器方法示例
如果控制器方法有HttpServletRequest類型的形參,DispatcherServlet就會將封裝了當前請求的對象傳給控制器方法
@RequestMapping("/param/servletAPI")
public String getParamByServletAPI(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username:"+username);
System.out.println("password:"+password);
return "success";
}
4.2.2、頁面請求示例
<form th:action="@{/param/servletAPI}" method="post">
用戶名:<input type="text" name="username">
密碼:<input type="password" name="password">
<input type="submit" value="登錄">
</form>
4.2.3、測試效果
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
4.3、通過控制器方法的形參獲取請求參數
4.3.1、控制器方法示例
如果在控制器方法存在和請求參數同名的形參,DispatcherServlet就會將請求參數賦值給控制器方法中對應的形參
@RequestMapping("/param")
public String getParam(String username,String password){
System.out.println("username:"+username);
System.out.println("password:"+password);
return "success";
}
4.3.2、頁面請求示例
<form th:action="@{/param}" method="post">
用戶名:<input type="text" name="username">
密碼:<input type="password" name="password">
<input type="submit" value="登錄">
</form>
4.3.3、測試效果
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
4.4、@RequestParam註解
@RequestParam註解用於將請求參數和控制器方法的形參建立映射關係,其有三個屬性:
-
value:指定要為形參賦值的請求參數的參數名。
-
required:設置是否必須傳輸此請求參數,預設值為true;
若設置為true時,則當前請求必須傳輸value所指定的請求參數,
若沒有傳輸該請求參數,且沒有設置defaultValue屬性,則頁面報錯400:Required String parameter 'xxx' is not present;
若設置為false時,則當前請求不是必須傳輸value所指定的請求參數,
若沒有傳輸該請求參數,則註解所標識的形參的值為null。 -
defaultValue:設置形參的預設值;
不管required屬性值為true或false,當value所指定的請求參數沒有傳輸或傳輸的值為""時,則使用預設值為形參賦值。
4.4.1、控制器方法示例
@RequestMapping("/param")
public String getParam(@RequestParam(value = "name",required = true,defaultValue = "everyone") String username, String password){
System.out.println("username:"+username);
System.out.println("password:"+password);
return "success";
}
4.4.2、頁面請求示例
<form th:action="@{/param}" method="post">
用戶名:<input type="text" name="name">
密碼:<input type="password" name="password">
<input type="submit" value="登錄">
</form>
4.4.3、有傳參的測試效果
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
4.4.4、無傳參的測試效果
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
4.5、@RequestHeader和@CookieValue
-
@RequestHeader 註解用於將請求頭信息和控制器方法的形參建立映射關係,其三個屬性的用法和@RequestParam的相同
-
@CookieValue 註解用於將Cookie數據和控制器方法的形參建立映射關係,其三個屬性的用法和@RequestParam的相同
4.5.1、控制器方法示例
@RequestMapping("/param")
public String getParam(
@RequestParam(value = "name",required = true,defaultValue = "everyone") String username,
String password,
@RequestHeader("referer") String referer,
@CookieValue(value = "JSESSIONID",required = false) String jsessionId
) {
System.out.println("username:"+username);
System.out.println("password:"+password);
System.out.println("referer:"+referer);
System.out.println("jsessionId:"+jsessionId);
return "success";
}
4.5.2、測試效果
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
4.6、通過實體類形參獲取請求參數(常用)
4.6.1、實體類示例
註意:要保證實體類的屬性名和請求參數名一致
package org.rain.pojo;
/**
* @author liaojy
* @date 2023/10/10 - 8:45
*/
public class User {
private Integer id;
private String username;
private String password;
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = 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 + '\'' +
'}';
}
}
4.6.2、控制器方法示例
@RequestMapping("/param/pojo")
public String getParamByPojo(User user){
System.out.println(user);
return "success";
}
4.6.3、頁面請求示例
<form th:action="@{/param/pojo}" method="post">
用戶名:<input type="text" name="username">
密碼:<input type="password" name="password">
<input type="submit" value="登錄">
</form>
4.6.4、測試效果
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
+++++++++++++++++++++++++分割線+++++++++++++++++++++++++
4.7、解決獲取請求參數的亂碼問題
4.7.1、post請求的亂碼解決方式
註意:SpringMVC中處理編碼的過濾器一定要配置到其他過濾器之前,否則無效;過濾器的順序根據filter-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>
4.7.2、get請求的亂碼解決方式
從tomcat8及以後的版本,get請求的亂碼問題預設已被官方解決;本例展示tomcat7的解決方式。
-
找到並打開編輯該文件:tomcat安裝目錄\conf\server.xml
-
在第一個 Connector 標簽中,添加對應的 UTF 編碼
URIEncoding="UTF-8"