Struts 2消息存储拦截器不使用字段错误(使用Hibernate验证)

时间:2012-12-19 15:21:10

标签: hibernate struts2 interceptor interceptorstack

我有一个带有一些文本字段的Jsp,这些是使用Hibernate验证注释(动作中的@Valid或@NotNull)验证,除了一个(输入图像文件),它使用默认的Struts2验证进行验证(使用ActionName-validation.xml) 。 我想在提交的表单无效时重定向到另一个操作(当textfields为null时),并且我想存储字段错误。

我试过这个:

<interceptors>
    <interceptor name="SessionCheckInterceptor" class="util.SessionCheckInterceptor"/>
        <interceptor-stack name="mySessionValidationStack">              
            <interceptor-ref name="defaultStackHibernate" />
            <interceptor-ref name="SessionCheckInterceptor" />
        </interceptor-stack>      
</interceptors> 

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

<action name="insert" class="actions.InsertAction" >       
    <interceptor-ref name="mySessionValidationStack">
        <param name="fileUpload.allowedTypes">image/png</param>
    </interceptor-ref> 
    <interceptor-ref name="store">
        <param name="operationMode">STORE</param>
    </interceptor-ref>
    <interceptor-ref name="defaultStack" />
    <result name="success" type="tiles" >baseLayout</result>
    <result name="error" type="redirectAction" >
        <param name="actionName">showinsertform</param>
    </result>
    <result name="input" type="redirectAction" >
        <param name="actionName">showinsertform</param>
    </result>
</action>

<action name="showinsertform" class="actions.ShowInsertFormAction" > 
    <interceptor-ref name="mySessionValidationStack" />
    <interceptor-ref name="store">
        <param name="operationMode">RETRIEVE</param>
    </interceptor-ref>
    <result name="success" type="tiles" >insert</result>
    <result name="error" type="tiles" >baseLayout</result>
</action> 

但是当我对表单进行求和时,重定向成功而没有显示我的字段错误消息。也许我设置错误的拦截器?使用hibernate我需要覆盖一些东西? 如果我尝试在操作中设置手动错误消息(使用addActionError),它们可以工作! 是否可以将hibernate错误字段消息存储在会话中?

1 个答案:

答案 0 :(得分:0)

Hibernate插件struts-plugin.xml这里你使用的是 defaultStackHibernate ,它不包括在struts-default.xml中定义的商店拦截器,你应该在使用它之前将商店拦截器包含到你的自定义拦截器堆栈中

<?xml version="1.0" encoding="UTF-8" ?>

    

<package name="hibernate-default" extends="struts-default" abstract="true">


    <interceptors>

        <interceptor name="hibernateSessionInterceptor" class="com.googlecode.s2hibernate.struts2.plugin.interceptors.SessionTransactionInjectorInterceptor" />
        <interceptor name="hibernateValidatorInterceptor" class="com.googlecode.s2hibernate.struts2.plugin.s2hibernatevalidator.interceptor.HibernateValidatorInterceptor">
            <param name="excludeMethods">input,prepare,back,cancel,browse</param>
        </interceptor>

        <interceptor-stack name="defaultStackNoValidation">
           <interceptor-ref name="exception"/>
            <interceptor-ref name="alias"/>
            <interceptor-ref name="servletConfig"/>
            <interceptor-ref name="prepare"/>
            <interceptor-ref name="i18n"/>
            <interceptor-ref name="chain"/>
            <interceptor-ref name="debugging"/>
            <interceptor-ref name="profiling"/>
            <interceptor-ref name="scopedModelDriven"/>
            <interceptor-ref name="modelDriven"/>
            <interceptor-ref name="fileUpload"/>
            <interceptor-ref name="checkbox"/>
            <interceptor-ref name="staticParams"/>
            <interceptor-ref name="params">
              <param name="excludeParams">dojo\..*,^struts\..*</param>
            </interceptor-ref>
            <interceptor-ref name="conversionError"/>
            <interceptor-ref name="workflow">
                <param name="excludeMethods">input,back,cancel,browse</param>
            </interceptor-ref>
        </interceptor-stack>

        <interceptor-stack name="basicStackHibernate">
            <interceptor-ref name="basicStack"/>
            <interceptor-ref name="hibernateSessionInterceptor"/>
        </interceptor-stack>

        <interceptor-stack name="defaultStackHibernate">
            <interceptor-ref name="defaultStackNoValidation"/>
            <interceptor-ref name="hibernateSessionInterceptor"/>
            <interceptor-ref name="hibernateValidatorInterceptor"/>
        </interceptor-stack>

        <interceptor-stack name="defaultStackHibernateStrutsValidation">
            <interceptor-ref name="defaultStack"/>
            <interceptor-ref name="hibernateSessionInterceptor"/>
        </interceptor-stack>

    </interceptors>

    <default-interceptor-ref name="defaultStackHibernate"/><!--

    <global-exception-mappings>
        <exception-mapping exception="org.hibernate.validator.InvalidStateException" result="input" name="input"/>
    </global-exception-mappings>

--></package>


<package name="hibernateManager" namespace="/hibernateManager" extends="struts-default">

    <interceptors>
        <interceptor name="hibernatePluginInterceptor" class="com.googlecode.s2hibernate.struts2.plugin.interceptors.InternalHibernatePluginInterceptor" />
        <interceptor name="managerInterceptor" class="com.googlecode.s2hibernate.struts2.plugin.interceptors.HibernateManagementInterceptor" />
    </interceptors>

    <default-class-ref class="com.googlecode.s2hibernate.struts2.plugin.actions.HibernateManagementAction"/>

    <action name="" method="index">
        <interceptor-ref name="hibernatePluginInterceptor"/>
        <interceptor-ref name="managerInterceptor"/>
        <interceptor-ref name="validationWorkflowStack"/>
        <result>/WEB-INF/temp/hibernatePlugin/management.jsp</result>
        <result name="input">/WEB-INF/temp/hibernatePlugin/management.jsp</result>
    </action>
    <action name="*" method="{1}">
        <interceptor-ref name="hibernatePluginInterceptor"/>
        <interceptor-ref name="managerInterceptor"/>
        <interceptor-ref name="validationWorkflowStack"/>
        <result>/WEB-INF/temp/hibernatePlugin/management.jsp</result>
        <result name="input">/WEB-INF/temp/hibernatePlugin/management.jsp</result>
    </action>

</package>