[Spring MVC] - SpringMVC的各种参数绑定方式
1. 基本数据类型(以int为例,其他类似):
Controller代码:
@RequestMapping("saysth.do")
public void test(int count) {
}
表单代码:
<</span>form action="saysth.do" method="post">
<</span>input name="count" value="10" type="text"/>
......
</</span>form>
表单中input的name值和Controller的参数变量名保持一致,就能完成数据绑定,如果不一致可以使用@RequestParam注解。需要注意的是,如果Controller方法参数中定义的是基本数据类型,但是从页面提交过来的数据为null或者”"的话,会出现数据转换的异常。也就是必须保证表单传递过来的数据不能为null或”",所以,在开发过程中,对可能为空的数据,最好将参数数据类型定义成包装类型,具体参见下面的例子。
2. 包装类型(以Integer为例,其他类似):
Controller代码:
@RequestMapping("saysth.do")
public void test(Integer count) {
}
表单代码:
<</span>form action="saysth.do" method="post">
<</span>input name="count" value="10" type="text"/>
......
</</span>form>
和基本数据类型基本一样,不同之处在于,表单传递过来的数据可以为null或”",以上面代码为例,如果表单中num为”"或者表单中无num这个input,那么,Controller方法参数中的num值则为null。
3. 自定义对象类型:
Model代码:
public class User {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Controller代码:
@RequestMapping("saysth.do")
public void test(User user) {
}
表单代码:
<</span>form action="saysth.do" method="post">
<</span>input name="firstName" value="张" type="text"/>
<</span>input name="lastName" value="三" type="text"/>
......
</</span>form>
非常简单,只需将对象的属性名和input的name值一一匹配即可。
4. 自定义复合对象类型:
Model代码:
public class ContactInfo {
private String tel;
private String address;
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class User {
private String firstName;
private String lastName;
private ContactInfo contactInfo;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public ContactInfo getContactInfo() {
return contactInfo;
}
public void setContactInfo(ContactInfo contactInfo) {
this.contactInfo = contactInfo;
}
}
Controller代码:
@RequestMapping("saysth.do")
public void test(User user) {
System.out.println(user.getFirstName());
System.out.println(user.getLastName());
System.out.println(user.getContactInfo().getTel());
System.out.println(user.getContactInfo().getAddress());
}
表单代码:
<</span>form action="saysth.do" method="post">
<</span>input name="firstName" value="张" /><</span>br>
<</span>input name="lastName" value="三" /><</span>br>
<</span>input name="contactInfo.tel" value="13809908909" /><</span>br>
<</span>input name="contactInfo.address" value="北京海淀" /><</span>br>
<</span>input type="submit" value="Save" />
</</span>form>
User对象中有ContactInfo属性,Controller中的代码和第3点说的一致,但是,在表单代码中,需要使用“属性名(对象类型的属性).属性名”来命名input的name。
5. List绑定:
List需要绑定在对象上,而不能直接写在Controller方法的参数中。
Model代码:
public class User {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
public class UserListForm {
private List users;
public List getUsers() {
return users;
}
public void setUsers(List users) {
this.users = users;
}
}
Controller代码:
@RequestMapping("saysth.do")
public void test(UserListForm userForm) {
for (User user : userForm.getUsers()) {
System.out.println(user.getFirstName() + " - " + user.getLastName());
}
}
表单代码:
<</span>form action="saysth.do" method="post">
<</span>table>
<</span>thead>
<</span>tr>
<</span>th>First Name</</span>th>
<</span>th>Last Name</</span>th>
</</span>tr>
</</span>thead>
<</span>tfoot>
<</span>tr>
<</span>td colspan="2"><</span>input type="submit" value="Save" /></</span>td>
</</span>tr>
</</span>tfoot>
<</span>tbody>
<</span>tr>
<</span>td><</span>input name="users[0].firstName" value="aaa" /></</span>td>
<</span>td><</span>input name="users[0].lastName" value="bbb" /></</span>td>
</</span>tr>
<</span>tr>
<</span>td><</span>input name="users[1].firstName" value="ccc" /></</span>td>
<</span>td><</span>input name="users[1].lastName" value="ddd" /></</span>td>
</</span>tr>
<</span>tr>
<</span>td><</span>input name="users[2].firstName" value="eee" /></</span>td>
<</span>td><</span>input name="users[2].lastName" value="fff" /></</span>td>
</</span>tr>
</</span>tbody>
</</span>table>
</</span>form>
其实,这和第4点User对象中的contantInfo数据的绑定有点类似,但是这里的UserListForm对象里面的属性被定义成List,而不是普通自定义对象。所以,在表单中需要指定List的下标。值得一提的是,Spring会创建一个以最大下标值为size的List对象,所以,如果表单中有动态添加行、删除行的情况,就需要特别注意,譬如一个表格,用户在使用过程中经过多次删除行、增加行的操作之后,下标值就会与实际大小不一致,这时候,List中的对象,只有在表单中对应有下标的那些才会有值,否则会为null,看个例子:
表单代码:
<</span>form action="saysth.do" method="post">
<</span>table>
<</span>thead>
<</span>tr>
<</span>th>First Name</</span>th>
<</span>th>Last Name</</span>th>
</</span>tr>
</</span>thead>
<</span>tfoot>
<</span>tr>
<</span>td colspan="2"><</span>input type="submit" value="Save" /></</span>td>
</</span>tr>
</</span>tfoot>
<</span>tbody>
<</span>tr>
<</span>td><</span>input name="users[0].firstName" value="aaa" /></</span>td>
<</span>td><</span>input name="users[0].lastName" value="bbb" /></</span>td>
</</span>tr>
<</span>tr>
<</span>td><</span>input name="users[1].firstName" value="ccc" /></</span>td>
<</span>td><</span>input name="users[1].lastName" value="ddd" /></</span>td>
</</span>tr>
<</span>tr>
<</span>td><</span>input name="users[20].firstName" value="eee" /></</span>td>
<</span>td><</span>input name="users[20].lastName" value="fff" /></</span>td>
</</span>tr>
</</span>tbody>
</</span>table>
</</span>form>
这个时候,Controller中的userForm.getUsers()获取到List的size为21,而且这21个User对象都不会为null,但是,第2到第19的User对象中的firstName和lastName都为null。打印结果:
aaa - bbb
ccc - ddd
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
null - null
eee - fff
6. Set绑定:
Set和List类似,也需要绑定在对象上,而不能直接写在Controller方法的参数中。但是,绑定Set数据时,必须先在Set对象中add相应的数量的模型对象。
Model代码:
public class User {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
public class UserSetForm {
private Set users = new HashSet();
public UserSetForm() {
users.add(new User());
users.add(new User());
users.add(new User());
}
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
}
Controller代码:
@RequestMapping("saysth.do")
public void test(UserSetForm userForm) {
for (User user : userForm.getUsers()) {
System.out.println(user.getFirstName() + " - " + user.getLastName());
}
}
表单代码:
<</span>form action="saysth.do" method="post">
<</span>table>
<</span>thead>
<</span>tr>
<</span>th>First Name</</span>th>
<</span>th>Last Name</</span>th>
</</span>tr>
</</span>thead>
<</span>tfoot>
<</span>tr>
<</span>td colspan="2"><</span>input type="submit" value="Save" /></</span>td>
</</span>tr>
</</span>tfoot>
<</span>tbody>
<</span>tr>
<</span>td><</span>input name="users[0].firstName" value="aaa" /></</span>td>
<</span>td><</span>input name="users[0].lastName" value="bbb" /></</span>td>
</</span>tr>
<</span>tr>
<</span>td><</span>input name="users[1].firstName" value="ccc" /></</span>td>
<</span>td><</span>input name="users[1].lastName" value="ddd" /></</span>td>
</</span>tr>
<</span>tr>
<</span>td><</span>input name="users[2].firstName" value="eee" /></</span>td>
<</span>td><</span>input name="users[2].lastName" value="fff" /></</span>td>
</</span>tr>
</</span>tbody>
</</span>table>
</</span>form>
基本和List绑定类似。
需要特别提醒的是,如果最大下标值大于Set的size,则会抛出org.springframework.beans.InvalidPropertyException异常。所以,在使用时有些不便。
7. Map绑定:
Map最为灵活,它也需要绑定在对象上,而不能直接写在Controller方法的参数中。
Model代码:
public class User {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
public class UserMapForm {
private Map users;
public Map getUsers() {
return users;
}
public void setUsers(Map users) {
this.users = users;
}
}
Controller代码:
@RequestMapping("saysth.do")
public void test(UserMapForm userForm) {
for (Map.Entry entry : userForm.getUsers().entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue().getFirstName() + " - " +
entry.getValue().getLastName());
}
}
表单代码:
<</span>form action="saysth.do" method="post">
<</span>table>
<</span>thead>
<</span>tr>
<</span>th>First Name</</span>th>
<</span>th>Last Name</</span>th>
</</span>tr>
</</span>thead>
<</span>tfoot>
<</span>tr>
<</span>td colspan="2"><</span>input type="submit" value="Save" /></</span>td>
</</span>tr>
</</span>tfoot>
<</span>tbody>
<</span>tr>
<</span>td><</span>input name="users['x'].firstName" value="aaa" /></</span>td>
<</span>td><</span>input name="users['x'].lastName" value="bbb" /></</span>td>
</</span>tr>
<</span>tr>
<</span>td><</span>input name="users['y'].firstName" value="ccc" /></</span>td>
<</span>td><</span>input name="users['y'].lastName" value="ddd" /></</span>td>
</</span>tr>
<</span>tr>
<</span>td><</span>input name="users['z'].firstName" value="eee" /></</span>td>
<</span>td><</span>input name="users['z'].lastName" value="fff" /></</span>td>
</</span>tr>
</</span>tbody>
</</span>table>
</</span>form>
打印结果:
x: aaa - bbb
y: ccc - ddd
z: eee - fff
总述一:SpringMVC页面传入值,一共有三种方式:
request,方法参数,对象;
//其实有篇思路比较清晰的博文,建议大家读一下:SpringMVC——接收请求参数和页面传参
首先配置和搭建项目,在web.xml中配置控制器,配置容器中打开注解扫描和注解的驱动。
[html] view plain copy
1. <</span>span style="font-size:18px;">
2. <</span>context:component-scan base-package="com.throne"></</span>context:component-scan>
3.
4. <</span>mvc:annotation-driven></</span>mvc:annotation-driven>
5.
6.
7.
8.
9. <</span>bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
10. <</span>property name="prefix" value="/WEB-INF/"></</span>property>
11. <</span>property name="suffix" value=".jsp"></</span>property>
12. </</span>bean>
13. </</span>beans></</span>span>
在这个之后,也就不用在增加其他内容了,相当方便。
1.使用request
java类:
[java] view plain copy
1. public String login3(HttpServletRequest req){
2. String id=req.getParameter("id");
3. String name=req.getParameter("name");
4. System.out.println(id);
5. System.out.println(name);
6. return "Hello";
7. }
public String login3(HttpServletRequest req){
String id=req.getParameter("id");
String name=req.getParameter("name");
System.out.println(id);
System.out.println(name);
return "Hello";
}
String id=req.getParameter("id");String name=req.getParameter("name");// 打印一下System.out.println(id);System.out.println(name);return "Hello";}
2 使用方法参数接受参数,注意如果方法参数名字和传入的参数名字一致,则正确用,如果不一致,则使用@RequestParam这个注入标签
[java] view plain copy
1. "font-size:18px;">@RequestMapping("/login.do")
2.
3. public String login1(String id,@RequestParam("name") String na){
4. System.out.println(id);
5. System.out.println(na);
6. return "Hello";
7. //因为hyml中的id,name与此参数相同所以,spring会直接自动注入,
8. }
@RequestMapping("/login.do")
public String login1(String id,@RequestParam("name") String na){
System.out.println(id);
System.out.println(na);
return "Hello";
//因为hyml中的id,name与此参数相同所以,spring会直接自动注入,
}
3.使用对象接受参数 ,注意 实体类中的属性名和页面的name值要一样。
[java] view plain copy
- "font-size:18px;"> @RequestMapping("/login.do")
- public String addUser5(@ModelAttribute Student s) {
- System.out.println("userName is:"+s.getId());
- System.out.println("password is:"+s.getName());
- return "Hello";
- } "code" class="java">@Component
- public class Student implements Serializable{
- private String id;
- private String name;
- public Student() {
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
@RequestMapping("/login.do")
public String addUser5(@ModelAttribute Student s) {
System.out.println("userName is:"+s.getId());
System.out.println("password is:"+s.getName());
return "Hello";
}
@Component
public class Student implements Serializable{
private String id;
private String name;
public Student() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
SpringMVC向页面传出值
注意:在调试的时候,有些浏览器会有缓存,导致在测试其他传值的时候,无法预计效果,此时需要清理浏览器缓存。
[java] view plain copy
1. "font-size:18px;">//第一种 往外传值方式
2.
3. public ModelAndView login5(String id, String demo){
4. Map map=new HashMap();
5. //建议使用map
6. map.put("idsss", id);
7. map.put("demosss", demo);
8. return new ModelAndView("Hello1", map);
9. }
10.
11. //第二种 往外传值方式
12. @RequestMapping("/login.do")
13. public String login6(String id, String name,ModelMap model){
14. model.addAttribute("id", id);
15. model.addAttribute("name", name);
16. return "Hello1";
17. }
18.
19. //第三种往外传值方式 @ModelAttribute
20.
21. public String login8(@ModelAttribute Student s){
22. System.out.println(s.getId());
23. System.out.println(s.getName());
24. return "Hello1";
25. }
26. //第四种往外传值方式 @ModelAttribute
27.
28.
29. public String login9(HttpServletRequest req){
30. String id=req.getParameter("id");
31. String name=req.getParameter("name");
32. req.setAttribute("idsdfasdf", id);
33. req.setAttribute("sdfsad", name);
34. //idsdfasdf与sdfsad名字随意
35. return "Hello1";
36. }
37.
38. //第五种往外传值方式 @ModelAttribute
39. // 得到session的方式 有两种
40.
41. public String login10(String id ,String name,HttpSession session){
42. session.setAttribute("idsdfasdf", id);
43. session.setAttribute("sdfsad", name);
44. return "Hello1";
45. }
springmvc请求参数获取的几种方法
1、直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交。
@RequestMapping("/addUser1")
public String addUser1(String username,String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
url形式:http://localhost/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 提交的参数需要和Controller方法中的入参名称一致。
2、通过HttpServletRequest接收,post方式和get方式都可以。
@RequestMapping("/addUser2")
public String addUser2(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
3、通过一个bean来接收,post方式和get方式都可以。
(1)建立一个和表单中参数对应的bean
package demo.model;
public class UserModel {
private String username;
private String password;
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;
}
}
(2)用这个bean来封装接收的参数
@RequestMapping("/addUser3")
public String addUser3(UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "demo/index";
}
4、通过@PathVariable获取路径中的参数
@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
public String addUser4(@PathVariable String username,@PathVariable String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
例如,访问http://localhost/SSMDemo/demo/addUser4/lixiaoxi/111111 路径时,则自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=lixiaoxi、password=111111。
5、使用@ModelAttribute注解获取POST请求的FORM表单数据
Jsp表单如下:
用户名:
密 码:
Java Controller如下:
@RequestMapping(value="/addUser5",method=RequestMethod.POST)
public String addUser5(@ModelAttribute("user") UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "demo/index";
}6、用注
解@RequestParam绑定请求参数到方法入参
当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false)
@RequestMapping(value="/addUser6",method=RequestMethod.GET)
public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
Spring接收请求参数:
1,使用HttpServletRequest获取
Java代码
@RequestMapping("/login.do")
public String login(HttpServletRequest request){
String name = request.getParameter("name")
String pass = request.getParameter("pass")
}
2,Spring会自动将表单参数注入到方法参数,和表单的name属性保持一致。和Struts2一样
Java代码
@RequestMapping("/login.do")
public String login(HttpServletRequest request,
String name,
@RequestParam("pass")String password) // 表单属性是pass,用变量password接收
{
syso(name);
syso(password)
}
3,自动注入Bean属性
Java代码 //封装的User类
public class User{
private String name;
private String pass;
}
Java代码
@RequestMapping("/login.do")
public String login(User user)
{
syso(user.getName());
syso(user.getPass());
}
向页面传值:
当Controller组件处理后,向jsp页面传值,
1,使用HttpServletRequest 和 Session 然后setAttribute(),就和Servlet中一样
2,使用ModelAndView对象
3,使用ModelMap对象
4,使用@ModelAttribute注解
Model数据会利用HttpServletRequest的Attribute传值到success.jsp中
Java代码
@RequestMapping("/login.do")
public ModelAndView login(String name,String pass){
User user = userService.login(name,pwd);
Map data = new HashMap();
data.put("user",user);
return new ModelAndView("success",data);
}
使用ModelMap参数对象示例:
ModelMap数据会利用HttpServletRequest的Attribute传值到success.jsp中
Java代码
@RequestMapping("/login.do")
public String login(String name,String pass ,ModelMap model){
User user = userService.login(name,pwd);
model.addAttribute("user",user);
model.put("name",name);
return "success";
}
使用@ModelAttribute示例
在Controller方法的参数部分或Bean属性方法上使用
@ModelAttribute数据会利用HttpServletRequest的Attribute传值到success.jsp中
Java代码
@RequestMapping("/login.do")
public String login(@ModelAttribute("user") User user){
//TODO
return "success";
}
@ModelAttribute("name")
public String getName(){
return name;
}
Session存储:
可以利用HttpServletReequest的getSession()方法
Java代码
@RequestMapping("/login.do")
public String login(String name,String pwd
ModelMap model,HttpServletRequest request){
User user = serService.login(name,pwd);
HttpSession session = request.getSession();
session.setAttribute("user",user);
model.addAttribute("user",user);
return "success";
}
Spring MVC 默认采用的是转发来定位视图,如果要使用重定向,可以如下操作
1,使用RedirectView
2,使用redirect:前缀
Java代码
public ModelAndView login(){
RedirectView view = new RedirectView("regirst.do");
return new ModelAndView(view);
}
或者用如下方法,工作中常用的方法:
Java代码
public String login(){
//TODO
return "redirect:regirst.do";
}