0

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

我试过这个:

<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> 

但是当我提交表单时,重定向成功而没有显示字段错误消息。也许我设置错误的拦截器?使用休眠我需要覆盖一些东西?如果我尝试在操作中手动设置错误消息(使用 addActionError),它们会起作用!会不会是休眠错误字段消息没有存储在会话中?

4

1 回答 1

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>

于 2012-12-19T17:12:24.630 回答