SpringMVC框架 (6) —— 数据绑定

数据绑定

数据绑定是什么

SpringMVC里面,所谓的数据绑定就是将请求带过来的表单数据绑定到执行方法的参数变量.

实际开发中,SpringMVC作为表现层框架,肯定会接受前台页面传递过来的参数,SpringMVC提供了丰富的接受参数的方法

原始方式request.getParameter() 了解

SpringMVC可以注入HttpServletRequest对象,直接使用getParameter参数接受

<!--  原始方式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>

@RequestMapping(value="/method1",method=RequestMethod.POST) //资源名称 
public void method1(HttpServletRequest req,HttpServletResponse resp,HttpSession session) {
		 //原始方式request.getParameter()
	String username = req.getParameter("username");
	String age = req.getParameter("age");
	System.out.println(username);
	System.out.println(age);
}

方法形参与前台参数同名

<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>

//方法形参与前台参数同名
@RequestMapping(value="/method2",method=RequestMethod.POST) 
public ModelAndView method2(String username,String age) {
	System.out.println(username);
	System.out.println(age);
	return null;
}

方法形参与前台参数不同名

<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>

// 方法形参与前台参数不同同名
// 解决方案使用 : @RequestParam("前台表单对应的名")
@RequestMapping(value = "/method3", method = RequestMethod.POST)
public ModelAndView method3(@RequestParam("name") String username, String age) {
	System.out.println(username);
	System.out.println(age);
	return null;
}

接受数组

<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>

// 接受数组
	@RequestMapping(value = "/method4", method = RequestMethod.POST)
	public ModelAndView method4(String[] hobbys) {
		System.out.println(Arrays.toString(hobbys));
		return null;
	}

对象传参

后台并不能直接接受集合参数,需要将集合设置到对应的JavaBean中,通过JavaBean接受集合参数

Pojo对象

public class User {
	private String username;
	private String password;
	private String email;
	private String phone;
	private String[] hobby;
}

<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>

// 对象传参->对象中有集合
	@RequestMapping(value = "/method5", method = RequestMethod.POST)
	public ModelAndView method4(User user) {
		System.out.println(user);
		return null;
	}

接受参数封装成Map集合

Map集合接受参数,自动把表单的参数名称做为key,把表单的值作为value封装到map集合中
缺点 :map集合只能接受单值的数据,因为map的key是唯一的,无法存储key相同,有多个值的数据

<fieldset>
	<legend>接受参数封装成Map集合</legend>
	<form action="${pageContext.request.contextPath}/request/method6.do" method="post">
		账号: <input name="username"><br>
		密码: <input name="password"><br>
		邮箱: <input name="email"><br>
		电话: <input name="phone"><br>
          <button type="submit">提交</button>
	</form>
</fieldset>

// 接受参数封装成Map集合
	@RequestMapping(value = "/method6", method = RequestMethod.POST)
	public ModelAndView method6(@RequestParam Map<String,Object> map) {
		System.out.println("map:"+map);
		return null;
	}

RESTful风格支持

RESTFUL 风格介绍

REST(英文:Representational State Transfer,简称REST)描述了一个架构样式的网络系统,比如 web 应用程序。它首次出现在 2000 年 Roy Fielding 的博士论文中,他是 HTTP 规范的主要编写者之一。在目前主流的三种Web服务交互方案中,REST相比于SOAP(Simple Object Access protocol,简单对象访问协议)以及XML-RPC更加简单明了,REST都倾向于用更加简单轻量的方法设计和实现。值得注意的是REST并没有一个明确的标准,而更像是一种设计的风格。

RESTful一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

参数传递方法 GET

例如:根据商品id查询对应的商品信息(京东网站)

如果按照我们web开发应该是将商品id通过get方法跟在地址后面

普通方式 https://item.jd.com?product_id=100000287117 京东不支持

但是京东不是使用的此种方式,使用的 RESTFUL风格

RESTful风格 : https://item.jd.com/100000287117.html

案例代码

// RESTful风格
@RequestMapping(value = "/method7/{product_id}")
public ModelAndView method7(@PathVariable("product_id") Integer product_id) {
	System.out.println(product_id);//1231323123
	return null;
}

浏览器访问地址

localhost:8080/springmvc/request/method7/1231323123.html

使用RESTful优势

  1. 可以让页面伪静态
    页面访问感觉像在访问静态html页面,实际上访问时动态页面(伪静态)
  2. 方便搜索引擎的SEO优化

请求中文乱码问题

SpringMVC默认接受的参数是ISO-8859-1编码参数,单字节,不支持中文,Spring提供了一个过滤器可以让开发者自定义请求参数的字符编码

注意:此种方式只对post提交方式有效

	<!-- 统一配置SpringMVC接受请求参数的编码 -->
	<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>

如果提交中文参数的提价是get方式 -tomcat7以及以前需要设置
配置 tomcat/conf/server.xml 添加 URIEncoding=“UTF-8”

 <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

建议:以后表单一般使用post,get方式请求尽量不要使用中文作为参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值