javascript
springmvc 学习系列 (4) 之 数据绑定 -尊龙游戏旗舰厅官网
2019独角兽企业重金招聘python工程师标准>>>
在系列(3)中我们介绍了请求是如何映射到一个action上的,下一步当然是如何获取到请求中的数据,这就引出了本篇所要讲的内容—数据绑定。
首先看一下都有哪些绑定数据的注解:
1.@requestparam,绑定单个请求数据,可以是url中的数据,表单提交的数据或上传的文件;
2.@pathvariable,绑定url模板变量值;
3.@cookievalue,绑定cookie数据;
4.@requestheader,绑定请求头数据;
5.@modelattribute,绑定数据到model;
6.@sessionattributes,绑定数据到session;
7.@requestbody,用来处理content-type不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等;
8.@requestpart,绑定“multipart/data”数据,并可以根据数据类型进项对象转换;
下面我们来看如何使用:
1.@requestparam:
为了验证文件绑定我们需要先做以下工作:
a.把commons-fileupload-1.3.1.jar和commons-io-2.4.jar两个jar包添加到我们项目。
b.配置我们项目中的springservlet-config.xml文件使之支持文件上传,内容如下:
<bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <property name="maxuploadsize"> <value>1048576value> property><property name="defaultencoding"> <value>utf-8value> property> bean>其中maxuploadsize用于限制上传文件的最大大小,也可以不做设置,这样就代表上传文件的大小木有限制。defaultencoding用于设置上传文件的编码格式,用于解决上传的文件中文名乱码问题。
下面就看具体如何使用:
添加一个databindcontroller,里面有2个parambind的action分别对应get和post请求:
package com.demo.web.controllers;import javax.servlet.http.httpservletrequest; import org.springframework.stereotype.controller; import org.springframework.web.bind.servletrequestutils; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.multipart.multipartfile; import org.springframework.web.multipart.multiparthttpservletrequest; import org.springframework.web.servlet.modelandview;@controller @requestmapping(value = "/databind") public class databindcontroller {@requestmapping(value="/parambind", method = {requestmethod.get})public modelandview parambind(){modelandview modelandview = new modelandview(); modelandview.setviewname("parambind"); return modelandview;}@requestmapping(value="/parambind", method = {requestmethod.post})public modelandview parambind(httpservletrequest request, @requestparam("urlparam") string urlparam, @requestparam("formparam") string formparam, @requestparam("formfile") multipartfile formfile){//如果不用注解自动绑定,我们还可以像下面一样手动获取数据string urlparam1 = servletrequestutils.getstringparameter(request, "urlparam", null);string formparam1 = servletrequestutils.getstringparameter(request, "formparam", null);multipartfile formfile1 = ((multiparthttpservletrequest) request).getfile("formfile"); modelandview modelandview = new modelandview(); modelandview.addobject("urlparam", urlparam); modelandview.addobject("formparam", formparam); modelandview.addobject("formfilename", formfile.getoriginalfilename()); modelandview.addobject("urlparam1", urlparam1); modelandview.addobject("formparam1", formparam1); modelandview.addobject("formfilename1", formfile1.getoriginalfilename()); modelandview.setviewname("parambindresult"); return modelandview;}}在views文件夹中添加parambind.jsp和parambindresult.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>insert title heretitle> head> <body><form action="parambind?urlparam=aaa" method="post" enctype="multipart/form-data"> <input type="text" name="formparam" /><br/> <input type="file" name="formfile" /><br/><input type="submit" value="submit" />form> body> html> <%@ 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>insert title heretitle> head> <body>自动绑定数据:<br/><br/>${urlparam}<br/>${formparam}<br/>${formfilename}<br/><br/><br/><br/>手动获取数据:<br/><br/>${urlparam1}<br/>${formparam1}<br/>${formfilename1}<br/> body> html>运行项目,输入内容,选择上传文件:
提交查看结果:
可以看到绑定的数据已经获取到了。
上面我们演示了如何把数据绑定到单个变量,但在实际应用中我们通常需要获取的是model对象,别担心,我们不需要把数据绑定到一个个变量然后在对model赋值,只需要把model加入相应的action参数(这里不需要指定绑定数据的注解)spring mvc会自动进行数据转换并绑定到model对象上,一切就是这么简单。测试如下:
添加一个accountmodel类作为测试的model:
package com.demo.web.models;public class accountmodel {private string username;private string password;public void setusername(string username){this.username=username;}public void setpassword(string password){this.password=password;}public string getusername(){return this.username;}public string getpassword(){return this.password;} }在databindcontroller里面添加2个modelautobind的action分别对应get和post请求:
@requestmapping(value="/modelautobind", method = {requestmethod.get}) public string modelautobind(httpservletrequest request, model model){model.addattribute("accountmodel", new accountmodel());return "modelautobind"; }@requestmapping(value="/modelautobind", method = {requestmethod.post}) public string modelautobind(httpservletrequest request, model model, accountmodel accountmodel){model.addattribute("accountmodel", accountmodel);return "modelautobindresult"; }在views文件夹中添加modelautobind.jsp和modelautobindresult.jsp 2个视图用于提交数据和展示提交的数据:
modelautobind.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"><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %><html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title heretitle> head> <body><form:form modelattribute="accountmodel" method="post"> 用户名:<form:input path="username"/><br/>密 码:<form:password path="password"/><br/><input type="submit" value="submit" />form:form> body> html>modelautobindresult.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>insert title heretitle> head> <body>用户名:${accountmodel.username}<br/>密 码:${accountmodel.password} body> html>运行测试:
用户名 输入aaa 密码 输入bbb,提交:
可以看到结果显示正确,说明自动绑定成功。
注:
1.关于@requestparam的参数,这是一个@requestparam的完整写法@requestparam(value="username", required=true, defaultvalue="aaa")。
value表示要绑定请求中参数的名字;
required表示请求中是否必须有这个参数,默认为true这是如果请求中没有要绑定的参数则返回404;
defaultvalue表示如果请求中指定的参数值为空时的默认值;
要绑定的参数如果是值类型必须要有值否则抛异常,如果是引用类型则默认为null(boolean除外,默认为false);
2.在刚才添加的2个action中可以看到返回类型和以前的不一样了由modelandview变成了string,这是由于spring mvc 提供model、modelmap、map让我们可以直接添加渲染视图需要的模型数据,在返回时直接指定对应视图名称就可以了。同时map是继承于modelmap的,而model和modelmap是继承于extendedmodelmap的。
3.在刚才添加的视图modelautobind.jsp中可以看到<form:form、<form:input 等标签,这是spring mvc提供的表单标签,借助于这些标签我们可以很方便的把模型数据绑定到表单上面(当然你也可以选择继续使用原生的html表单标签),要使用spring mvc只要在视图中添加引用 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>即可,关于spring mvc表单标签的具体内容会在以后的文章中作介绍。
转载于:https://my.oschina.net/gaoguofan/blog/753237
总结
以上是尊龙游戏旗舰厅官网为你收集整理的springmvc 学习系列 (4) 之 数据绑定 -1的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: httpclient 讲解 (2) 项目
- 下一篇: c *.h和*.cpp在编译中的作用