![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
Struts2想在global Package里拦截404请求,求配置代码,如果可能,最好跳转至一个ACTION里来处理事件
讨厌web.xml配置,然后同求java.lang.NoSuchMethodException(Struts2)拦截处理跳转的方式,高分哦!!!...
讨厌web.xml配置,然后同求java.lang.NoSuchMethodException(Struts2)拦截处理跳转的方式,高分哦!!!
展开
3个回答
展开全部
如果讨厌在web.xml配置的话,那只能在struts.xml里只能对(错误类型)进行拦截。
例如:
<!-- 全局异常 -->
<global-exception-mappings>
<exception-mapping result="nullExce" exception="java.lang.NullPointerException"></exception-mapping>
</global-exception-mappings>
<!--全局异常跳转-->
<global-results>
<result name="nullExce">/500page.jsp</result>
</global-results>
<!-- action里异常跳转 -->
<action name="AAA_*" class="com.test.aaa" method="{1}">
<result name="nullExce">AAA_index</result>
</action>
web.xml里加入错误页面代码配置,就可以实现你需要的拦截404(错误代码)。
例:
<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>
<error-page>
<error-code>500</error-code>
<location>/500page.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404page.jsp</location>
</error-page>
例如:
<!-- 全局异常 -->
<global-exception-mappings>
<exception-mapping result="nullExce" exception="java.lang.NullPointerException"></exception-mapping>
</global-exception-mappings>
<!--全局异常跳转-->
<global-results>
<result name="nullExce">/500page.jsp</result>
</global-results>
<!-- action里异常跳转 -->
<action name="AAA_*" class="com.test.aaa" method="{1}">
<result name="nullExce">AAA_index</result>
</action>
web.xml里加入错误页面代码配置,就可以实现你需要的拦截404(错误代码)。
例:
<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>
<error-page>
<error-code>500</error-code>
<location>/500page.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404page.jsp</location>
</error-page>
展开全部
<global-exception-mappings>
<exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<global-results>
<result name="exception" type="redirect">/page/error.html</result>
</global-results>
<exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<global-results>
<result name="exception" type="redirect">/page/error.html</result>
</global-results>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<default-action-ref name="resourceNotExisted" />
<action name="resourceNotExisted">
<result>/WEB-INF/404.jsp</result>
</action>
<action name="resourceNotExisted">
<result>/WEB-INF/404.jsp</result>
</action>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询