SpringMVC数据绑定
---------------------siwuxie095
SpringMVC 数据绑定
获取用户提交的参数,将之绑定到方法参数上,就叫数据绑定
(一)Servlet 内置对象的绑定
在 Controller 类中获取 Servlet 的内置对象(Request、Response、
Session)非常简单:需要什么就定义什么
对应的是:HttpServletRequest、HttpServletResponse、HttpSession
例如:
@RequestMapping("/hello") public ModelAndView hello(HttpServletRequest request) { // 创建 ModelAndView 对象,并设置视图名称 ModelAndView mv = new ModelAndView("hello"); // 添加模型数据 mv.addObject("msg", request.toString()); return mv; } |
(二)POJO 对象的绑定
SpringMVC 会将请求参数名和 POJO 实体中的属性名进行匹配,
如果名称一致,则把值填充到对象中
例如:
编写一个实体类
User.java:
package com.siwuxie095.entity;
public class User {
private Integer userId; private String userName; private String userSex; private String userAge;
public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; }
public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }
public String getUserSex() { return userSex; } public void setUserSex(String userSex) { this.userSex = userSex; }
public String getUserAge() { return userAge; } public void setUserAge(String userAge) { this.userAge = userAge; }
@Override public String toString() { return "User [userId=" + userId + ", userName=" + userName + ", userSex=" + userSex + ", userAge=" + userAge + "]"; }
} |
编写一个 Controller 类
UserController.java:
package com.siwuxie095.controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;
import com.siwuxie095.entity.User;
@Controller public class UserController {
@RequestMapping("/show") public ModelAndView show(User user) { // 创建 ModelAndView 对象,并设置视图名称 ModelAndView mv = new ModelAndView("show"); // 添加模型数据 mv.addObject("msg", user); return mv; }
} |
在访问时,只需要在 URL 后加上与实体类属性匹配的请求参数即可
如(请求参数可不全写出来):
http://localhost:8080/工程名/show.do?userId=1&userName=siwuxie095
(三)基本数据类型的绑定
SpringMVC 对 Java 基本数据类型的转换,有很多支持,基本能满足
日常开发需求
默认支持的基本数据类型在 org.springframework.beans.Property
EditorRegistrySuport 中定义
通过 @RequestParam 注解即可完成基本数据类型的绑定
例如:
编写一个 JSP 页面
test.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>test</title> </head> <body>
<%-- <%=request.getContextPath()%> 等同于 ${pageContext.request.contextPath} --%> <form action="<%=request.getContextPath()%>/test.do" method="post"> 年龄:<input type="text" name="userAge"/><br/> 爱好:<input type="checkbox" name="interestArr" value="唱歌"/>唱歌 <input type="checkbox" name="interestArr" value="书法"/>书法 <input type="checkbox" name="interestArr" value="打篮球"/>打篮球<br/> <input type="submit" value="提交"/> </form>
</body> </html> |
编写一个 Controller 类
TestController.java:
package com.siwuxie095.controller;
import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus;
@Controller public class TestController {
/** * 这里获取用户提交的年龄和爱好 * * 注意:这里没有返回值,仅仅使用 @ResponseStatus 注解, * 返回了一个 HTTP 状态码 200 * * 也可以写成 @ResponseStatus(value=HttpStatus.OK) */ @RequestMapping("/test") @ResponseStatus(HttpStatus.OK) public void test(@RequestParam("userAge") Integer userAge, @RequestParam("interestArr") String[] interestArr) { System.out.println("年龄:" + userAge); System.out.println("爱好:"); for (String interest : interestArr) { System.out.print(interest + " "); } }
} |
访问路径:
http://localhost:8080/工程名/test.jsp
(四)List 集合的绑定
List 集合的绑定需要将 List 集合对象包装到一个类中才能绑定
Map 集合、Set 集合与之类似
例如:
编写一个实体类
User.java:
package com.siwuxie095.entity;
public class User {
private Integer userId; private String userName; private String userSex; private String userAge;
public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; }
public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }
public String getUserSex() { return userSex; } public void setUserSex(String userSex) { this.userSex = userSex; }
public String getUserAge() { return userAge; } public void setUserAge(String userAge) { this.userAge = userAge; }
@Override public String toString() { return "User [userId=" + userId + ", userName=" + userName + ", userSex=" + userSex + ", userAge=" + userAge + "]"; }
} |
将 List 集合对象包装到一个类中
UserListForm.java:
package com.siwuxie095.entity;
import java.util.List;
public class UserListForm {
private List<User> userList;
public List<User> getUserList() { return userList; }
public void setUserList(List<User> userList) { this.userList = userList; }
@Override public String toString() { return "UserListForm [userList=" + userList + "]"; }
} |
编写一个 JSP 页面
list.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>list</title> </head> <body>
<%-- <%=request.getContextPath()%> 等同于 ${pageContext.request.contextPath} --%> <form action="<%=request.getContextPath()%>/list.do" method="post"> 用户1:<input type="text" name="userList[0].userId" value="1"/> <input type="text" name="userList[0].userName" value="ZhangSan"/><br/> 用户2:<input type="text" name="userList[1].userId" value="2"/> <input type="text" name="userList[1].userName" value="LiSi"/><br/> 用户3:<input type="text" name="userList[2].userId" value="3"/> <input type="text" name="userList[2].userName" value="WangWu"/><br/> <input type="submit" value="提交"/> </form>
</body> </html> |
编写一个 Controller 类
UserController.java:
package com.siwuxie095.controller;
import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus;
import com.siwuxie095.entity.UserListForm;
@Controller public class UserController {
/** * 注意:这里没有返回值,仅仅使用 @ResponseStatus * 注解,返回了一个 HTTP 状态码 200 */ @RequestMapping("/list") @ResponseStatus(HttpStatus.OK) public void listUser(UserListForm userListForm) { System.out.println(userListForm); }
} |
访问路径:
http://localhost:8080/工程名/list.jsp
参考链接:
【made by siwuxie095】
posted on 2018-03-01 11:30 siwuxie095 阅读(199) 评论(0) 编辑 收藏 举报