码迷,mamicode.com
首页 > 其他好文 > 详细

Struts(三)

时间:2016-07-06 23:18:21      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

架构:
    技术分享
    
===============================struts2 核心接口和类=========================
名称                                作用
ActionMapper        根据请求的URI查找是否存在对应Action调用
ActionMapping        保存调用Action的映射信息,如namespace、name等
ActionProxy         在XWork和真正的Action之间充当代理
ActionInvocation    表示Action的执行状态,保存拦截器、Action实例
Interceptor            在请求处理之前或者之后执行的Struts 2组件


===============================struts2 拦截器=========================
struts2大多数核心功能是通过拦截器实现,每个拦截器完成某项功能,拦截器之间可以互相自由组合

拦截器方法在Action执行之前或执行之后

拦截器栈
    1.结构:相当于多个拦截器的组合
    2.功能:也是拦截器
    
与过滤器原理相似
    为Action提供附加功能,无须修改Action代码,使用拦截器来提供
    
三阶段执行周期:
        1.Action执行前的预处理
        2.将控制交给后续拦截器或者返回字符串(执行Action)
        3.Action执行后的处理
        
范例:

 1 public class MyTimeInterceptor extends AbstractInterceptor {
 2     /* (non-Javadoc)
 3      * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
 4      */
 5     @Override
 6     public String intercept(ActionInvocation actionInvocation) throws Exception {
 7         //预处理工作
 8         System.out.println("执行Action之前");
 9         long startTime = System.currentTimeMillis();
10         
11         //执行后续拦截器或Action
12         String result = actionInvocation.invoke();
13         
14         //后续处理工作
15         System.out.println("执行Action之后");
16         long execTime = System.currentTimeMillis() - startTime;
17         System.out.println("The interval time is "+execTime+" ms");
18         
19         //返回结果字符串
20         return result;
21     }
22 }


拦截器的xml配置:struts.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
 3 <struts>
 4     <!-- namespace是请求路径 -->
 5     <package name="default" namespace="/" extends="struts-default">
 6     
 7         <!-- 配置拦截器 -->
 8         <interceptors>
 9             <!-- 定义单个拦截器 -->
10             <interceptor name="myTime" class="com.Elastic.StrutsDemo3.ivy.interceptor.MyTimeInterceptor"></interceptor>
11             <interceptor name="authorization" class="com.Elastic.StrutsDemo3.ivy.interceptor.AuthorizationInterceptor"></interceptor>
12             
13             <!-- 自定义拦截器栈 -->
14             <interceptor-stack name="myDefault">
15                 <interceptor-ref name="myTime"></interceptor-ref>
16                 <interceptor-ref name="defaultStacks"></interceptor-ref>
17             </interceptor-stack>
18             
19         </interceptors>
20         
21         <action name="login" class="com.Elastic.StrutsDemo3.ivy.action.UserAction" method="login">
22             <result name="success" type="redirectAction">index</result>
23             <!-- 引用拦截器 -->
24             <interceptor-ref name="myTime"></interceptor-ref>
25             <!-- 引用Struts默认的拦截器栈。如果用了自定义拦截器,一定要加载struts-default中的defaultStacks -->
26             <interceptor-ref name="defaultStacks"></interceptor-ref>
27             
28             <!-- 引用拦截器栈 -->
29             <!-- <interceptor-ref name="myDefault"></interceptor-ref> -->
30         </action>
31         
32     </package>
33 </struts>


自带拦截器
    1.params拦截器:负责将请求参数设置为Action属性
    2.servletConfig拦截器:将源于servlet API的各种对象注入到Action
    3.fileUpload拦截器:对文件上传提供支持
    4.exception拦截器:捕获异常,并将异常映射到自定义d错误页面
    5.validation拦截器:调用验证框架进行数据验证
    6.workflow拦截器:调用Action类的validate(),执行数据验证
    
自定义拦截器:
    void init():初始化拦截器所需资源
    void destroy():释放在init()中分配的资源
    String intercept(ActionInvocation actionInvocation) throws Exception:
        1.实现拦截器功能
        2.利用ActionInvocation参数获取Action状态
        3.返回结果码(result)字符串
        
    继承AbstractInterceptor类
        1.提供了init()和destroy()方法的空实现
        2.只需要实现intercept方法即可
        3.推荐使用
范例:权限验证拦截器

 1 public class AuthorizationInterceptor extends AbstractInterceptor {
 2      public String intercept(ActionInvocation invocation) throws Exception{
 3           //获取用户会话信息
 4           Map session = invocation.getInvocationContext().getSession();
 5           User user = (User)session.get("login");
 6           if (user == null) {
 7               //终止执行,返回登录页面
 8               return Action.LOGIN;
 9           } else {
10               //继续执行剩余的拦截器和Action
11               return invocation.invoke();
12           }
13       }
14 }


===================================文件上传下载==========================
一、文件上传
Commons-FileUpload组件
    Commons是Apache开放源代码组织的一个Java子项目,其中的FileUpload是用来处理HTTP文件上传的子项目

    特点:
        a.使用简单:可以方便地嵌入到JSP文件中,编写少量代码即可完成文件的上传功能
        b.能够全程控制上传内容
        c.能够对上传文件的大小、类型进行控制
    
    环境要求:commons-fileupload-xxx.jar    commons-io-xxx.jar(在 struts2 的jar包中查找)
范例:

 1 public class UploadAction extends ActionSupport {
 2     private File upload;
 3     private String uploadContentType;
 4     private String uploadFileName;
 5     private String savePath;
 6     //…省略set和get方法
 7     public String execute() throws Exception {
 8     FileInputStream fis=new FileInputStream(getUpload());
 9     FileOutputStream fos=new FileOutputStream(getSavePath()+"\\”+this.getUploadFileName());
10     }
11 }
1 <s:form action="upload.action" enctype="multipart/form-data" method="post">
2     <s:file name="upload" label="选择文件"/>
3 </s:form>
4 
5 <action name="upload" class="com.xuetang9.demo.action.UploadAction">
6     <!--通过param参数设置保存目录的路径-->
7     <param name="savePath">/upload</param>
8     <result name="success">/upload_success.jsp</result>
9 </action>


多文件上传
    【name属性相同】
    将三个属性的类型修改成数组类型
    //获取提交的多个文件
    private File[] upload;
    //封装上传文件的类型
    private String[] uploadContentType;
    //封装上传文件名称
    private String[] uploadFileName;
    
二、文件下载
stream结果类型
    1.将文件数据(通过InputStream获取)直接写入响应流
    2.相关参数的配置
    名称                    作用
    contentType            设置发送到浏览器的MIME类型
    contentLength        设置文件的大小
    contentDisposition    设置响应的HTTP头信息中的Content-Disposition参数的值(一方面表示文件的处理方式,另一方面指定下载文件的显示文件名称)
    inputName            指定Action中提供的inputStream类型的属性名称
    bufferSize            设置读取和下载文件时的缓冲区大小
    
    contentType:指定文件下载的类型
        文件类型    类型设置
        Word        application/msword
        Execl        application/vnd.ms-excel
        PPT            application/vnd.ms-powerpoint
        图片        image/gif , image/bmp,image/jpeg
        文本文件    text/plain
        html网页    text/html
        可执行文件    application/octet-stream
范例:

 1 public class FileDownAction extends ActionSupport {
 2     //读取下载文件的目录
 3     private String inputPath;
 4     //下载文件的文件名
 5     private String fileName;
 6     //下载文件的类型
 7     private String conetntType;
 8     //创建InputStream输入流
 9     public InputStream getInputStream() throws FileNotFoundException{
10         String path=ServletActionContext.getServletContext().getRealPath(inputPath);
11         return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
12     }
13 }
 1 <action name="download" class="com.xuetang9.demo.action.FileDownAction">
 2     <param name="inputPath">/upload</param>
 3     
 4     <!--  指定类型为stream -->
 5     <result name="success" type="stream">
 6     
 7         <!-- 通用设置 -->
 8         <param name="contentType">application/octet-stream</param>
 9         <param name="inputName">inputStream</param>
10         <param name="contentDisposition">
11             <!--  
12                 attachement表示以附件形式下载
13                 filename表示下载时显示的文件名称
14             -->
15             attachment;filename="${fileName}"
16         </param>
17         <param name="bufferSize">4096</param>
18     </result>
19 </action>


只有首页用,清除session内容
    
范例:
1.实体类及其配置文件
a.User类

 1 package com.Elastic.StrutsDemo3.ivy.entity;
 2 
 3 import java.io.Serializable;
 4 public class User implements Serializable{
 5     private String userName;
 6     private String passWord;
 7     
 8     public String getUserName() {
 9         return userName;
10     }
11     public void setUserName(String userName) {
12         this.userName = userName;
13     }
14     public String getPassWord() {
15         return passWord;
16     }
17     public void setPassWord(String passWord) {
18         this.passWord = passWord;
19     }
20 }


b.User.hbm.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <class name="com.Elastic.StrutsDemo3.ivy.entity.User" table="user">
 6         <id name="userName" type="java.lang.String">
 7             <column name="userName"></column>
 8             <generator class="assigned"></generator>
 9         </id>
10         <property name="passWord" type="java.lang.String">
11             <column name="passWord"></column>
12         </property>
13     </class>
14 </hibernate-mapping>

2.action包
a.UserAction类

 1 package com.Elastic.StrutsDemo3.ivy.action;
 2 
 3 import com.Elastic.StrutsDemo3.ivy.entity.User;
 4 import com.opensymphony.xwork2.ActionContext;
 5 import com.opensymphony.xwork2.ActionSupport;
 6 public class UserAction extends ActionSupport{
 7     private User user;
 8     
 9     public User getUser() {
10         return user;
11     }
12 
13     public void setUser(User user) {
14         this.user = user;
15     }
16 
17     public String login(){
18         System.out.println("执行登录功能!");
19         if ("admin".equals(user.getUserName().trim()) && "123456".equals(user.getPassWord().trim())) {
20             ActionContext.getContext().getSession().put("loginUser", "admin");
21             return SUCCESS;
22         }
23         return LOGIN;
24     }
25 }


b.IndexAction类

 1 package com.Elastic.StrutsDemo3.ivy.action;
 2 
 3 import com.opensymphony.xwork2.ActionContext;
 4 import com.opensymphony.xwork2.ActionSupport;
 5 public class IndexAction extends ActionSupport{
 6     private String data;
 7     
 8     public String getData() {
 9         return data;
10     }
11 
12     public void setData(String data) {
13         this.data = data;
14     }
15 
16     public String index(){
17         System.out.println("查询数据");
18         data = "从数据库中得到了数据";
19         ActionContext.getContext().getSession().put("data", data);
20         return SUCCESS;
21     }
22 }


c.上传文件 -- FileAction类

 1 package com.Elastic.StrutsDemo3.ivy.action;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.InputStream;
 9 import java.io.UnsupportedEncodingException;
10 
11 import org.apache.struts2.ServletActionContext;
12 
13 import com.opensymphony.xwork2.ActionSupport;
14 public class FileAction extends ActionSupport{
15     //文件夹的名称与表单name属性值一致
16     private File head;
17     //文件类型(后缀固定写法)
18     private String headContentType;
19     //文件名称(后缀固定写法)
20     private String headFileName;
21     
22     public File getHead() {
23         return head;
24     }
25 
26     public void setHead(File head) {
27         this.head = head;
28     }
29 
30     public String getHeadContentType() {
31         return headContentType;
32     }
33 
34     public void setHeadContentType(String headContentType) {
35         this.headContentType = headContentType;
36     }
37 
38     public String getHeadFileName() {
39         return headFileName;
40     }
41 
42     public void setHeadFileName(String headFileName) {
43         this.headFileName = headFileName;
44     }
45 
46     /**
47      * 文件上传
48      * @return
49      * @throws IOException
50      */
51     public String fileUpload() throws IOException {
52         //1.读成二进制文件
53         FileInputStream fis = new FileInputStream(head);
54         //虚拟路径--项目下
55         String path =  "/head/" + headFileName;
56         //绝对路径 -- 物理路径
57         String realPath = ServletActionContext.getServletContext().getRealPath(path);
58         //获取文件的总大小
59         int length = fis.available();
60         //读成二进制文件
61         byte[] buffers = new byte[length];
62         fis.read(buffers);
63         
64         //2.输出文件
65         FileOutputStream fos = new FileOutputStream(realPath);
66         fos.write(buffers);
67         fis.close();
68         fos.close();
69         return SUCCESS;
70     }
71 }


c2.下载文件 -- FileAction类

  1 package com.Elastic.StrutsDemo3.ivy.action;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileNotFoundException;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.io.InputStream;
  9 import java.io.UnsupportedEncodingException;
 10 
 11 import org.apache.struts2.ServletActionContext;
 12 
 13 import com.opensymphony.xwork2.ActionSupport;
 14 public class FileAction extends ActionSupport {
 15     // 文件夹的名称与表单name属性值一致
 16     private File head;
 17     // 文件类型(后缀固定写法)
 18     private String headContentType;
 19     // 文件名称(后缀固定写法)
 20     private String headFileName;
 21 
 22     private String savePath;
 23 
 24     public String getSavePath() {
 25         return savePath;
 26     }
 27 
 28     public void setSavePath(String savePath) {
 29         this.savePath = savePath;
 30     }
 31 
 32     public File getHead() {
 33         return head;
 34     }
 35 
 36     public void setHead(File head) {
 37         this.head = head;
 38     }
 39 
 40     public String getHeadContentType() {
 41         return headContentType;
 42     }
 43 
 44     public void setHeadContentType(String headContentType) {
 45         this.headContentType = headContentType;
 46     }
 47 
 48     public String getHeadFileName() {
 49         return headFileName;
 50     }
 51 
 52     public void setHeadFileName(String headFileName) {
 53         this.headFileName = headFileName;
 54     }
 55 
 56     /**
 57      * 文件上传
 58      *
 59      * @return
 60      * @throws IOException
 61      */
 62     public String fileUpload() throws IOException {
 63         // 1.读成二进制文件
 64         FileInputStream fis = new FileInputStream(head);
 65         // 虚拟路径--项目下
 66         String path = savePath + "/" + headFileName;
 67         // 绝对路径 -- 物理路径
 68         String realPath = ServletActionContext.getServletContext().getRealPath(path);
 69         // 获取文件的总大小
 70         int length = fis.available();
 71         // 读成二进制文件
 72         byte[] buffers = new byte[length];
 73         fis.read(buffers);
 74 
 75         // 2.输出文件
 76         FileOutputStream fos = new FileOutputStream(realPath);
 77         fos.write(buffers);
 78         fis.close();
 79         fos.close();
 80         return SUCCESS;
 81 
 82     }
 83 
 84     private String fileName;
 85 
 86     public String getFileName() {
 87         return fileName;
 88     }
 89 
 90     public void setFileName(String fileName) {
 91         this.fileName = fileName;
 92     }
 93 
 94     private InputStream inputStream;
 95 
 96     public void setInputStream(InputStream inputStream) {
 97         this.inputStream = inputStream;
 98     }
 99 
100     public InputStream getInputStream() throws FileNotFoundException {
101 
102         try {
103             fileName = new String(fileName.getBytes("iso-8859-1"), "utf-8");
104             String path = "/head/" + fileName;
105             String realPath = ServletActionContext.getServletContext().getRealPath(path);
106             inputStream = new FileInputStream(realPath);
107         } catch (Exception e) {
108             e.printStackTrace();
109         }
110         return inputStream;
111     }
112 }


3.interceptor包
a.MyTimeInterceptor类

 1 package com.Elastic.StrutsDemo3.ivy.interceptor;
 2 
 3 import com.opensymphony.xwork2.ActionInvocation;
 4 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 5 public class MyTimeInterceptor extends AbstractInterceptor {
 6 
 7     /* (non-Javadoc)
 8      * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
 9      */
10     @Override
11     public String intercept(ActionInvocation actionInvocation) throws Exception {
12         System.out.println("执行Action之前");
13         String result = actionInvocation.invoke();
14         System.out.println("执行Action之后");
15         return result;
16     }
17 }


b.AuthorizationInterceptor类

 1 package com.Elastic.StrutsDemo3.ivy.interceptor;
 2 
 3 import com.opensymphony.xwork2.Action;
 4 import com.opensymphony.xwork2.ActionContext;
 5 import com.opensymphony.xwork2.ActionInvocation;
 6 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 7 public class AuthorizationInterceptor extends AbstractInterceptor{
 8 
 9     /* (non-Javadoc)
10      * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
11      */
12     @Override
13     public String intercept(ActionInvocation actionInvocation) throws Exception {
14         System.out.println("登录验证之前");
15         // 执行Action之前判断用户是否登录
16         boolean isLogined = ActionContext.getContext().getSession().containsKey("loginUser");
17         if (!isLogined) {
18             return Action.LOGIN;
19         }
20         String result = actionInvocation.invoke();
21         System.out.println("登录验证之后");
22         return result;
23     }
24 }


4.jsp
a.index.jsp

 1 <%-- 引入JSP页面PAGE指令 --%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <%-- 引入JSTL标签指令 --%>
 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 6 <!DOCTYPE html>
 7 <html language="zh-CN">
 8 <head>
 9     <meta charset="utf-8">
10     <!-- 设置浏览器渲染的引擎  -->
11     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
12     <!-- 设置支持移动设备  -->
13     <meta name="viewport" content="width=device-width, initial-scale=1">
14     <title>首页</title>
15     <!-- 引用bootstrap样式 -->
16     <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.min.css">
17 </head>
18 <body>
19     
20     <div class="container">
21         ${data }
22     </div>
23     
24     <!-- 引用外部JS文件  -->
25     <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-2.2.4.js"></script>
26     <script type="text/javascript" src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script>
27 
28 </body>
29 </html>


b.login.jsp

 1 <%-- 引入JSP页面PAGE指令 --%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <%-- 引入JSTL标签指令 --%>
 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 6 <%@ taglib uri="/struts-tags" prefix="s"%>
 7 <!DOCTYPE html>
 8 <html language="zh-CN">
 9 <head>
10 <meta charset="utf-8">
11 <!-- 设置浏览器渲染的引擎  -->
12 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
13 <!-- 设置支持移动设备  -->
14 <meta name="viewport" content="width=device-width, initial-scale=1">
15 <title>网页标题</title>
16 <!-- 引用bootstrap样式 -->
17 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.min.css">
18 </head>
19 <body>
20     <s:debug></s:debug>
21     <div class="container-fluid">
22         <div class="panel panel-default center-block" style="width: 500px;">
23             <div class="panel-heading">登录</div>
24             <div class="panel-body">
25                 <form action="user/login" method="post" class="form-horizontal">
26                     <div class="form-group">
27                         <label class="col-md-3 control-label" for="userName">用户名:</label>
28                         <div class="col-md-6">
29                             <input class="form-control" id="userName" name="user.userName"
30                                 type="text" autocomplete="off" />
31                         </div>
32                         <div class="col-md-3">
33                             ${fieldErrors.userName[0] }
34                         </div>
35                     </div>
36                     <div class="form-group">
37                         <label class="col-md-3 control-label" for="passWord">密码:</label>
38                         <div class="col-md-6">
39                             <input class="form-control" id="passWord" name="user.passWord"
40                                 type="password" />
41                         </div>
42                         <div class="col-md-3">
43                         
44                         </div>
45                     </div>
46                     <div class="form-group">
47                         <input class="btn btn-primary btn-block" type="submit" value="登录" />
48                     </div>
49                 </form>
50             </div>
51         </div>
52     </div>
53 
54     <!-- 引用外部JS文件  -->
55     <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-2.2.4.js"></script>
56     <script type="text/javascript" src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script>
57 
58 </body>
59 </html>


c.fileUpload.jsp

 1 <%-- 引入JSP页面PAGE指令 --%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <%-- 引入JSTL标签指令 --%>
 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 6 <!DOCTYPE html>
 7 <html language="zh-CN">
 8 <head>
 9     <meta charset="utf-8">
10     <!-- 设置浏览器渲染的引擎  -->
11     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
12     <!-- 设置支持移动设备  -->
13     <meta name="viewport" content="width=device-width, initial-scale=1">
14     <title>网页标题</title>
15     <!-- 引用bootstrap样式 -->
16     <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.min.css">
17 </head>
18 <body>
19     <div class="container-fluid">
20         <form action="upload" enctype="multipart/form-data" method="post">
21             <input name="head" type="file" />
22             <input type="submit" value="上传头像" />
23         </form>
24     </div>
25     
26     <!-- 引用外部JS文件  -->
27     <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-2.2.4.js"></script>
28     <script type="text/javascript" src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script>
29 
30 </body>
31 </html>


d.downLoad.jsp

 1 <%-- 引入JSP页面PAGE指令 --%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <%-- 引入JSTL标签指令 --%>
 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 6 <!DOCTYPE html>
 7 <html language="zh-CN">
 8 <head>
 9     <meta charset="utf-8">
10     <!-- 设置浏览器渲染的引擎  -->
11     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
12     <!-- 设置支持移动设备  -->
13     <meta name="viewport" content="width=device-width, initial-scale=1">
14     <title>网页标题</title>
15     <!-- 引用bootstrap样式 -->
16     <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.min.css">
17 </head>
18 <body>
19     <div class="container-fluid">
20         <a href="downLoad?fileName=面试.txt">面试.txt</a>
21     </div>
22     
23     <!-- 引用外部JS文件  -->
24     <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-2.2.4.js"></script>
25     <script type="text/javascript" src="<%=request.getContextPath() %>/js/bootstrap.min.js"></script>
26 
27 </body>
28 </html>


5.Struts配置文件
a1.拦截器 -- struts.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
 3 <struts>
 4     <package name="default" namespace="/" extends="struts-default">
 5     
 6         <!-- 配置拦截器 -->
 7         <interceptors>
 8             <!-- 定义单个拦截器 -->
 9             <interceptor name="myTime" class="com.Elastic.StrutsDemo3.ivy.interceptor.MyTimeInterceptor"></interceptor>
10             <interceptor name="authorization" class="com.Elastic.StrutsDemo3.ivy.interceptor.AuthorizationInterceptor"></interceptor>
11         
12             <!-- 自定义拦截器栈 -->
13             <interceptor-stack name="myDefault">
14                 <interceptor-ref name="myTime"></interceptor-ref>
15                 <interceptor-ref name="defaultStack"></interceptor-ref>
16             </interceptor-stack>
17         
18             <!-- 自定义拦截器栈 -->
19             <interceptor-stack name="myAuthorStack">
20                 <interceptor-ref name="myTime"></interceptor-ref>
21                 <interceptor-ref name="authorization"></interceptor-ref>
22                 <interceptor-ref name="defaultStack"></interceptor-ref>
23             </interceptor-stack>
24         </interceptors>
25     
26         <!-- 全局的result -->
27         <global-results>
28             <result name="login" type="redirect">/login.jsp</result>
29         </global-results>
30         
31         <action name="login" class="com.Elastic.StrutsDemo3.ivy.action.UserAction" method="login">
32             <result name="success" type="redirectAction">index</result>
33             <!-- 方法1 -->
34             <!-- 引用拦截器 -->
35             <!-- <interceptor-ref name="myTime"></interceptor-ref> -->
36             <!-- 引用Struts默认的拦截器栈 -->
37             <!-- <interceptor-ref name="defaultStack"></interceptor-ref> -->
38             
39             <!-- 方法2 -->
40             <interceptor-ref name="myDefault"></interceptor-ref>
41         </action>
42         
43         <action name="index" class="com.Elastic.StrutsDemo3.ivy.action.IndexAction" method="index">
44             <result name="success" type="redirect">/index.jsp</result>
45             <!-- 拦截器配置有顺序 -->
46             <!-- 方法1 -->
47             <interceptor-ref name="authorization"></interceptor-ref>
48             <interceptor-ref name="myTime"></interceptor-ref>
49             <!-- 引用Struts默认的拦截器栈 -->
50             <interceptor-ref name="defaultStack"></interceptor-ref>
51             
52             <!-- 方法2 -->
53             <!-- <interceptor-ref name="myAuthorStack"></interceptor-ref> -->
54         </action>
55     </package>
56 </struts>


a2.上传文件 -- struts.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
 3 <struts>
 4     <package name="default" namespace="/" extends="struts-default">
 5     
 6         <!-- 配置拦截器 -->
 7         <interceptors>
 8             <!-- 定义单个拦截器 -->
 9             <interceptor name="myTime" class="com.Elastic.StrutsDemo3.ivy.interceptor.MyTimeInterceptor"></interceptor>
10             <interceptor name="authorization" class="com.Elastic.StrutsDemo3.ivy.interceptor.AuthorizationInterceptor"></interceptor>
11         
12             <!-- 自定义拦截器栈 -->
13             <interceptor-stack name="myDefault">
14                 <interceptor-ref name="myTime"></interceptor-ref>
15                 <interceptor-ref name="defaultStack"></interceptor-ref>
16             </interceptor-stack>
17         
18             <!-- 自定义拦截器栈 -->
19             <interceptor-stack name="myAuthorStack">
20                 <interceptor-ref name="myTime"></interceptor-ref>
21                 <interceptor-ref name="authorization"></interceptor-ref>
22                 <interceptor-ref name="defaultStack"></interceptor-ref>
23             </interceptor-stack>
24         
25         </interceptors>
26     
27         <!-- 全局的result -->
28         <global-results>
29             <result name="login" type="redirect">/login.jsp</result>
30         </global-results>
31     
32         <action name="login" class="com.Elastic.StrutsDemo3.ivy.action.UserAction" method="login">
33             <result name="success" type="redirectAction">index</result>
34             <interceptor-ref name="myDefault"></interceptor-ref>
35         </action>
36         
37         <action name="index" class="com.Elastic.StrutsDemo3.ivy.action.IndexAction" method="index">
38             <result name="success" type="redirect">/index.jsp</result>
39             <interceptor-ref name="myAuthorStack"></interceptor-ref>
40         </action>
41         
42         <action name="upload" class="com.Elastic.StrutsDemo3.ivy.action.FileAction" method="fileUpload">
43             <result name="success" type="redirect">/index.jsp</result>
44         </action>
45         
46     </package>
47 </struts>


a3.下载文件 -- struts.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
 3 <struts>
 4     <package name="default" namespace="/" extends="struts-default">
 5     
 6         <!-- 配置拦截器 -->
 7         <interceptors>
 8             <!-- 定义单个拦截器 -->
 9             <interceptor name="myTime" class="com.Elastic.StrutsDemo3.ivy.interceptor.MyTimeInterceptor"></interceptor>
10             <interceptor name="authorization" class="com.Elastic.StrutsDemo3.ivy.interceptor.AuthorizationInterceptor"></interceptor>
11         
12             <!-- 自定义拦截器栈 -->
13             <interceptor-stack name="myDefault">
14                 <interceptor-ref name="myTime"></interceptor-ref>
15                 <interceptor-ref name="defaultStack"></interceptor-ref>
16             </interceptor-stack>
17         
18             <!-- 自定义拦截器栈 -->
19             <interceptor-stack name="myAuthorStack">
20                 <interceptor-ref name="myTime"></interceptor-ref>
21                 <interceptor-ref name="authorization"></interceptor-ref>
22                 <interceptor-ref name="defaultStack"></interceptor-ref>
23             </interceptor-stack>
24         
25         </interceptors>
26     
27         <!-- 全局的result -->
28         <global-results>
29             <result name="login" type="redirect">/login.jsp</result>
30         </global-results>
31     
32         <action name="login" class="com.Elastic.StrutsDemo3.ivy.action.UserAction" method="login">
33             <result name="success" type="redirectAction">index</result>
34             <interceptor-ref name="myDefault"></interceptor-ref>
35             
36         </action>
37         
38         <action name="index" class="com.Elastic.StrutsDemo3.ivy.action.IndexAction" method="index">
39             <result name="success" type="redirect">/index.jsp</result>
40             <interceptor-ref name="myAuthorStack"></interceptor-ref>
41         </action>
42         
43         <action name="upload" class="com.Elastic.StrutsDemo3.ivy.action.FileAction" method="fileUpload">
44             <param name="savePath">/head</param>
45             <result name="success" type="redirect">/index.jsp</result>
46         </action>
47         
48         <action name="downLoad" class="com.Elastic.StrutsDemo3.ivy.action.FileAction">
49             <result type="stream">
50                 <param name="contentType">application/octet-stream</param>
51                 <param name="inputName">inputStream</param>s
52                 <param name="contentDisposition">
53                     attachment;fileName=${fileName}
54                 </param>
55             </result>
56         </action>
57         
58     </package>
59 </struts>

 



    
    
    
    
    
    
    

Struts(三)

标签:

原文地址:http://www.cnblogs.com/ivy-xu/p/5648385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!