@ManagedProperty不填写变量

时间:2017-05-28 15:26:22

标签: java spring primefaces

在applicationContext文件中,我添加了包来映射它。 调用到达,但变量仍为空,不会传递给服务器。在其他类中,同样正常工作。

提前致谢!

@ManagedBean(name="registerUser")
@SessionScoped
public class RegisterUser{

    @ManagedProperty("#{userService}")
    private DAOUser userService;

    private User user = new User();

    public DAOUser getUserService() {
        return userService;
    }

    public void setUserService(DAOUser userService) {
        this.userService = userService;
    }
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    public String register() {
        // Calling Business Service
        String passwordEncripada = PasswordControl.encriptString(user.getPassword());
        user.setPassword(passwordEncripada);
        userService.register(user);
        // Add message
        FacesContext.getCurrentInstance().addMessage(null, 
                new FacesMessage("Registro realizado"));
        return "";
    }

和DAOUser类:

@Component
public class DAOUser implements Serializable {
    @Autowired
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    @Transactional
    public void register(User user){
        // Acquire session
        Session session = sessionFactory.getCurrentSession();
        // Save employee, saving behavior get done in a transactional manner
        session.save(user);     
    }
}

编辑:调用setter,但它发送的对象为null

1 个答案:

答案 0 :(得分:0)

我认为问题在于DAOUser类名称的规范化。我已经使用@Repository(" userService")而不是@Component,它已经在使用了。

@Repository("userService")
public class DAOUser implements Serializable {
    @Autowired
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    @Transactional
    public void register(User user){
        // Acquire session
        Session session = sessionFactory.getCurrentSession();
        // Save employee, saving behavior get done in a transactional manner
        session.save(user);     
    }
}