使用Bootsfaces / Primefaces的@SessionScoped bean CDI

时间:2019-03-06 12:27:05

标签: jsf primefaces cdi bootsfaces session-scope

我目前正在做一个Maeven项目。

问题1:用户可以轻松登录,并且页面菜单也会相应更改。但是,一旦单击菜单项,用户便会注销。

问题2:当我将它们注入另一个bean中时,登录bean返回空值。

主菜单是用引导面制作的。

测试:使用Primefaces菜单栏,一切正常。

Template.xml:

 `<div id="top">
    <ui:insert name="top">
        <h:form rendered="#{!login.isIsloggIn()}" >
            <b:navBar brand="Enjoy the Holiday" brandHref="#" 
  inverse="true">

            <b:navbarLinks>
                <b:navLink value="Home" icon="home" 
   outcome="/HomePage"/>
              <b:navLink value="About" href="#about"/>
              <b:navLink value="Contact" href="#contact"/>
            </b:navbarLinks>
           <b:navbarLinks pull="right">
            <b:navLink value="Sign Up" outcome="/Registrationpage"/>
            <b:navLink value="Login" outcome="/LoginPage"/>

            </b:navbarLinks>
          </b:navBar>
        </h:form>

        <h:form rendered="#{login.isIsloggIn()}">
             <b:navBar brand="Enjoy the Holiday" brandHref="#" inverse="true">
            <b:navbarLinks>
                <b:navLink value="Home" icon="home" outcome="/HomePage"></b:navLink>
              <b:navLink value="About" href="#about"></b:navLink>
              <b:navLink value="Contact" href="#contact"></b:navLink>
            </b:navbarLinks >
            <b:navbarLinks pull="right">

                <b:dropMenu value="Wilkommen 
   {login.GetUserNamebyEmail()}"  >
                    <b:navLink value="Profil" icon="user" 
    outcome="/Overviews/KundeOverview"></b:navLink>

                    <b:navLink value="Inbox" icon="envelope" href="#"> 
        </b:navLink>
                    <b:navLink></b:navLink>
                    <b:navLink header="Konto"></b:navLink>
                    <b:navLink value="Einstellung" icon="cog" href="#"> 
   </b:navLink>
                </b:dropMenu>
                <b:navLink value="Logout" icon="log-out"  outcome="# 
    {login.loggOut()}"></b:navLink>
                 </b:navbarLinks>
              </b:navBar>
        </h:form>
    </ui:insert>

`

Login.xhtml:

 <ui:define name="content">
     <b:container>
        <h:form styleClass="form-signin" prependId="false">
            <h2 class="form-signin-heading">Please sign in</h2>
            <b:inputText id="email" placeholder="Email address"  value="#{login.email}">
                <f:facet name="prepend">
                    <b:icon name="user" />
                </f:facet>
            </b:inputText>
            <b:inputText id="password" placeholder="Password" type="password" value="#{login.password}">
                <f:facet name="prepend">
                    <b:iconAwesome name="key" />
                </f:facet>
            </b:inputText>
            <b:selectBooleanCheckbox value="true" caption="remember me"/>
            <b:commandButton look="primary btn-block" value="Sign In"  icon="log-in" size="lg" action="#{login.loggIn()}"/>
        </h:form>
    </b:container>

</ui:define>

登录Bean:

@Named
@SessionScoped
 public class Login implements Serializable{

@Inject
private AccountService accountService;

private Account account;

@Inject
private DataTestService datatestservice;



@Inject
private UserService userService;

private boolean isloggIn = false;

private String email ,password;

private String loggedUserEmail = null;

@PostConstruct
public void postConstruct() {
    System.out.println("PostConstruct");
}

@PreDestroy
public void preDestroy() {
    System.out.println("PreDestroy");
}
public String loggIn(){


    this.account = this.accountService.checkPassword(email, password);
    if(this.account == null){

       return  "Loginfailed";
    }else{

        this.isloggIn = true;
        this.loggedUserEmail = email;
         return  "Loginsuccess";
    }

}

另一个Bean:

@Named
@RequestScoped
public class KundenModel implements Serializable {

@Inject
private Login login ;

 @Inject
private AccountService accountService;

@Inject
private KundeService kundeService;

@PersistenceContext(unitName="EtHPU")
private E .....

输出无错误,但出现以下警告:

13:05:22,555 WARN  [org.jboss.weld.Bootstrap] (MSC service thread 1-5) WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class com.sun.faces.flow.FlowDiscoveryCDIHelper is deprecated from CDI 1.1!

13:17:08,869 WARN  [org.jboss.as.weld] (default task-7) WFLYWELD0052: Using deployment classloader to load proxy classes for module org.jboss.jts. Package-private access will not work. To fix this the module should declare dependencies on [org.jboss.weld.core, org.jboss.weld.spi]

1 个答案:

答案 0 :(得分:0)

也许您应该尝试实现一个LoginService(RequestScoped),它具有注入的UserSession或UserContext(SessionScoped),它们仅保留有关已登录用户的信息,而没有注入任何bean。我之所以这样想,是因为我不知道在销毁RequestScoped注入后,SessionScoped Bean会发生什么。

尝试一下:

@Named
@SessionScoped
 public class UserContext implements Serializable{
 private boolean loggedIn;
 private String email;

....
}

@Named
@RequestScoped
 public class LoginService implements Serializable{

 @Inject
 private UserContext userContext;

   public String loggIn(){


    this.account = this.accountService.checkPassword(email, password);
    if(this.account == null){

       return  "Loginfailed";
    }else{

        userContext.setLoggedIn(true);
         userContext.setUserEmail(email);
         return  "Loginsuccess";
    }

  }

}