Jalal
Jalal

Reputation: 1246

How to execute an action before loading a page in Struts2?

I got a Java Webapp project which uses Struts2 as the controller layer. I need to check user's permissions before doing anything (like opening a web page). I have developed an authorization system which is needed to be executed in struts action. So, how is it possible to execute this action before loading of a page? Should I use interceptors?

Upvotes: 0

Views: 6859

Answers (2)

JB Nizet
JB Nizet

Reputation: 691685

You failed to understand the whole point of an MVC framework like Struts. In a MVC app, you should never have any link or form pointing to a JSP.

You should always point to a URL mapped to the Struts servlet/filter. Then the servlet invokes the appropriate action based on the URL, the action returns the result (the JSP to go to), and Struts forwards to this JSP.

If you point to a JSP directly, then you're bypassing Struts completely. Read http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html

Upvotes: 4

batbaatar
batbaatar

Reputation: 5468

Absolutely, you should implement your own interceptor and put it on top of the other interceptors (not necessarily on the top). If you put it on top, it works before your page execution and can change your return value.

For example:

In your struts.xml

<interceptor name="security"
    class="com.solekia.common.SecurityInterceptor">
</interceptor>

<interceptor-stack name="defaultStack">
    <interceptor-ref name="security"/>
    <interceptor-ref name="exception" />
...
</interceptor-stack>

<global-results>
    <result name="login" type="redirectAction">Login</result>
    <result name="adminlogin" type="redirectAction">AdminLogin</result>
    <result name="error">/error.jsp</result>
</global-results>

Your Interceptor:

public class SecurityInterceptor extends AbstractInterceptor
{
    public static final String CURRENT_USER = "current_user";
    public static final String LOGIN = "login";
    public static final String ADMINLOGIN = "adminlogin";

    public String intercept(ActionInvocation invocation) throws Exception {

        Object user = invocation.getInvocationContext().getSession().get(CURRENT_USER); 
        Object action = invocation.getAction();

        if(action instanceof Login)
        {
            return invocation.invoke();
        }
        else if(action instanceof Register)
        {
            return invocation.invoke();
        }
        else if(action instanceof Pro)
        {
            return invocation.invoke();
        }

        if( user == null )
        {
            invocation.getInvocationContext().getSession().clear();
            return LOGIN;
        }
        else
        {
            if(user instanceof User)
            {
                if(action instanceof AdminLogin)
                    return ADMINLOGIN;
            }

            if(user instanceof Admin)
            {
                if(action instanceof ClientPortal)
                    return LOGIN;
            }           

            synchronized(user)
            {           
                return invokeLocal(invocation) ;
            }
        }
    }

    protected String invokeLocal(ActionInvocation invocation) throws Exception
    {
        return invocation.invoke();     
    }   
}

Hope this helps

Upvotes: 4

Related Questions