Struts2 - Action no cache
整了两天,终于找到一个比较满意的答案了:如何让action不被浏览器缓存
写一个interceptor:
package com.my.interceptor; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; @SuppressWarnings("serial")
public class CacheInterceptor extends AbstractInterceptor { @Override
public String intercept(ActionInvocation invocation) throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setHeader("Expires","0"); return invocation.invoke();
} }
在struts2中如果是普通的action(即没有加入convertion-plugin的),只需要在struts.xml直接配置interceptor即可。
但如果使用了convertion-plugin和rest-plugin,因为它是自动化完成action配置了,这时在struts.xml里所定义的interceptor就不起作用了。
必需这样写struts.xml:
<!-- 设置默认package -->
<constant name="struts.convention.default.parent.package" value="default"></constant> <package name="default" namespace="/" extends="rest-default">
<interceptors>
<interceptor name="cacheInterceptor" class="com.my.interceptor.CacheInterceptor" />
<interceptor-stack name="cacheStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="paramsPrepareParamsStack"></interceptor-ref>
<interceptor-ref name="cacheInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="cacheStack"></default-interceptor-ref>
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
</package>
关键点有以下两点:
1)constant的value=package的name,如上。
2)cacheStack节点可以看到除了自定的cacheInterceptor外,还有两个stack:defaultStack和paramsPrepareParamsStack,这两个必不可少,否则如果action中使用了ModelDriven的都会失效。
失效原因:
defaultStack的作用是普通的param传递,paramsPrepareParamsStack是ModelDriven的对象传递。
如果在使用了自定的interceptor后,这两个stack将不再被调用,所以要手工加上去。
Struts2 - Action no cache的更多相关文章
- struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
- struts2 action 页面跳转
struts2 action 页面跳转 标签: actionstruts2redirect 2013-11-06 16:22 20148人阅读 评论(0) 收藏 举报 (1)type="di ...
- Java Hour 32 Weather ( 5 ) struts2 – Action class
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 32 Struts2 Action 1 将action 映射到 ac ...
- Struts2 Action接收表单参数
struts2 Action获取表单传值 1.通过属性驱动式 JSP: <form action="sys/login.action" method ...
- struts2 action result type类型
struts2 action result type类型 1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony. ...
- struts2 action通配符
首先,看一个struts2的配置文件: <package name="actions" extends="struts-default" namespac ...
- 第三章Struts2 Action中动态方法调用、通配符的使用
01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...
- [Struts2] Action Implements SessionAware
struts2 的Action中若希望访问Session对象,可采用两种方式: 1.从ActionContext中获取: 2.实现SessionAware接口. 1.从ActionContext中获取 ...
- Struts2 Action下面的Method调用方法
1. 在struts.xml中加入<constant name="struts.enable.DynamicMethodInvocation" value="tru ...
随机推荐
- 关于VS中更改栈和堆空间的大小
编号:1008时间:2016年4月12日17:01:38功能:关于VS中更改栈和堆空间的大小 URL:http://blog.csdn.net/icerock2000/article/details/ ...
- Day02_JAVA语言基础第二天
1.常量(理解) 1.概念 在程序运行过程中,其值不会发生改变的量 2.分类(掌握) A .字面值常量 整数常量:1,2,-3 小数常量:2.3,-232.3 字符常量:'A' 字符串 ...
- EASYRECOVERY_3.3.29包含注册机、都教授数据恢复含注册码
用EASYRECOVERY恢复过U盘,和回收站永久清空的东西.效果很好.但疑惑doc,docx,后者恢复效果特别好 都教授没用过,别人买的,没用,很贵. 每次用,都找不着,还要重新淘宝买.这次分享给大 ...
- 高逼格的画图:VIM原来可以这样玩
在Linux上其实并不缺少画图软件(比如yEd等),那么为什么还需要用VIM来画图: 更轻,不需要安装太多东西 更小,就是一些文本,比图片什么的小多了,使用起来也更简单 更有逼格 那么我们该怎么做呢? ...
- HTML <area> 标签 带有可点击区域的图像映射(图像映射指的是带有可点击区域的图像)
例子: <img src="planets.gif" width="145" height="126" alt="Plane ...
- 关于Toad连接DB2的sqlstate=08001错误
新装的centos6.3+db29.7,数据库导入完了的之后用Toad连接访问之的时候出错了. DB2 Database Error: ERROR [08001] [IBM] SQL30081N A ...
- window7资源管理器一直重启(百度知道找到可用)
今天我的机器也出现这种问题:我的解决方式是,在开机时选择系统修复选项中的进入命令行方式(尝试过用安全模式,文件被占用,现象一样),然后cd C:\Users\Administrator\AppData ...
- 织梦DedeCMS"当前位置"去除最后一个 > 符号的方法
首先找到根目录下面的include 目录,然后找到 typelink.class.php 文件, 再查找到 GetPositionLink 方法 下面的 return $this->valueP ...
- GCC编译器
详见<gcc中文手册> 编译过程 预处理器cpp 编译器gcc 汇编器as 链接器linker file.c -------------> file.i ----------- ...
- 打开eclipse报错:发现了以元素 'd:skin' 开头的无效内容。此处不应含有子元素。
[错误] 打开eclipse报错:发现了以元素 ‘d:skin’ 开头的无效内容.此处不应含有子元素. [具体报错信息] Error parsing D:\Android-sdks\system-im ...