简体   繁体   English

从EJB访问ManagedBean方法

[英]Accessing ManagedBean method from EJB

I have an EJB that handles creation of customer user accounts that needs access to a managed bean (account scoped) which manages user verification keys for user accounts (the keys are transient, so they don't need to be handled by database calls). 我有一个EJB,用于处理需要访问托管bean(客户范围)的客户用户帐户的创建,该bean管理用户帐户的用户验证密钥(密钥是临时的,因此不需要数据库调用来处理)。 However, I can't figure out a way to send the verification key to the EJB (which generates the verification email that is send to a user). 但是,我想不出一种将验证密钥发送到EJB的方法(EJB会生成发送给用户的验证电子邮件)。

AccountVerifierBean.java AccountVerifierBean.java

@ManagedBean(name = "accountVerifierBean", eager = true)
@ApplicationScoped
public class AccountVerifierBean implements Serializable {
    private HashMap<String, String> verificationKeyMapping;

    public AccountVerifierBean() {}

    public boolean verifyKey(String username, String key) {
        return key.equals(verificationKeyMapping.get(username));
    }
    public String generateKey(String username) {
        Date time = new Date();
        String key = username + time.toString();
        key = Encryption.hashSHA(key);
        verificationKeyMapping.put(username, key);
        return key;
    }
}

CustomerService.java CustomerService.java

@Named
@Stateless
@LocalBean
public class CustomerService {
    @PersistenceContext(unitName = "MovieProject-ejbPU")
    private EntityManager em;

    private String username;
    private String password;

    //getters and setters

    public void validateEmail() {
        Properties serverConfig = new Properties();
        serverConfig.put("mail.smtp.host", "localhost");
        serverConfig.put("mail.smtp.auth", "true");
        serverConfig.put("mail.smtp.port", "25");

        try {
            Session session = Session.getInstance(serverConfig, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("<ACCOUNT>","<PASSWORD>");
                }
            });
            MimeMessage message = new MimeMessage( session );
            message.setFrom(new InternetAddress("accounts@minimalcomputers.com","VideoPile"));
            message.addRecipient(
                Message.RecipientType.TO, new InternetAddress(username)
            );
            message.setSubject("Welcome to VideoPile!");
            message.setContent("<p>Welcome to VideoPile, Please verify your email.</p><p>" + verifierKey + "</p>", "text/html; charset=utf-8"); //verifierKey is what I'm trying to get from AccountVerifierBean.
            Transport.send( message );
        }
        catch (MessagingException ex){
            Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (Exception e) {
            Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public String encrypt(String password) {
        try {
            return new     String(Base64.encode(MessageDigest.getInstance("SHA").digest(password.getBytes())));
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}

I've tried @Inject, @ManagedProperty, using the Application map, and using the ELContext. 我已经尝试使用应用程序映射和ELContext来使用@ Inject,@ ManagedProperty。 Nothing seems to work. 似乎没有任何作用。

EDIT: I think there is something wrong with the bean. 编辑:我认为bean有问题。 Any methods called from bean don't seem to do anything (EL is resolved, no bean method calls though). 从bean调用的任何方法似乎都无能为力(EL已解决,但没有bean方法调用)。

I've tested the annotations that it uses (both are javax.faces.bean.*) 我已经测试了它使用的注释(都是javax.faces.bean。*)

So, the issue was in the AccountVerifierBean. 因此,问题出在AccountVerifierBean中。

I added the following lines to faces-config.xml and it is now working. 我在faces-config.xml中添加了以下几行,现在可以使用了。

<managed-bean eager="true">
  <managed-bean-name>accountVerifierBean</managed-bean-name>
  <managed-bean-class>org.Videotheque.beans.AccountVerifierBean</managed-bean-class>
  <managed-bean-scope>application</managed-bean-scope>
</managed-bean>

I'm fairly certain that the problem was because my bean needed to be in the EJB package instead of the WAR so that the EJBs can access it, but because of that, the WAR didn't know that the bean existed. 我可以肯定地确定问题是因为我的bean需要位于EJB包中而不是WAR中,以便EJB可以访问它,但是因此,WAR不知道该bean存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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