Session bean in Struts2

I'm trying to create a simple session with a stateful bean using Struts2, but every time I resubmit my request and try to get the information previously placed in my stateful bean, it returns NULL. Am I missing something?

The injection works correctly after registering, but when I make a new request with the retrieve method in the activity class, I cannot get my session information as it returns null.

How can i do this?

Ignore locks, I used them just to test other functions. you can ignore them. I repeat that injection works correctly when the method is executed

public class Registration extends ActionSupport implements ModelDriven<Account> {
    private static final long serialVersionUID = -8930461193700155653L;
    private Account account;
    private User user;

    @Inject // this is the interface implemented by my stateful bean
    AccountServiceLocal asl;

    @Inject
    PassHashInterface phi;

    @Inject
    UserServiceLocal usl;
    private final Lock lock =new ReentrantLock();

    @org.apache.struts2.interceptor.validation.SkipValidation
    public  String retrieve(){      
        if(asl.getEmail()!=null){ // always null        
            return "success";           
        }
        return "input";
    }
    public  String execute() throws NoSuchAlgorithmException,
                                    InvalidKeySpecException, 
                                    InterruptedException {

        user=new User();
        user.setEmail(account.getEmail().toString().trim());         
        System.out.println("in execute");
        account.setRole("Regular");
        account.setPwd(phi.createHash(account.getPwd()));
       /*
        * NB. the primary key in Account(AccountId) does not
        * Necessary matches the primary key in User(Id) 
        */         
        asl.create(account);
        usl.create(user);
        lock.unlock();          
        return "success";            
    }

    @Override
    public Account getModel() {
        account=new Account();
        return account;
    }
}

      

My struts.xml file

 <?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.objectFactory.cdi.jndiKey" 
                 value="java:comp/some/weird/BeanManagerReference" />
        <package name="default" namespace="/" extends="struts-default">
            <action name="register" class="action.Registration"
                                   method="execute">
                <result name="success">success.jsp</result> 
                <result name="input">registration.jsp</result>
            </action>                
            <action name="save" class="action.Registration" 
                               method="retrieve">
                <result name="success" >index.jsp</result> 
                <result name="input" >registration.jsp</result>
            </action>               
        </package>
    </struts>

      

AccountserviceImpl:

@Stateful
public class AccountServiceImpl implements AccountServiceLocal {

    @Inject 
    private AccountDaoLocal adl;
    /* (non-Javadoc)
     * @see ejb.Business.AccountServiceLocal#create(ejb.Domain.Account)
     */
    @Override
    public void create(Account a) {
        // TODO Auto-generated method stub
        System.out.println("in"+this);
        adl.createdao(a);
    }

    /* (non-Javadoc)
     * @see ejb.Business.AccountServiceLocal#delete(java.lang.Object)
     */
    @Override
    public void delete(Object id) {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see ejb.Business.AccountServiceLocal#find(java.lang.Object)
     */
    @Override
    public Account find(Object id) {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see ejb.Business.AccountServiceLocal#update(ejb.Domain.Account)
     */
    @Override
    public void update(Account a) {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see ejb.Business.AccountServiceLocal#findemail(java.lang.Object)
     */
    @Override
    public List findemail(Object id) {
        // TODO Auto-generated method stub

        return adl.finddaobyEmail(id);
    }

}

      

AccountServiceLocal:

@Local
public interface AccountServiceLocal {

    public void create(Account a);

    public void delete(Object id);

    public Account find(Object id);

    public List<?> findemail(Object id);

    public void update(Account a);

}

      

+3


source to share


1 answer


Thank you both Andrea and Alexander. I used @Stateful

instead @Named

@SessionScoped

. What is the difference between them anyway. The tutorial I used is used @Stateful

to refer to a stateful bean.



0


source







All Articles