您可以更改视图代码以添加 ajax 功能:
<h:form id="loginform">
<h:panelGroup rendered="#{not loginManagedBean.isLogged}">
<h:inputText id="username" required="true" value="#{loginManagedBean.username}" />
<h:inputSecret id="password" required="true" value="#{loginManagedBean.password}" />
<h:commandLink id="login_button" value="Sign In">
<f:ajax listener="#{loginManagedBean.onButtonLoginClick}" render="loginform" />
</h:commandLink>
</h:panelGroup>
<h:panelGroup rendered="#{loginManagedBean.isLogged}">
<h:commandLink id="logout_button" value="Sign Out">
<f:ajax listener="#{loginManagedBean.onButtonLogoutClick}" render="loginform" />
</h:commandLink>
</h:panelGroup>
</h:form>
并将您的 bean 更改为以下内容:
@ManagedBean
@RequestScoped
public class LoginManagedBean
{
private String username;
private String password;
public void setUsername(String username)
{
this.username = username;
}
public String getUsername()
{
return this.username;
}
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return this.password;
}
public boolean getIsLogged()
{
return false;// change to verify in session if logged
}
public void onButtonLoginClick(AjaxBehaviorEvent event)
{
// Do the login stuff verification, put your user in session, etc
}
public void onButtonLogoutClick(AjaxBehaviorEvent event)
{
// Do the logout stuff verification, remove your user in session, etc
}
}
注意:您还可以添加登录错误消息并使用<h:message />
.
更多信息 :
将 Ajax 与 Facelets 结合使用