在Struts2 上面写的一个将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);
}
}
<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>
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配置
<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>
本文介绍了一个自定义的Struts2拦截器,该拦截器能够将所有的JSP页面转移到WEB-INF目录下,通过修改配置实现路径的统一管理。此方案有助于增强项目的安全性。

被折叠的 条评论
为什么被折叠?



