Struts2中的有状态会话bean

时间:2015-05-19 04:09:15

标签: java struts2 ejb cdi stateful-session-bean

我一直在尝试使用Struts2创建一个简单的有状态会话bean,但每次重新发送我的请求并尝试检索以前放在我的有状态bean中的信息时,它都会返回NULL。我错过了什么吗?

注册后注入工作正常,但是当我在动作类中使用retrieve方法发出新请求时,我无法获取会话信息,因为它返回null。

那么我该如何做到呢?

不注意锁,我把它们只是为了测试其他功能。你可以忽略它们。我重复注入在execute方法上正常工作

sums = np.array( [data[i, start_indices[i]:].sum() for i in range(data.shape[0])] )

我的struts.xml文件

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

AccountserviceImpl:

 <?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>

AccountServiceLocal:

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

}

1 个答案:

答案 0 :(得分:0)

感谢Andrea和Aleksandr。我使用的是@Stateful而不是@Named @SessionScoped。无论如何,他们两个有什么区别。我正在使用的教科书使用@Stateful来引用有状态的bean。