user1111871
user1111871

Reputation: 147

Struts2 Interceptors and Spring Integration

My question is a bit basic since I'm still learning up on Struts2 and Spring, and their integration. When we define an interceptor in the struts.xml e.g.

<interceptors>
  <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/> 

  <interceptor-stack name="simpleStack">
    <interceptor-ref name="validation">
      <param name="excludeMethods">input,back,cancel,execute</param>
    </interceptor-ref>
  </interceptor-stack>
</interceptors>

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

And if in the applicationContext.xml I have:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
          "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
 <bean id="user" class="struts.model.User"/>
     <bean id="registerUserAction" class="struts.actions.UserInformationAction">
         <property name="userBean" ref="user"/>
    </bean>

    <bean id="chooseUsernamePasswordAction" class="struts.actions.ChooseUsernameAction">
        <property name="userBean" ref="user"/>
    </bean>    
</beans>

Without the interceptor defined the user bean is persistent across both actions (registerUserAction -> JSP -> chooseUsernamePasswordAction) and i can access the properties. Once the interceptor is introduced it looks like the values are nulled out.

  1. First from what i read it looks like i might be doing this all wrong. i.e. with Spring integrated all these items are in singleton default mode which might not be right since multiple users will be registering and so each would have to have their own Action/User beans.
  2. What is happening or needs to be done to pass information across and have the interceptors working right.

Upvotes: 0

Views: 2446

Answers (3)

softmage99
softmage99

Reputation: 807

We also using same project structure

Struts2+Spring+Hibernate

Before using the Struts interceptor, we can able to pass the parameter

But after using the interceptor, we are not able to send the parameters (values are null)

To avoid that, use params interceptor, then you can pass the parameters

e.g.:

<action name="doUpload_valid1" method="importExcel" class="validationAction">
  <interceptor-ref name="params"/>
  <interceptor-ref name="fileUpload"/>

  <result name="error">jsp/cmdbValidPage.jsp</result> 
  <result name="success">jsp/testJob.jsp</result> 
</action>

Upvotes: 0

Alban
Alban

Reputation: 1943

Have a look at struts-default.xml. Now check out the interceptor stack named defaultStack. At the end of the document, this stack is set as the default interceptor stack. As Dave Newton explained, all the magic of struts 2 comes from its interceptors.

Basically, when you set your stack as default stack, you replace a stack that contains 17 interceptors by one that contains only 1. By doing so you gave up many struts 2 functionalities like parameter injection for example, provided by the interceptor param.

Also, note that the validation interceptor is included in defaultStack, but with different parameter excludeMethods.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160191

If you are explicitly defining your actions in the Spring config they must be defined as scope="prototype". It's relatively unusual you'd need to define them manually, unless you specifically want to use XML configuration for everything, like service injection and so on.

It's not entirely clear to me what your intent is with the user bean. You may use a Spring session-scoped bean, available with the Spring web contexts. I'm not sure how necessary that is; I've usually done it manually and retrieved the bean from the session when I actually need it. Likely doesn't matter.

Note also that your stack eliminates essentially all Struts 2 functionality, like the conversion of form parameters to action properties. This may be what you intend, but it's unlikely.

Upvotes: 1

Related Questions