JSF does not populate @Named @RequestScoped bean with submitted input values

前端 未结 1 1475
春和景丽
春和景丽 2020-11-30 14:49

this is my first question in this beautiful site. I have googled a lot but I didn\'t find any solution.

I\'m new to JSF and I\'m learning it with \"JSF 2 APIs and JB

1条回答
  •  孤独总比滥情好
    2020-11-30 15:01

    From your bean:

    import javax.faces.bean.RequestScoped;
    import javax.inject.Named;
    
    @Named("loginRequest")
    @RequestScoped
    public class LoginRequest {
    

    You're mixing CDI and JSF annotations. You can and should not do that. Use the one or the other. I don't know what's the book is telling you, but most likely you have chosen the wrong autocomplete suggestion during the import of the @RequestScoped annotation. Please pay attention to if whatever the IDE suggests you matches whatever the book tells you.

    So, you should be using either CDI annotations only

    import javax.enterprise.context.RequestScoped;
    import javax.inject.Named;
    
    @Named("loginRequest")
    @RequestScoped
    public class LoginRequest {
    

    or JSF annotations only

    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    
    @ManagedBean(name="loginRequest")
    @RequestScoped
    public class LoginRequest {
    

    Otherwise the scope defaults to "none" and every single EL expression referring the bean would create a brand new and separate instance of the bean. With three EL expressions referring to #{loginRequest} you would end up with 3 instances. One where name is been set, one where password is been set and one where action is been invoked.


    Unrelated to the concrete problem, the managed bean name already defaults to the classname with 1st character lowercased conform Javabean specification. You could just omit the ("loginRequest") part altogether.

    0 讨论(0)
提交回复
热议问题