You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ML...@abusinessware.com on 2007/04/23 08:36:50 UTC

[S2][interceptors]Inserted my own interceptor => no more params value

Hi all,

I try to insert my "AuthentificatedInterceptor" in the stack, but now 
nothing works.

First, I had a login page, with login/password fields, and the LoginAction 
getted well the parameters' value. 

Now, with my new Interceptor, the LoginAction gets empty values from my 
login and password fields.

What's wrong ? I tried a lot of order's combinations in the stack, but I 
did'nt find the good one. 

My login.jsp :
        <s:form action="login" validate="true">
            <table border="0" cellspacing="0" cellpadding="0" 
align="center" width="400"> 
                <s:hidden name="user.loginAttempt" value="%{'1'}" />
                <s:actionerror/>
                <s:textfield label="%{getText('login.login ')}" 
name="user.login" size="20" maxlength="20" tabindex="1"/>
                <s:password label="%{getText('login.pwd')}" 
name="user.pwd" size="20" maxlength="20" tabindex="2"/> 
                <s:submit label="%{getText('login.submit')}" 
method="login">
                    <s:param name="colspan" value="%{2}" />
                    <s:param name="align" value="%{'center'}" /> 
                </s:submit>
            </table>
        </s:form>

My LoginAction.java :
public class LoginAction extends ActionSupport implements 
ServletRequestAware {
    private Logger log = Logger.getLogger(LoginAction.class);
    private HttpServletRequest request;
    private UserVO user;
    private AuthentifyUserServiceInterface authentifyUserService;
    
    public String login () {
        String vTarget; 
        try {
            // Call the Service layer for authentification.
            if (authentifyUserService.authentifyUser(user)) {
                setSessionAttribute(request, Constants.USER, user.getLogin 
());
                vTarget = SUCCESS;
            } else {
                addActionError(getText("errors.loginko"));
                vTarget = INPUT;
            }
        }
        catch (TechnicalException e) { 
            addActionError(getText("error.message"));
            vTarget = ERROR;
        }
        return vTarget;
    }
    protected void setSessionAttribute(HttpServletRequest request, String 
key, Object o) { 
        if(o != null) {
            request.getSession().setAttribute(key, o);
        }
    }
    public void setUser(UserVO user) {
        this.user = user;
    }
    public void setAuthentifyUserService( 
            AuthentifyUserServiceInterface authentifyUserService) {
        this.authentifyUserService = authentifyUserService;
    }
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
}

My LoginInterceptor.java :
public class LoginInterceptor extends AbstractInterceptor implements 
StrutsStatics {
    private static final String LOGIN_ATTEMPT = "user.loginAttempt ";

    public String intercept (ActionInvocation invocation) throws Exception 
{
        final ActionContext context = invocation.getInvocationContext ();
        HttpServletRequest request = (HttpServletRequest) 
context.get(HTTP_REQUEST);
        HttpSession session =  request.getSession (true);

        Object user = session.getAttribute(Constants.USER);
        if (user == null) {
            String loginAttempt = request.getParameter (LOGIN_ATTEMPT);
            if (! StringUtils.isBlank (loginAttempt) ) {
                return invocation.invoke ();
            }
            return "notLogged";
        } else { 
            return invocation.invoke();
        }
    }
}

My struts.xml :
<interceptors>
            <interceptor name="login" 
class="com.abw.util.LoginInterceptor" /> 

            <interceptor-stack name="crmStack">
                <interceptor-ref name="exception"/>
                <interceptor-ref name="alias"/>
                <interceptor-ref name="servlet-config" /> 
                <interceptor-ref name="login" />
                <interceptor-ref name="i18n"/>
                <interceptor-ref name="prepare" />
                <interceptor-ref name="chain" /> 
                <interceptor-ref name="model-driven" />
                <interceptor-ref name="fileUpload" />
                <interceptor-ref name="checkbox"/>
                <interceptor-ref name="static-params" />
                <interceptor-ref name="params" />
                <interceptor-ref name="conversionError" />
                <interceptor-ref name="validation" />
                <interceptor-ref name="workflow" />
            </interceptor-stack>
        </interceptors>

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

The fact is that first I would like to have the hidden parameter in the 
login.jsp being not an attribute in the userVO bean. But if I set 
<s:hidden name="somethingOther" value="%{'1'}" />, I have a 
ParametersInterceptor Exception saying "ParametersInterceptor - 
[setParameters]: Unexpected Exception catched". 

Then, How could I have my values in my LoginAction ? I would like to do 
the authentifyUserService.authentifyUser() in the Action, not in the 
Interceptor. And I suppose this will allow me to have my values in my 
others forms too. 

Thanks for your help.

Regards,

Michaël. 

Re: [S2][interceptors]Inserted my own interceptor => no more params value

Posted by Mark Menard <ma...@mjm.net>.
On 4/23/07 2:36 AM, "MLENEVEUT@abusinessware.com"
<ML...@abusinessware.com> wrote:

> Hi all,
> 
> I try to insert my "AuthentificatedInterceptor" in the stack, but now
> nothing works.

Hi Michaël,

I just did this last week. I didn't use a LoginAction though. I did all the
work in the interceptor, with a backing service bean.
(http://www.vitarara.org/cms/struts_2_cookbook/creating_a_login_interceptor)

I found it easier to just do the login attempt in the interceptor rather
than having to sometimes pass through the interceptor if an attempt was
occurring, and other times passing it through if they are logged in.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org