用Struts 2的拦截器将对JSP页面的访问转到WEB-INF下

在Struts2(struts-2.0.6+jdk1.6)上面写的一个拦截器,它将对JSP页面的访问全部转移到WEB-INF下,以下是具体代码和配置


package com.stockvote.interceptor;



import java.util.Map;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class WebInfInterceptor implements Interceptor ...{
  private static String PathPrefix = "/WEB-INF/";
  private static final String LOCATION = "location";
 
  public void destroy() ...{

  }

  public void init() ...{
    if (PathPrefix.endsWith("/")) ...{
        PathPrefix = PathPrefix.substring(0, PathPrefix.length() - 1);
    }
  }

  public String intercept(ActionInvocation arg0) throws Exception ...{

    Map map = arg0.getProxy().getConfig().getResults();

    for (java.util.Iterator iter = map.values().iterator(); iter.hasNext();) ...{
        ResultConfig rcg = (ResultConfig) iter.next();
        transferLocation(rcg);
    }

    return arg0.invoke();
  }

  public void transferLocation(ResultConfig rcg) ...{

    Map map = rcg.getParams();

    if (!map.containsKey(LOCATION)) ...{
        return;
    }

    //
    String location = (String) map.get(LOCATION);

    if (location.startsWith(PathPrefix)) ...{
        return;
    }

    if (!location.startsWith("/")) ...{
        location = "/" + location;
    }

    location = PathPrefix + location;

    map.put(LOCATION, location);
  }
}
下面是xml配置,定义默认拦截器:
  <interceptors>
  <interceptor name="WebInfoPath" class="com.stockvote.interceptor.WebInfointerceptor"></interceptor>
       
        <interceptor-stack name="defaultStack1">
        <!--interceptor-ref name="log"></interceptor-ref!-->
        <interceptor-ref name="defaultStack"></interceptor-ref>
        <interceptor-ref name="WebInfoPath"></interceptor-ref>
       
        </interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack1"></default-interceptor-ref>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值