当我使用Struts 2通配符配置时如何返回404错误? [英] How to return an 404 error when I use Struts 2 wildcard configuration?

查看:151
本文介绍了当我使用Struts 2通配符配置时如何返回404错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用struts 2构建一个网站。
这是我的struts.xml的剪辑:

I am building a website using struts 2 . Here is a clip of my "struts.xml":

<action name="*" class="com.domain.actions.UserAction" method="{1}">        
    <result name="myresult">/Pages/myresult.jsp</result>
    <!-- there are many other results -->
</action>

现在,我遇到了问题。

当我访问一个动作时,我没有设计,如aaabbb,服务器将返回500错误。

因为通配符配置,struts 2将尝试调用类com.domain.actions的aaabbb方法.UserAction,但aaabbb方法不存在。

但是,从逻辑上讲,返回404错误更好。

如何在这些情况下返回404错误同时使用通配符配置?

Now, I got a problem.
When I visit an action I didn't design, such as "aaabbb", the server will return a 500 error.
because of the wildcard configuration, struts 2 will try to call the "aaabbb" method of class "com.domain.actions.UserAction", but the "aaabbb" method not exsits.
But, by logically, return a 404 error is better.
How can I return a 404 error in these situation and use wildcard configuration at the same time ?

推荐答案

我不是Struts2专家,但你可以试试这个:

I'm no Struts2 expert, but you might try this:

<global-exception-mappings>
  <exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>

然后有一个返回404的错误操作。

Then have an "error" action that returns the 404.

这篇关于当我使用Struts 2通配符配置时如何返回404错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆