[TOC]html
前面Struts博文基本把Struts的配置信息讲解完了.....本博文主要讲解Struts对数据的处理java
在第一次咱们写开发步骤的时候,咱们写的Action是继承着ActionSupport类的...为啥咱们继承了ActionSupport类呢?下面我就会讲解到web
咱们来看一下ActionSupport干了什么:浏览器
也就是说,若是咱们在Action类中须要用到Struts为咱们提供的数据校验等Struts已经帮咱们实现的功能,咱们就继承着ActionSupport类..服务器
咱们再来看看Action接口干了什么:微信
固然啦,ActionSuppot也继承着Action接口,因此ActionSuppot拥有Action接口的所有功能....所以,这种开发方式咱们是比较少用的...session
开发此类的Action,它是不继承任何类、不实现任何接口的...也就是说,它就是一个普通的Java类....app
public class PrivilegeAction {
public String login() {
System.out.println("我是普通的javaAction,不继承任何的类、不实现任何的接口");
return "success";
}
}
复制代码
<struts>
<package name="privilige" extends="struts-default">
<action name="login" class="privilegeaction.PrivilegeAction" method="login">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
复制代码
通常地,咱们使用Servlet的时候都是分为几个步骤的:框架
如今问题来了,咱们本身编写的Action类是没有request、response、Session、application之类的对象的....咱们是怎么获得web层的数据、再将数据存到域对象中的呢??jsp
前面已经说过了,Struts预先帮咱们完成了对数据封装的功能,它是经过params拦截器来实现数据封装的
<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
复制代码
首先,咱们填写表单页面的数据,请求Action处理数据
<form action="${pageContext.request.contextPath}/date01" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="text" name="psd"><br>
年龄:<input type="text" name="age"><br>
生日:<input type="text" name="birthday"><br>
<input type="submit" value="注册"><br>
</form>
复制代码
在Action设置与JSP页面相同的属性,并为它们编写setter方法
private String username;
private String psd;
private int age;
private Date birthday;
public void setUsername(String username) {
this.username = username;
}
public void setPsd(String psd) {
this.psd = psd;
}
public void setAge(int age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
复制代码
咱们直接在业务方法中访问这些变量,看是否能获得表单的值。
通常地,咱们注册的时候,都是在Servlet上把基本信息封装到对象上...那么在Struts怎么作呢?
package qwer;
import java.util.Date;
/** * Created by ozc on 2017/4/27. */
public class User {
private String username;
private String psd;
private int age;
private Date birthday;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPsd() {
return psd;
}
public void setPsd(String psd) {
this.psd = psd;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
复制代码
public class ccAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String register() {
System.out.println(user.getUsername());
System.out.println(user.getPsd());
System.out.println(user.getAge());
System.out.println(user.getBirthday());
return "success";
}
}
复制代码
user.username
之类的<form action="${pageContext.request.contextPath}/register" method="post">
用户名:<input type="text" name="user.username"><br>
密码:<input type="text" name="user.psd"><br>
年龄:<input type="text" name="user.age"><br>
生日:<input type="text" name="user.birthday"><br>
<input type="submit" value="注册"><br>
</form>
复制代码
Struts怎么把数据保存在域对象中呢???Struts提供了三种方式
咱们能够经过ServletActionContext获得Servlet API
因为每一个用户拥有一个Action对象,那么底层为了维护用户拿到的是当前线程的request等对象,使用ThreadLocal来维护当前线程下的request、response等对象...
//经过ServletActionContext获得Servlet API
javax.servlet.ServletContext context = ServletActionContext.getServletContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
HttpServletResponse response = ServletActionContext.getResponse();
复制代码
咱们还能够经过ActionContext类来获得request、response、session、application被Struts封装的Map集合
//获得ActionContext 对象
ActionContext context = ActionContext.getContext();
Map<String, Object> session = context.getSession();
Map<String, Object> application = context.getApplication();
//这是request的Map
Map<String, Object> request = context.getContextMap();
复制代码
当web容器发现该Action实现了Aware接口,会把相对应的资源经过Aware接口注射进去,实际上就是一种IOC。
Aware实际就是一种拦截器,拦截代码在执行Action以前执行、将资源注射到Action中
实现SessionAware, RequestAware, ApplicationAware接口,它就要在程序中实现三个方法:
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
@Override
public void setApplication(Map<String, Object> map) {
this.application = map;
}
@Override
public void setRequest(Map<String, Object> map) {
this.request = map;
}
@Override
public void setSession(Map<String, Object> map) {
this.session = map;
}
复制代码
经过这些方法,咱们就能够获得对应的Map对象.....
那么,咱们有三种方法能够获得Servlet对应的对象,那么该使用哪种呢???
分析:
若是咱们须要使用到对象的其余方法,相似getContextPath()之类的,那么只能使用第一种
若是咱们就按照日常的开发,咱们就使用第二种【获取简单,没有耦合】
至于第三种,当咱们未来可能开发BaseAction的时候,就使用它!
前面博文已经讲解了,Struts2为咱们实现了数据自动封装...由上篇的例子咱们能够看出,表单提交过去的数据全都是String类型的,可是通过Struts自动封装,就改为是JavaBean对应成员变量的类型了。
可是呢,日期类型只支持是yyyy-MM-dd这种格式的,由于咱们在上个例子中直接使用的是Struts支持的格式,所以没有报错...本篇博文就是讲解Struts如何对日期类型的格式更好地支持
当咱们使用的是yyyyMMdd这种格式的时候,咱们看看Struts的自动封装能不能解析出相对应的日期
直接抛出了异常
那么,咱们怎么让Struts可以支持更多的日期格式呢??好比,我想Struts在自动封装数据的时候支持yyyyMMdd,yyyy年MM月dd日这样的日期格式.....
Struts提供了转换器给咱们使用,也就是,咱们能够自定义转换器,咱们定义了什么格式,Struts就能够根据对应的格式进行自动封装...
当咱们写完自定义转换器,是须要向Struts说明咱们写了,否则的话,Struts是不知道咱们自定义了转换器类的...
也就是说,咱们要想实现类型转换,须要两步:
通常地,咱们想要编写自定义转换器类,都是实现StrutsTypeConverter类的....
/** * Created by ozc on 2017/5/1. * 自定义异常转换器类 * * 咱们要实现的就是:在Struts转换的时候, * */
public class MyConvter extends StrutsTypeConverter {
//需求,当Struts自动封装数据时,也支持yyyyMMdd,yyyy年MM月dd日等格式的支持\
SimpleDateFormat[] format = {new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyyMMdd"), new SimpleDateFormat("yyyy年MM月dd日")};
/** * 把String转换为指定的类型 【String To Date】 * * * @param map * 当前上下文环境 * @param strings * jsp表单提交的字符串的值 * @param aClass * 要转换为的目标类型 */
@Override
public Object convertFromString(Map map, String[] strings, Class aClass) {
//判断是否有值
if (strings == null) {
return null;
}
//判断是不是日期类型的
if (Date.class != aClass) {
return null;
}
//遍历循环
for (SimpleDateFormat dateFormat : format) {
try {
//解析传递进来的第一个就行啦
dateFormat.parse(strings[0]);
} catch (ParseException e) {
//若是格式不对,那么就跳出当前的循环
continue;
}
}
return null;
}
@Override
public String convertToString(Map map, Object o) {
return null;
}
}
复制代码
告诉Struts我写了一个转换器类,也分两种方式
步骤:
xwork-conversion.properties
的文件java.util.Date=qwer.MyConvter
步骤:
Action名-conversion.properties
的文件user.birthday=qwer.MyConvter
当发生了日期转换的异常时,Struts给出的页面是这样子的:
这个咱们称之为input视图,咱们要作的就是给出用户更友好的提示,因而在struts.xml文件中配置:若是返回的是input视图,那么跳转到咱们相对应的页面上
<result name="input">/error.jsp</result>
复制代码
在讲解开山篇的时候就已经说了,Struts2框架封装了文件上传的功能........本博文主要讲解怎么使用Struts框架来完成文件上传和下载
首先,咱们先来回顾一下之前,咱们在web中上传文件是怎么作的....blog.csdn.net/hon_3y/arti…
可使用FileUpload或者SmartUpload组件来完成文件上传的功能。可是呢,FileUpload组件使用起来是比较麻烦的...而SmartUPload解决中文的问题也很是麻烦
从要导入的jar包咱们就能够知道:Struts内部仍是使用fileUpload上传组件....可是它极大的简化地咱们的具体操做
那咱们怎么用它呢??看下面的图
在注册页面上拥有两个上传文件控件
<form action="${pageContext.request.contextPath}/register" method="post" enctype="multipart/form-data">
<input type="file" name="photo"><br>
<input type="file" name="photo1"><br>
<input type="submit" value="注册"><br>
</form>
复制代码
获得相对应的File对象、上传文件名称、上传文件的类型
package fileupload;
import java.io.File;
/** * Created by ozc on 2017/5/2. */
public class FileUploadAction {
//上传文件对应的File对象
private File photo;
private File photo1;
//获得上传文件的名称
private String photoFileName;
private String photo1FileName;
//获得上传文件的类型
private String photoContentType;
private String photo1ContentType;
//给出相对应的setter
public void setPhoto(File photo) {
this.photo = photo;
}
public void setPhoto1(File photo1) {
this.photo1 = photo1;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public void setPhoto1FileName(String photo1FileName) {
this.photo1FileName = photo1FileName;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public void setPhoto1ContentType(String photo1ContentType) {
this.photo1ContentType = photo1ContentType;
}
public String register() {
System.out.println(photo1FileName);
System.out.println(photoFileName);
return "success";
}
}
复制代码
成功获得数据:
public String register() throws IOException {
//获得上传的路径
String path = ServletActionContext.getServletContext().getRealPath("upload");
System.out.println(path);
//建立文件对象
File destFile = new File(path,photoFileName);
//调用工具类方法,将文件拷贝过去
FileUtils.copyFile(photo, destFile);
return "success";
}
复制代码
咱们之前是经过设置request消息头来实现文件下载的.....那么在Struts又如何实现文件下载呢??
咱们请求服务器处理都是经过Action类来完成的,可是呢,Action类的业务方法都是返回字符串。所以,Struts在<result>
节点中提供了类型为stream的type值。经过stream来配置相对应的信息,从而实现下载!
public class downLoadAction {
//列出全部能够下载的文件
public String list() {
//获得upload文件夹
String path = ServletActionContext.getServletContext().getRealPath("/upload");
//建立file对象
File file = new File(path);
//列出文件下全部的文件
File[] files = file.listFiles();
//将这些文件存到request域中
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("files", files);
return "list";
}
}
复制代码
<action name="down_*" class="fileupload.downLoadAction" method="{1}">
<result name="{1}">/list.jsp</result>
<!-- <result name="{1}" type="stream">/index.jsp</result>-->
</action>
复制代码
<c:if test="${files==null}">
对不起,没有下载的页面
</c:if>
<c:if test="${files!=null}">
<table border="1px">
<tr>
<td>编号</td>
<td>文件名称</td>
<td>操做</td>
</tr>
<c:forEach items="${files}" varStatus="file" var="fileName">
<tr>
<td>${file.count}</td>
<%--若是直接写fileName,输出的名字带有路径,使用EL方法库来截取--%>
<td>${fn:substringAfter(fileName, "upload\\")}</td>
<td>
<%--使用url标签来构建url,否则超连接带有中文,会出现乱码--%>
<c:url var="url" value="down_downLoad">
<c:param name="fileName">${fn:substringAfter(fileName, "upload\\")}</c:param>
</c:url>
<a href="${url}">下载</a>
</td>
</tr>
</c:forEach>
</table>
</c:if>
复制代码
/** * 访问Action的业务方法仅仅返回的是字符串。所以Struts在result节点提供了stream类型的type, * 指定了stream就表明着我这是要下载的... * <p> * 既然要下载文件,那么确定须要几样东西: * 一、文件名 * 二、表明文件的流 */
public String downLoad() {
return "downLoad";
}
//获得要下载的文件名,Struts提供了自动封装的功能
private String fileName;
//若是文件名是中文的,那么须要手动转换,由于超连接是get方法提交
public void setFileName(String fileName) throws UnsupportedEncodingException {
fileName = new String(fileName.getBytes("ISO8859-1"), "UTF-8");
this.fileName = fileName;
System.out.println(fileName);
}
//获得表明下载文件流,该方法由Struts调用
public InputStream getAttrInputStream() {
return ServletActionContext.getServletContext().getResourceAsStream("/upload/" + fileName);
}
//下载时,显示的名称【若是是中文,可能会乱码,所以要URLencode】---->在Struts.xml文件中经过${}可获取
public String getDownFileName() throws UnsupportedEncodingException {
fileName = URLEncoder.encode(fileName, "UTF-8");
return fileName;
}
复制代码
<action name="down_*" class="fileupload.downLoadAction" method="{1}">
<result name="{1}">/list.jsp</result>
<result name="downLoad" type="stream">
<!--运行下载的类型,指定为全部的二进制文件-->
<param name="contentType">application/octet-stream</param>
<!-- 对应的是Action中属性: 返回流的属性【其实就是getAttrInputStream()】 -->
<param name="inputName">attrInputStream</param>
<!-- 下载头,包括:浏览器显示的文件名 --> <!--${}这里不是EL表达式-->
<param name="contentDisposition">attachment;filename=${downFileName}</param>
<!-- 缓冲区大小设置 -->
<param name="bufferSize">1024</param>
</result>
</action>
复制代码
在Struts2中模型驱动就是用来封装数据的..完成数据的自动封装.
咱们以前就使用过Sturts2的数据自动封装功能,是用params拦截器完成的...既然有了params拦截器,为啥还要模型驱动??
当咱们使用params拦截器完成数据自动封装的时候,若是要封装的是JavaBean对象,那么在web表单中就必须的name写上javaBean.属性名
....
这样的话,web层和Action层就耦合了...由于在web层必需要知道封装的JavaBean对象是什么才可以实现自动封装!
而模型驱动就解决了这个问题!即时不知道Action层的JavaBean对象是什么,也可以完成数据自动封装!
实现模型驱动功能也是由拦截器完成的,咱们来看看拦截器到底作了什么吧....
<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
复制代码
拦截方法的源码是这样的:
public String intercept(ActionInvocation invocation) throws Exception {
//获得当前要执行的Action对象
Object action = invocation.getAction();
//判断该Action对象是否实现了ModelDriven接口
if(action instanceof ModelDriven) {
ModelDriven modelDriven = (ModelDriven)action;
//获取值栈对象
ValueStack stack = invocation.getStack();
//获得model的对象
Object model = modelDriven.getModel();
//把对象存到值栈对象中
if(model != null) {
stack.push(model);
}
if(this.refreshModelBeforeResult) {
invocation.addPreResultListener(new ModelDrivenInterceptor.RefreshModelBeforeResult(modelDriven, model));
}
}
return invocation.invoke();
}
复制代码
把model对象放到值栈对象以后,**Parameters 拦截器将把表单字段映射到 ValueStack 栈的栈顶对象的各个属性中. **
也就是说,使用模型驱动是须要配合Params拦截器完成的!
public class UserAction extends ActionSupport implements ModelDriven<User> {
public String login() {
return SUCCESS;
}
@Override
public User getModel() {
return null;
}
}
复制代码
public class UserAction extends ActionSupport implements ModelDriven<User> {
//这里必定要实例化
User user = new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public User getModel() {
return user;
}
}
复制代码
JSP提交页面,直接写上JavaBean对象的属性就好了..不须要写上JavaBean对象的名称!
<form action="${pageContext.request.contextPath}/user_execute">
<table border="1">
<tr>
<td>用户名:<input type="text" name="username"></td>
</tr>
<tr>
<td> 密码:<input type="password" name="password"></td>
</tr>
<tr>
<td>电话:<input type="text" name="cellphone"></td>
</tr>
<tr>
<td> 邮箱:<input type="text" name="email"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
复制代码
@Override
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
复制代码
若是文章有错的地方欢迎指正,你们互相交流。习惯在微信看技术文章,想要获取更多的Java资源的同窗,能够关注微信公众号:Java3y