简体   繁体   中英

Stateful session bean in Struts2

I have been trying to create a simple stateful session bean with Struts2, but every time I resend my request and try to retrieve the info previously put in my stateful bean, it returns NULL. Am I missing something?

The injection is working properly after the registration but when I make a new request with on the retrieve method in the action class, I am unable to get the info of my session since it returns null.

So how do I do to achieve that?

Pay no attention to the locks, I put them just to test other features. you can ignore them.I repeat the injection is working properly on the execute method

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);

}

Thank you to both of you Andrea and Aleksandr. I was using @Stateful instead of @Named @SessionScoped . What is the difference between them two anyway. The textbook I was using makes use of @Stateful to refer to stateful bean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM