struts 设置默认 action

struts 如何设置默认action呢?

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

在struts.xml中添加:

 

<!-- 404页面 -->
		<default-action-ref name="notFound" />
<action name="notFound" class="com.common.action.error.NotFoundErrerAction">
			<result name="success">/error/not_found.jsp</result>
		</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 过滤器配置改为:

 

<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>/*</url-pattern>
  </filter-mapping>

 

 

修改之后的效果:

 

 

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

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

 

<error-page>
            <error-code>404</error-code>
            <location>/error/not_found.jsp</location>
        </error-page>

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

 

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值