Struts2-转到无效URL不会重定向到映射的404页面或不提供默认的不操作消息 [英] Struts2 - Going to an Invalid URL does not redirect to mapped 404 page or give the default no action message

查看:165
本文介绍了Struts2-转到无效URL不会重定向到映射的404页面或不提供默认的不操作消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道可能要进行哪些配置更改,但由于某种原因,转到了一个无效URL,该URL映射为no struts2操作,仅给出空白页.它不会重定向到映射的404"页面,甚至不会像以前那样显示没有为名称空间[/]和操作名称[]映射任何操作".我正在尝试使404页面正常工作,但是我不知道这是怎么回事.在我的web.xml中,我有:

I do not know what configuration changes I may have done to cause this, but for some reason, going to an invalid URL which maps to no struts2 action, only gives a blank page. It does not redirect to the Mapped 404 page, and it does not even display the "There is no Action mapped for namespace [/] and action name []" like it used to. I am trying to get my 404 page working, but I have no clue what is going on here. In my web.xml I have:

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

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

您对为什么使用无效的URL和操作提供空白页有任何想法吗? 感谢您的帮助

Have any idea as to why blank pages are being served with invalid URL's and actions? I appreciate the help

推荐答案

最后-在深入研究此问题之后,我将找到一个很好的解决方案,以解决这个问题!

Finally - After deep searching about this issue, I'll find a great solution to how get this works as probably!

第一个:,您需要在 struts.xml 中添加 com.opensymphony.xwork2.UnknownHandler bean:

1st: you need to add the com.opensymphony.xwork2.UnknownHandler bean in your struts.xml:

<bean type="com.opensymphony.xwork2.UnknownHandler" name="handler" class="com.path.to.your.InvalidRequests"/>


第二::设置一个操作以引用页面未找到的行为.


2nd: setup an action to refer into the page not found behavior.

<action name="pageNotFound" class="com.path.to.your.PageNotFound" method="execute">
   <result name="success">/jsps/404.jsp</result>
</action>


第三:,您需要在自定义处理程序中定义 com.opensymphony.xwork2.config.entities.ActionConfig ,并检查操作配置列表中是否已存在该操作


3rd: in your custom handler you need to define your com.opensymphony.xwork2.config.entities.ActionConfig and check if the action is already exists in your action configuration lists

public class InvalidRequests implements UnknownHandler {
@Override
public ActionConfig handleUnknownAction(String namespace, String actionName) throws XWorkException {
    ConfigurationManager configurationManager = Dispatcher.getInstance().getConfigurationManager();
    RuntimeConfiguration runtimeConfiguration = configurationManager.getConfiguration().getRuntimeConfiguration();
    ActionConfig actionConfig = runtimeConfiguration.getActionConfig(namespace, actionName);
    if(actionConfig == null) { // invalid url request, and this need to be handled
        actionConfig = runtimeConfiguration.getActionConfig("", "pageNotFound");
    }
    return actionConfig;
}
// ... also you need to implements handleUnknownResult, handleUnknownActionMethod
}


请记住,您需要覆盖两个方法 handleUnknownResult handleUnknownActionMethod ,并且这两个方法都返回 null .


Keep in mind you need to override the both of methods handleUnknownResult and handleUnknownActionMethod and the both of methods return null.

这篇关于Struts2-转到无效URL不会重定向到映射的404页面或不提供默认的不操作消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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