数据绑定是什么?
答:SpringMVC里面,所谓的数据绑定就是将请求带过来的表单数据绑定到执行方法的参数变量.
实际开发中,SpringMVC作为表现层框架,肯定会接受前台页面传递过来的参数,SpringMVC提供了丰富的接受参数的方法
1、原始方式request.getParameter()
–jsp
<!-- 原始方式request.getParameter() -->
<fieldset>
<legend> 原始方式request.getParameter()</legend>
<form action="${pageContext.request.contextPath}/request/method1.do" method="get">
账号: <input name="username"><br>
年龄: <input name="age"><br>
<button type="submit">提交</button>
</form>
</fieldset>
–controller
// 原始方式request.getParameter()
@RequestMapping("/method1")
public ModelAndView method1(HttpServletRequest request, HttpServletResponse resp, HttpSession session) {
String username = request.getParameter("username");
String age = request.getParameter("age");
System.out.println("username :" + username);
System.out.println("age :" + age);
return null;
}
2、方法形参与前台参数同名
--jsp<fieldset>
<legend>方法形参与前台参数同名</legend>
<form action="${pageContext.request.contextPath}/request/method2.do" method="post">
账号: <input name="username"><br>
年龄: <input name="age"><br>
<button type="submit">提交</button>
</form>
</fieldset>
–controller
@RequestMapping("/method2")
public ModelAndView method2(String username, Integer age) {
System.out.println("username1 :" + username);
System.out.println("age1 :" + age);
return null;
}
3、方法形参与前台参数不同名
--jsp<fieldset>
<legend>方法形参与前台参数不同名</legend>
<form action="${pageContext.request.contextPath}/request/method3.do" method="post">
账号: <input name="name"><br>
年龄: <input name="age"><br>
<button type="submit">提交</button>
</form>
</fieldset>
–controller
@RequestMapping("/method3")
public ModelAndView method3(@RequestParam("name") String username, Integer age) {
System.out.println("username1 :" + username);
System.out.println("age1 :" + age);
return null;
}
4、接收数组或集合
--jsp<fieldset>
<legend>接收数组或集合</legend>
<form action="${pageContext.request.contextPath}/request/method4.do" method="post">
账号: <input name="name"><br>
年龄: <input name="age"><br>
爱好: <input type="checkbox" name="hobbys" value="java">java
<input type="checkbox" name="hobbys" value="html">html<br>
<button type="submit">提交</button>
</form>
</fieldset>
–controller
@RequestMapping("/method4")
public ModelAndView method4(@RequestParam("name") String username, Integer age,String[] hobbys) {
System.out.println("username1 :" + username);
System.out.println("age1 :" + age);
System.out.println("hobbys" + Arrays.toString(hobbys));
return null;
}
5、接受对象,表单参数名必须和后台pojo对象对应的属性名相同
--pojopublic class User {
private String username;
private String password;
private String email;
private String phone;
private String[] hobby;
//省略get/set方法
}
–jsp
<fieldset>
<legend>接受对象,表单参数名必须和后台pojo对象对应的属性名相同</legend>
<form action="${pageContext.request.contextPath}/request/method5.do" method="get">
账号: <input name="username"><br>
密码: <input type="password" name="password"><br>
邮箱: <input name="email"><br>
电话: <input name="phone"><br>
爱好:<input type="checkbox" name="hobby" value="java">java
<input type="checkbox" name="hobby" value="C">C
<input type="checkbox" name="hobby" value="C++">C++<br/>
<button type="submit">提交</button>
</form>
</fieldset>
–controller
@RequestMapping("/method5")
public ModelAndView method5(User user) {
System.out.println("user :" + user);
return null;
}
6、接受参数封装成Map集合
Map集合接受参数,自动把表单的参数名称做为key,把表单的值作为value封装到map集合中
缺点 :map集合只能接受单值的数据,因为map的key是唯一的,无法存储key相同,有多个值的数据
–jsp
<fieldset>
<legend>接受参数封装成Map集合</legend>
<form action="${pageContext.request.contextPath}/request/method6.do" method="get">
账号: <input name="username"><br>
密码: <input type="password" name="password"><br>
邮箱: <input name="email"><br>
电话: <input name="phone"><br>
爱好:<input type="checkbox" name="hobby" value="java">java
<input type="checkbox" name="hobby" value="C">C
<input type="checkbox" name="hobby" value="C++">C++<br/>
<button type="submit">提交</button>
</form>
</fieldset>
–controller
@RequestMapping("/method6")
public ModelAndView method6(@RequestParam Map<String, Object> userMap) {
System.out.println("userMap :" + userMap);
return null;
}
7、RESTful风格支持
--修改web.xml。主要修改url-pattern为/。使用restful风格,不可设置为.*do。<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 集成配置SpringMVC -->
<!-- 配置SpringMVC的前端控制器(总控) 让浏览器的所有请求都经过SpringMVC框架 -->
<servlet>
<servlet-name>MVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置读取配置文件的初始化参数 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 初始化容器创建所有对象 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MVC</servlet-name>
<!-- <url-pattern>/</url-pattern> -->
<!-- 实际开发一般都喜欢通配符 * 加上后缀
*.do , *.action (早期Struts2的风格)
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
–controller
@RequestMapping("/detail1/{productId}.html")
public ModelAndView detail1(@PathVariable("productId") Long id) {
System.out.println("productId :" + id);
return null;
}
访问地址则为:localhost:8080/springmvc/request/method7/1231323123.html
1231323123是id的值,你输入的值。
使用restful优势
- 可以让页面伪静态 页面访问感觉像在访问静态html页面,实际上访问时动态页面(伪静态)
2,方便搜索引擎的SEO优化