Struts2【开发Action】(三)

简介: Struts2【开发Action】

错误提示页面


当发生了日期转换的异常时,Struts给出的页面是这样子的:

微信图片_20220221171018.png这里写图片描述


这个我们称之为input视图,我们要做的就是给出用户更友好的提示,于是在struts.xml文件中配置:如果返回的是input视图,那么跳转到我们相对应的页面上


<result name="input">/error.jsp</result>

微信图片_20220221171020.png这里写图片描述

文件上传和下载


在讲解开山篇的时候就已经说了,Struts2框架封装了文件上传的功能……..本博文主要讲解怎么使用Struts框架来完成文件上传和下载


回顾以前的文件上传


首先,我们先来回顾一下以前,我们在web中上传文件是怎么做的….http://blog.csdn.net/hon_3y/article/details/66975268


可以使用FileUpload或者SmartUpload组件来完成文件上传的功能。但是呢,FileUpload组件使用起来是比较麻烦的…而SmartUPload解决中文的问题也非常麻烦


使用Struts进行文件上传


从要导入的jar包我们就可以知道:Struts内部还是使用fileUpload上传组件….但是它极大的简化地我们的具体操作


那我们怎么用它呢??看下面的图

微信图片_20220221171023.jpg这里写图片描述

  • 在Action中使用在表单中定义的name,就可以获取代表的上传文件的File对象
  • 在Action中使用在表单中定义的name+FileName,就得到上传文件的名字


JSP页面


在注册页面上拥有两个上传文件控件


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


Action


得到相对应的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";
    }
}

成功得到数据:

微信图片_20220221171027.jpg

这里写图片描述微信图片_20220221171029.jpg这里写图片描述



Action业务代码:


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";
    }
  • 效果:

微信图片_20220221171032.jpg这里写图片描述


文件下载


我们以前是通过设置request消息头来实现文件下载的…..那么在Struts又如何实现文件下载呢??


我们请求服务器处理都是通过Action类来完成的,但是呢,Action类的业务方法都是返回字符串。因此,Struts在<result>节点中提供了类型为stream的type值。通过stream来配置相对应的信息,从而实现下载


列出所有可以下载的文件


  • Action类的业务方法
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";
    }
}
  • Struts配置文件
<action name="down_*" class="fileupload.downLoadAction" method="{1}">
            <result name="{1}">/list.jsp</result>
           <!-- <result name="{1}" type="stream">/index.jsp</result>-->
        </action>
  • JSP显示页面
<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代码:
/**
     * 访问Action的业务方法仅仅返回的是字符串。因此Struts在result节点提供了stream类型的type,
     * 指定了stream就代表着我这是要下载的...
     * <p>
     * 既然要下载文件,那么肯定需要几样东西:
     * 1、文件名
     * 2、代表文件的流
     */
    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;
    }
  • Struts.xml
<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拦截器完成的!


使用数据模型驱动


实现ModelDriven接口


  • 实现ModelDriven接口,重写方法….实现接口时,要封装的对象是什么,形参类型就给什么
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>
  • 在Action业务方法中输出User对象的数据
@Override
    public String execute() throws Exception {
        System.out.println(user);
        return SUCCESS;
    }

微信图片_20220221171036.png这里写图片描述

目录
相关文章
|
安全 Java 应用服务中间件
struts2中Action的生命周期
struts2中Action的生命周期
80 0
|
XML 前端开发 数据格式
Struts2里面灵活action跳转
Struts2里面灵活action跳转
173 0
|
Java
Struts2【开发Action】(一)
Struts2【开发Action】
123 0
Struts2【开发Action】(一)
|
Java API 容器
Struts2【开发Action】(二)
Struts2【开发Action】
155 0
Struts2【开发Action】(二)
|
Java Apache 数据格式
Struts Action 控制器
控制器 即,mvc模型的控制器模型,用于接收数据,传递给视图层,和模型层默认使用execute方法 查看相关接口 查看com.opensymphony.xwork2下的Action接口文件如下 /* * Licensed to the Apache Software Foundation (AS...
1055 0
|
Java API 数据安全/隐私保护
Struts2【开发Action】知识要点
前言 前面Struts博文基本把Struts的配置信息讲解完了.....本博文主要讲解Struts对数据的处理 Action开发的三种方式 在第一次我们写开发步骤的时候,我们写的Action是继承着ActionSupport类的.
1163 0
|
Java 测试技术 程序员
[Struts]使用StrutsTestCase对Action进行单元测试简介
目前,测试驱动开发正变得越来越流行,由于“存在的就是合理的”,这种开发方式必然有其优越之处。作为一个小小程序员,对新鲜技术的追求是工作的重要动力,相信大家都有同感吧。 测试驱动开发是极限编程(XP)的重要组成部分,从字面上就可以看出,它是先有测试再有代码的。
1197 0