如何让struts2和servlet的共存
1个回答
推荐于2016-01-12 · 知道合伙人数码行家
关注
展开全部
解决方法目前有四种:
方法1:
统一在servlet后面加上.servlet结尾(包括web.xml配置文件中和页面上使用servlet的地方)
方法2:
在struts.xml中的<struts>的节点下面添加struts2处理的请求后缀 常量:
<!--
该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。
-->
<constant name="struts.action.extension" value="action" />
方法3:
在web.xml中修改struts2拦截页面请求的配置:
原:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
现:
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/user/*</url-pattern>
</filter-mapping>
servlet的请求路径不必改变
方法4:
自定义filter对servlet进行过滤:
##############################################
# 该类是定义了一个过滤器(随便你放在某个包下面),当既用到struts2又用到servlet时,
# 必须用该过滤器对所有的servlet做一次过滤,否则servlet将无法正常运行。
# 同时在web.xml中,在定义struts的核心控制器的过滤器前面添加该过滤器,添加代码如下:
* <filter>
* <filter-name>redisp</filter-name>
* <filter-class>filters.ReDispatcherFilter(该过滤器的类名)</filter-class>
* <init-param>
* <param-name>includeServlets</param-name>
* <param-value>所配置的servlet,注意是那url-pattern</param-value>
* </init-param>
* </filter>
* <filter-mapping>
* <filter-name>redisp</filter-name>
* <url-pattern>/*</url-pattern>
* </filter-mapping>
*该类也可以直接继承StrutsPrepareAndExecuteFilter的
###########################################################
public class ReDispatcherFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest)req;
String target = request.getRequestURI();
target = target.lastIndexOf("?")>0
?target.substring(target.lastIndexOf("/")+1,target.lastIndexOf("?")-target.lastIndexOf("/"))
:target.substring(target.lastIndexOf("/")+1);
System.out.println(target);
if(this.includes.contains(target))
{
RequestDispatcher rdsp = request.getRequestDispatcher(target);
System.out.println("go..............."+rdsp);
rdsp.forward(req, resp);
}
else
chain.doFilter(req, resp);
}
private ArrayList<String> includes = new ArrayList<String>();
public void init(FilterConfig config) throws ServletException {
this.includes.addAll( Arrays.asList(config.getInitParameter("includeServlets").split(",")));
}
}
方法1:
统一在servlet后面加上.servlet结尾(包括web.xml配置文件中和页面上使用servlet的地方)
方法2:
在struts.xml中的<struts>的节点下面添加struts2处理的请求后缀 常量:
<!--
该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。
-->
<constant name="struts.action.extension" value="action" />
方法3:
在web.xml中修改struts2拦截页面请求的配置:
原:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
现:
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/user/*</url-pattern>
</filter-mapping>
servlet的请求路径不必改变
方法4:
自定义filter对servlet进行过滤:
##############################################
# 该类是定义了一个过滤器(随便你放在某个包下面),当既用到struts2又用到servlet时,
# 必须用该过滤器对所有的servlet做一次过滤,否则servlet将无法正常运行。
# 同时在web.xml中,在定义struts的核心控制器的过滤器前面添加该过滤器,添加代码如下:
* <filter>
* <filter-name>redisp</filter-name>
* <filter-class>filters.ReDispatcherFilter(该过滤器的类名)</filter-class>
* <init-param>
* <param-name>includeServlets</param-name>
* <param-value>所配置的servlet,注意是那url-pattern</param-value>
* </init-param>
* </filter>
* <filter-mapping>
* <filter-name>redisp</filter-name>
* <url-pattern>/*</url-pattern>
* </filter-mapping>
*该类也可以直接继承StrutsPrepareAndExecuteFilter的
###########################################################
public class ReDispatcherFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest)req;
String target = request.getRequestURI();
target = target.lastIndexOf("?")>0
?target.substring(target.lastIndexOf("/")+1,target.lastIndexOf("?")-target.lastIndexOf("/"))
:target.substring(target.lastIndexOf("/")+1);
System.out.println(target);
if(this.includes.contains(target))
{
RequestDispatcher rdsp = request.getRequestDispatcher(target);
System.out.println("go..............."+rdsp);
rdsp.forward(req, resp);
}
else
chain.doFilter(req, resp);
}
private ArrayList<String> includes = new ArrayList<String>();
public void init(FilterConfig config) throws ServletException {
this.includes.addAll( Arrays.asList(config.getInitParameter("includeServlets").split(",")));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询