在以前的javaweb中传递参数我们都是使用 reques.getparameter 来接收参数了,那么在SpringMVC中是如何接收参数的呢?
首先我们还是在jsp页面写一个form表单用于传递参数
此表单的用户名/密码中的name属性中我们看到了两个比较特殊的值,user.uname和list[0].uname 这两个表示 绑定参数的实体对象和封装一个集合。
save.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
<form action="hw/javabean " method="post">
姓名:<input type="text" name="username"/><br/>
密码:<input type="password" 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="提交"/>
</form>
--%>
<form action="hw/javabean " method="post">
姓名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
金额:<input type="text" name="money"/><br/>
用户名:<input type="text" name="list[0].uname"/><br/>
年龄:<input type="text" name="list[0].age"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
Account.java
package com.qst.domain;
import java.io.Serializable;
import java.util.List;
public class Account implements Serializable {
private String username;
private String password;
private Double money;
private List<User> list;
// 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 List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
/* 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 +
", list=" + list +
'}';
}
}
User.java
package com.qst.domain;
import java.io.Serializable;
public class User implements Serializable {
private String uname;
private Integer 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;
}
@Override
public String toString() {
return "User{" +
"uname='" + uname + '\'' +
", age=" + age +
'}';
}
}
HelloController.java
此时我们用来接收参数,不是request.getparameter了 而是直接在方法中带参传递 如:public String sayHello(String username)
如果我们想把参数进行封装,那该怎么办呢,这时我们想到刚才定义的javabean的实体类了。此时我们可以直接把参数 用对象封装起来 ,如:public String javabean(Account account) 是不是方便了许多。
package com.qst.Controller;
import com.qst.domain.Account;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(path="/hw")
public class HelloController {
@RequestMapping(path="/javabean")
public String javabean(Account account){
System.out.println(account);
return "success";
}
}
最后我们在网页输入中文值的时候打印出来会有乱码,我们可以在web.xml中 添加一个过滤器来进行拦截中文乱码
<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>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置过滤器来拦截中文乱码-->
<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>
<!-- 前端控制器配置-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化时先加载springmvc.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 加载servlet的优先级为1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 拦截请求-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>