Link to home
Start Free TrialLog in
Avatar of icentris
icentris

asked on

Custom Struts 2 Interceptor intercept() method not running

I'm trying to create a Struts 2 Interceptor that will change the request to HTTPS and forward all of the parameters.  I must have made a mistake in setting it up because the intercept() method is not working.  The init() method is running when I start Tomcat up, but when it comes time to run the intercept() method, nothing happens.  My code is below split into sections:
//inside the package tag in the struts.xml:
<interceptors>
     <interceptor name="isSSL" class="com.StrutsApp.Interceptors.SSLInterceptor"/>
</interceptors>
 
// this action is defined within my package tag
<action name="newUserProfile"   class="com.StrutsApp.Login.Actions.NewUserProfileAction">
       <interceptor-ref name="isSSL"/>
       <result type="tiles">CreateUserProfile</result>            
</action>
 
 
//Ineterceptor class
 
 
package com.StrutsApp.Interceptors;
 
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.ActionInvocation;
 
public class SSLInterceptor extends AbstractInterceptor {
    public java.lang.String intercept(ActionInvocation actionInvocation) throws java.lang.Exception {
        //String retVal = (String) actionInvocation.getAction();
        Object obj = actionInvocation.getAction();
        System.out.println("############## We hit the interceptor");    
        return actionInvocation.invoke();
    }
    public void init() {
        System.out.println("########### Interceptor is initialized");
    }
	public void destroy() { }
}

Open in new window

Avatar of summerian
summerian
Flag of Poland image

Is the interceptor defined in the same package that action "newUserProfile"?

Is the action invoked ?
Did you resolve the problem?
Avatar of icentris
icentris

ASKER

Initially the Interceptor class was not in the same package.  I moved it into the same package with the same result.  The action is invoked.
ASKER CERTIFIED SOLUTION
Avatar of summerian
summerian
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
They are defined in the same package...here is my struts.xml file.  I appreciate you looking into this.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.multipart.saveDir" value="/home/paul/Desktop/tmp" />  
 
    <!--<include file="fileupload.xml" />-->
 
    <package name="DatingSite" namespace="/DatingSite" extends="struts-default">
        <result-types>
          <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
        </result-types>
        <interceptors>
           <interceptor name="isSSL" class="com.StrutsApp.Login.Actions.SSLInterceptor"/>
        </interceptors>
        <global-results>
            <result name="login" type="tiles">frontPage</result>
            <result name="login-success">/WEB-INF/pages/Index.jsp</result>
        </global-results>
        <action name="newUserProfile" class="com.StrutsApp.Login.Actions.NewUserProfileAction">
            <interceptor-ref name="isSSL"/>
            <result type="tiles">CreateUserProfile</result>
        </action>
 
 
        
        
        <action name="init" class="com.StrutsApp.Actions.InitAction">
            <result type="tiles">frontPage</result>
        </action>
        <action name="email" class="com.StrutsApp.Email.Actions.EmailSenderAction">
            <result name="ERROR">/jsp/LandingPage.jsp</result>
            <result>/jsp/LandingPage.jsp</result>
        </action>
 
        <action name="CheckAndCreateAccount" class="com.StrutsApp.Login.Actions.CheckAndCreateAccount">
            <result name="error">/jsp/LandingPage.jsp</result>
            <result type="tiles">CreateUserProfile</result>
            <result name="input" type="tiles">CreateUserProfile</result>
        </action>
        <action name="Contact">
            <result name="ERROR"></result>
            <result type="tiles">contacts</result>
        </action>
        <action name="Profile">
            <result name="ERROR"></result>
            <result type="tiles">profile</result>
        </action>
        <action name="SimpleSearch">
            <result name="ERROR"></result>
            <result type="tiles">simpleSearch</result>
        </action>
        <action name="EmailCompose">
            <result name="ERROR"></result>
            <result type="tiles">emailCompose</result>
        </action>
 
 
        <action name="SelectImage">
            <result name="ERROR"></result>
            <result type="tiles">selectImage</result>
        </action>
        <action name="SaveImages" class="com.StrutsApp.ImageUpload.Actions.SaveImage" method="excute">
            <result name="ERROR"></result>
            <result type="tiles">savedImages</result>
        </action>
        <action name="ManageImages">
            <result name="ERROR"></result>
            <result type="tiles">manageImages</result>
        </action>
        <action name="newUserProfile" class="com.StrutsApp.Login.Actions.NewUserProfileAction">
            <result name="ERROR"></result>
            <result type="tiles">CreateUserProfile</result>
        </action>
        <action name="profilePage1" class="com.StrutsApp.Profile.Actions.InitProfilePage1">
            <result name="ERROR"></result>
            <result type="tiles">profilePage1</result>
        </action>
        <action name="saveProfilePage1" class="com.StrutsApp.Profile.Actions.SaveProfilePage1">
            <result name="ERROR"></result>
            <result type="tiles">profilePage2</result>
        </action>
        <action name="saveProfilePage2" class="com.StrutsApp.Profile.Actions.SaveProfilePage2">
            <result name="ERROR"></result>
            <result type="tiles">profilePage3</result>
        </action>
        <action name="saveProfilePage3" class="com.StrutsApp.Profile.Actions.SaveProfilePage3">
            <result name="ERROR"></result>
            <result type="tiles">profilePage4</result>
        </action>
        <action name="saveProfilePage4" class="com.StrutsApp.Profile.Actions.SaveProfilePage4">
            <result name="ERROR"></result>
            <result type="tiles">profilePageFinish</result>
        </action>
    </package>
</struts>

Open in new window

The problem was that I had a duplicate action name.  It happened to be the action that I wanted the interceptor on....Thanks for help!
I ended up fixing it myself only because you didn't have time to look at it after I sent the complete struts.xml.