验证失败但它仍在struts2上执行

时间:2011-11-04 18:14:12

标签: struts2

我正在对现有应用进行修改。我的问题是即使表单提交验证失败,它仍然执行execute方法。

我的struts文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!-- <include file="com/paritysys/util/struts.xml" /> -->

<constant name="struts.url.includeParams" value="none" />
<constant name="struts.action.extension" value="html,action" />

<package name="public" extends="struts-default">
    <interceptors>
        <interceptor name="websiteOnline"
            class="parity.action.website.OnlineInterceptor" />
        <interceptor name="websiteLogin"
            class="parity.action.website.LoginInterceptor" />

        <interceptor-stack name="appStack">
            <interceptor-ref name="validation">
                <param name="excludeMethods">input,back,cancel,browse</param>
            </interceptor-ref>
            <!-- <interceptor-ref name="paritySessionStack"/> -->
            <interceptor-ref name="websiteOnline" />
            <interceptor-ref name="websiteLogin" />
        </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="appStack" />

    <global-results>
        <result name="login" type="redirectAction">
            <param name="actionName">index</param>
        </result>
        <result name="exception" type="freemarker">/public/error.html.ftl</result>
        <result name="error" type="freemarker">/public/error.html.ftl</result>
        <result type="freemarker" name="maintenance">/public/maintenance.html
        </result>
        <result type="freemarker" name="pre-offline">/public/pre-offline.html
        </result>
        <result type="freemarker" name="post-offline">/public/post-offline.html
        </result>
    </global-results>

    <action name="index" class="parity.action.website.LoginAction">
        <result type="freemarker" name="success">/public/index.html.ftl</result>
    </action>

    <action name="login" class="parity.action.website.SubmitLoginAction">
        <result type="freemarker" name="success">/public/questionnaire.html.ftl
        </result>
        <result type="freemarker" name="input">/public/index.html.ftl</result>
    </action>

    <action name="submit" class="parity.action.website.SubmitQuestionnaireAction">
        <result type="freemarker" name="success">/public/thanks.html.ftl
        </result>
        <result type="freemarker" name="input">/public/questionnaire.html.ftl
        </result>
    </action>

    <action name="whereIsMyId" class="parity.action.website.LoginAction">
        <result type="freemarker" name="success">/public/whereIsMyId.html.ftl
        </result>
    </action>

    <action name="loadCollegeFinder" class="parity.action.website.LoadCollegeFinderAction">
        <result type="freemarker" name="success">/public/college_finder.html.ftl
        </result>
    </action>

    <action name="findCollege" class="parity.action.website.FindCollegeAction">
        <result type="freemarker" name="success">/public/college_finder.html.ftl
        </result>
        <result type="freemarker" name="input">/public/college_finder.html.ftl
        </result>
    </action>
</package>

我的动作类代码

public void validate() {
        logger.debug("validate fired");
Bla bla bla
addFieldError("username","error");

        if (hasFieldErrors()) {
            logger.debug("Field errors is true");
        }
    }

    public String execute() throws Exception {
        logger.debug("execute firing");

        return result;
    }

为什么会发生这种情况的任何想法?出于某种原因,即使login.action失败,它仍然会向下发送成功并继续前进。

1 个答案:

答案 0 :(得分:2)

您的拦截器堆栈不包含“工作流”拦截器堆栈,这就决定了在验证失败时要做什么。

就此而言,它不包括“params”拦截器,这是在动作上设置参数的方式,因此它无论如何都不会工作。你不能随意删除拦截器 - 这是S2获得大部分功能的地方。 See the interceptor docs.

此外,您可以设置默认结果类型 - 如果大多数内容都是FreeMarker结果而不是一遍又一遍地输入,您可能希望这样做。