Domanda

I have a Jsp with some textfields, these are validate using Hibernate validation annotation (@Valid or @NotNull in the action) except one (input image file) that is validate with default Struts2 validation (using ActionName-validation.xml). I want to redirect to another action when the submitted form is invalid (when textfields are null), and I want to store fields error.

I tried with this:

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

But when I sumbit the form, the redirect succeed without showing me fields error messages. Maybe I'm setting wrong the interceptor? Using hibernate I need to override something? If I try to set manually error messages in the action (with addActionError), they works! Can it be that hibernate error fields messages are not stored in the session?

È stato utile?

Soluzione

Hibernate plugin struts-plugin.xml here you are using defaultStackHibernate which is not including store interceptor which is defined in struts-default.xml you should include store interceptor to your custom interceptor stack before using it.

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top