1.控制类
@RequestMapping("testServletAPI")
//形参request表示当前请求
public String testServletAPI(HttpServletRequest request)
{
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username: "+username+" password: "+password);
return "test_param";
}
2.test_param.html
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>测试请求参数h1>
<a th:href="@{/testServletAPI(username ='admin',password=123456)}">测试请求a>
body>
html>
1.1.控制器
形参要和请求参数名字相同,这样就可以自动匹配。
@RequestMapping("testCon")
public String testControllerParam(String username,String password)
{
System.out.println("username: "+username+" password: "+password);
return "test_param";
}
通过get提交方式发现重名的参数会以这种方式传送。
1.3.1.用字符串类型接收
@RequestMapping("testCon")
public String testControllerParam(String username,String password,String hobby)
{
System.out.println("username: "+username+" password: "+password+" hobby "+hobby);
return "test_param";
}
当用字符串类型接收时会用,隔开。
1.3.2.用字符串数组接收
@RequestMapping("testCon")
public String testControllerParam(String username,String password,String[] hobby)
{
System.out.println("username: "+username+" password: "+password+" hobby "+ Arrays.toString(hobby));
return "test_param";
}
@RequestMapping("testCon")
public String testControllerParam(
@RequestParam(value = "username") String username, String password, String[] hobby)
{
System.out.println("username: "+username+" password: "+password+" hobby "+ Arrays.toString(hobby));
return "test_param";
}
@RequestMapping("testCon")
public String testControllerParam(@RequestHeader("host") String host)
{
System.out.println("host "+host);
return "test_param";
}
同样具有三个属性
@RequestMapping("testCon")
public String testControllerParam(@CookieValue(value = "j")String cookie)
{
return "test_param";
}
同样具有三个属性
5.1.实体类
package com.mvc.bean;
public class User {
private Integer id;
private String username;
private String password;
private String sex;
private String email;
public User(Integer id, String username, String password, String sex, String email) {
this.id = id;
this.username = username;
this.password = password;
this.sex = sex;
this.email = email;
}
public User() {
}
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
'}';
}
}
5.2.表单
<form th:action="@{/testBean}" method="post">
用户名<input name="username" type="text"><br>
密码<input type="password" name="password"> <br>
性别:<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女 <br>
邮箱<input type="text" name="email"> <br>
<input type="submit" value="通过实体类对象获取">
form>
5.3.控制类方法
@RequestMapping("testBean")
public String testBean(User user)
{
System.out.println(user);
return "index";
}
5.4控制台输出
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceResponseEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>