Solved

[STRUTS 2.1.6] SessionAware does not inject session Object into Action

Posted on 2009-04-15
1
319 Views
Last Modified: 2013-11-24
Hello guys,

   Once more here I come with a question of s2, i'm a beginner at it and got some trouble finding examples.

   I'm build a menu service wich depends upon a login, wich is give when the user enter at start page. Once the login has been made, I store the user object into the session by doing the follow:

 

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
    ....
             
    // verifica se o parametro do CPF veio no get e tenta logar o usuario
    if (!StringUtils.isBlank(request.getParameter(USER_CPF_REQUEST))) {
   
    if ( doLogin(request, session) ) {
       return Action.SUCCESS;
    }

and then the doLogin method

    Usuario usuario = getServico().buscar( Long.valueOf(cpf) );
    //Caso o usuário exista, guarda na session
    if (usuario != null){
        session.setAttribute(USER_HANDLE, usuario);
        return true;
    }

Now comes the problem, I have a MenuBean injected on the MenuAction by the following xml piece on ApplicationContext.xml

<bean id="menuService" class="br.com.autenticis.renacon.ejb.MenuBean" />

    <bean id="menuAction" scope="prototype" class="br.com.autenticis.renacon.actions.MenuAction">
        <constructor-arg ref="menuService" />
    </bean>

   

And the menuAction is declared as follow:

    public class MenuAction extends ActionSupport implements Preparable, SessionAware

By doing so I need to implement the session set with a private member
....
private Map<String, Object> session;
....
@Override
public void setSession(Map<String, Object> session) {
   this.session = session;
}
    The failing part of all its the method bellow, the session object is null at debugging:


      

    public String execute() {
                if ( session.containsKey( LoginInterceptor.USER_HANDLE) ){
                      Usuario u = (Usuario) session.get( LoginInterceptor.USER_HANDLE);
                      setMenu( servico.getMenuPerfil( u.getPerfil() ) );
                      return Action.SUCCESS;
                }
                
                return input();
          }

   Does anyone know why? or how to implement it? Looking at the code above, i need 'Perfil' from the user wich is logged on, if the session contains the key to the user object i get it and then use the perfil to populate the Menu through the setter and return SUCESS, otherwise it'll return INPUT wich will lead to the login screen.


Thanks in advance.



0
Comment
Question by:dgomesbr
1 Comment
 

Accepted Solution

by:
dgomesbr earned 0 total points
ID: 24151965
After some ours I managed to discover the error. wich lies not in the program but in the struts.xml, I've written a custom stack for the application:

<!-- stack seguro -->
<!--
<interceptor-stack name="defaultLoginStack">
<interceptor-ref name="logger" />
<interceptor-ref name="params" />
<interceptor-ref name="login" />
<interceptor-ref name="prepare" />
<interceptor-ref name="chain" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="params" />
<interceptor-ref name="conversionError" />
<interceptor-ref name="validation" />
<interceptor-ref name="workflow" />
</interceptor-stack>
-->

And somewhat forgot to include session-related items, with the defaultLoginStack changed to

<interceptor-stack name="defaultLoginStack">
<interceptor-ref name="login" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>  
I'm now able to use session injected correctly.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
changeXy challenge 13 67
mapShare challenge 13 91
word0 challenge 4 66
custom annotations 9 32
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now