SpringMVC 请求参数的绑定
入门
/*param.jsp*/
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="param/testParam?username=user&password=123456">请求参数绑定</a>
</body>
</html>
@Controller
@RequestMapping("param")
public class ParamController {
@RequestMapping("testParam")
public String testParam(String username,String password){
System.out.println("执行了");
System.out.println(username);
System.out.println(password);
return "success";
}
}
请求参数绑定实体类型
package com.wl.domain;
import java.io.Serializable;
//创建实体类
public class User implements Serializable {
public String uname;
public Integer age;
@Override
public String toString() {
return "User{" +
"uname='" + uname + '\'' +
", age=" + age +
'}';
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.wl.domain;
import java.io.Serializable;
//创建Account实体类,并引用user实体类
public class Account implements Serializable {
private String username;
private String password;
private Double money;
private User user;
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 Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "Account{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", money=" + money +
", user=" + user +
'}';
}
}
//请求参数绑定把数据封装到JavaBean的类中
@RequestMapping("/saveAccount")
public String saveAccount(Account account) {
System.out.println("执行了...");
System.out.println(account);
return "success";
}
<%--使用提交表单的方式提交信息--%>
<form action="param/saveAccount" method="post">
姓名:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
金额:<input type="text" name="money"><br>
用户姓名:<input type="text" name="user.uname"><br>
用户年龄:<input type="text" name="user.age"><br>
<input type="submit" value="提交"><br>
</form>
在这里如果你输入中文就会发现控制台输出乱码,这时我们就要配置解决中文乱码的过滤器