struts 设置默认 action

简介:

struts 如何设置默认action呢?

我要达到的目的是:访问不存在的action时自动跳转到默认的action

在struts.xml中添加:

 

Java代码   收藏代码
  1. <!-- 404页面 -->  
  2.         <default-action-ref name="notFound" />  
  3. <action name="notFound" class="com.common.action.error.NotFoundErrerAction">  
  4.             <result name="success">/error/not_found.jsp</result>  
  5.         </action>  

 效果如下:

 action  aaa/xxx.action 不存在,所以自动跳转到了notFound.

 

 

但是现在有一个问题,如果我的url是http://localhost:8082/shop_goods/acc 时,界面如下:

 这是为什么呢?

我检查我的web.xml发现struts 过滤器配置如下:

<filter>

    <filter-name>struts2</filter-name>

    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>struts2</filter-name>

    <url-pattern>*.action</url-pattern>

  </filter-mapping>

 

所以struts只会处理url后缀名为action的,比如xxx.action,abc.action.

解决方法:struts 过滤器配置改为:

 

Xml代码   收藏代码
  1. <filter>  
  2.     <filter-name>struts2</filter-name>  
  3.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  4.   </filter>  
  5. <filter-mapping>  
  6.     <filter-name>struts2</filter-name>  
  7.     <url-pattern>/*</url-pattern>  
  8.   </filter-mapping>  

 

 

修改之后的效果:

 

 

如果struts过滤器非要使用*.action呢?

那么需要在web.xml中添加:

 

Xml代码   收藏代码
  1. <error-page>  
  2.             <error-code>404</error-code>  
  3.             <location>/error/not_found.jsp</location>  
  4.         </error-page>  

 这样,访问http://localhost:8082/shop_goods/acc 也会自动跳转到/error/not_found.jsp

 

注意:上述页面(/error/not_found.jsp)不能有struts 标签

相关文章
|
前端开发 Java 调度