整了两天,终于找到一个比较满意的答案了:如何让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的更多相关文章

  1. struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法

    struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法

  2. struts2 action 页面跳转

    struts2 action 页面跳转 标签: actionstruts2redirect 2013-11-06 16:22 20148人阅读 评论(0) 收藏 举报 (1)type="di ...

  3. Java Hour 32 Weather ( 5 ) struts2 – Action class

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 32 Struts2 Action 1 将action 映射到 ac ...

  4. Struts2 Action接收表单参数

    struts2 Action获取表单传值    1.通过属性驱动式    JSP:        <form action="sys/login.action" method ...

  5. struts2 action result type类型

    struts2 action result type类型 1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony. ...

  6. struts2 action通配符

    首先,看一个struts2的配置文件: <package name="actions" extends="struts-default" namespac ...

  7. 第三章Struts2 Action中动态方法调用、通配符的使用

    01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...

  8. [Struts2] Action Implements SessionAware

    struts2 的Action中若希望访问Session对象,可采用两种方式: 1.从ActionContext中获取: 2.实现SessionAware接口. 1.从ActionContext中获取 ...

  9. Struts2 Action下面的Method调用方法

    1. 在struts.xml中加入<constant name="struts.enable.DynamicMethodInvocation" value="tru ...

随机推荐

  1. C++学习笔记15:操作符重载的函数原型列表(推荐)

    //普通四则运算 friend A operator +(const A & lhs, const A & rhs); friend A operator -(const A & ...

  2. Yii 中的createUrl和redirectUrl

    Yii  这两个url表示方法容易混淆,区别如下: $this->CreateUrl('控制器/方法',get参数); $this->CreateUrl('方法');           ...

  3. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128) 解决办法

    最近在用Python处理中文字符串时,报出了如下错误: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ...

  4. Linux下使用fdisk发现磁盘空间和使用mount挂载文件系统

    若在安装Linux系统时没有想好怎么使用服务器,开始时只分了一部分给根目录.后面需要再使用时,可以通过几下一步进行分区和挂载文件系统. 看磁柱数使用情况 fdisk -l Disk /dev/sda: ...

  5. emulator: ERROR: Unable to load VM from snapshot. The snapshot has been saved for a different hardware configuration.

    emulator: ERROR: Unable to load VM from snapshot. The snapshot has been saved for a different hardwa ...

  6. AndroidStudio导入Android-PullToRefresh

    方法已经写到我的微信公众号中,公众号二维码在下面 本人联系方式: 更多精彩分享,可关注我的微信公众号: 若想给予我分享更多知识的动力,请扫描下面的微信打赏二维码,谢谢!: 微信号:WeixinJung ...

  7. leetcode 111 Minimum Depth of Binary Tree ----- java

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  8. Activity——思维导图

  9. HDU-3853 LOOPS(概率DP求期望)

    题目大意:在nxm的方格中,从(1,1)走到(n,m).每次只能在原地不动.向右走一格.向下走一格,概率分别为p1(i,j),p2(i,j),p3(i,j).求行走次数的期望. 题目分析:状态转移方程 ...

  10. kuangbin_ShortPath L (POJ 2502)

    dij部分还是跟模板差不多的 但是这题的难点是处理输入 或者说理解题意 事实上每个点之间都是可以走的......WA了好几发就因为没意识到同一条路线上的各个站点之间居然也可以走得比车子快.... PS ...