[Struts2 Convention] 禁止Convention的UnknownHandler

Convention提供了一个UnknownHandler。如果系统按命名习惯或者Annotation都找不到Action,就会用这个UnknownHandler再去web下或者classpath下找出一个Action

这个Handler不应该用。正式系统里的Action是严肃的,要么按命名习惯推导出,要么用Annotation显式定义好,不能随便乱找

所以,要禁掉这个Handler. 办法分两步:

1.自己写一个空的Handler

public class MyUnknownHandler implements UnknownHandler {

    @Override
    public ActionConfig handleUnknownAction(String namespace, String actionName) throws XWorkException {
        return null;
    }

    @Override
    public Result handleUnknownResult(ActionContext actionContext, String actionName, ActionConfig actionConfig, String resultCode) throws XWorkException {
        return null;
    }

    @Override
    public Object handleUnknownActionMethod(Object action, String methodName) throws NoSuchMethodException {
        return null;
    }

}

2.将这个Handler配到struts.xml中

	<bean type="com.opensymphony.xwork2.UnknownHandler" name="myUnknownHandler" class="MyUnknownHandler"/>
    <unknown-handler-stack>
        <unknown-handler-ref name="myUnknownHandler" />
    </unknown-handler-stack>

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.