我试图在使用ajax行为成功登录后为相同的primefaces组件充电,但我不知道该怎么做。充值组件必须显示用户的完整名称,该名称由数据库中的休眠dao恢复,用户和密码的输入框必须消失。
查看:
<h:head>
<title>header layout</title>
</h:head>
<h:body>
<h:panelGrid id="loginPanelGrid">
<h:form id="loginForm">
<h:outputLabel for="username" value="Username:" />
<p:inputText value="#{loginManagedBean.username}" id="username" required="true" label="username" />
<h:outputLabel for="password" value="Password:" />
<p:password value="#{loginManagedBean.password}" id="password" required="true" label="password" />
</h:form>
<f:facet name="footer">
<p:commandButton id="loginButton" value="Login" update="okLoginPanelGrid" action="#{loginManagedBean.login(actionEvent)}"/>
</f:facet>
</h:panelGrid>
<p:panelGrid id="okLoginPanelGrid">
<h:outputText id="clienteLogado" value="#{loginManagedBean.loggedClient}" />
</p:panelGrid>
</h:body>
Bean:
@ManagedBean
@SessionScoped
public class LoginManagedBean implements Serializable{
private String username;
private String password;
private Cliente loggedClient;
@ManagedProperty(value="#{clienteDAO}")
private ClienteDAO clienteDAO;
public void login(ActionEvent actionEvent){
RequestContext context = RequestContext.getCurrentInstance();
if( (this.getUsername() != null && !this.getUsername().isEmpty()) &&
(this.getPassword() != null && !this.getPassword().isEmpty()) ){
List<Cliente> clienteLogin = clienteDAO.comprobarLogin(username, password);
if(clienteLogin != null && clienteLogin.size() > 0){
loggedClient = new Cliente();
for(Cliente client : clienteLogin){
loggedClient.setCliente_nombre(client.getCliente_nombre());
loggedClient.setCliente_apellido1(client.getCliente_apellido1());
loggedClient.setCliente_apellido2(client.getCliente_apellido2());
}
}else{
System.out.println("**** There are NO CLIENTS for this user and password selected ****");
}
}else{
System.out.println("**** user or password NOT RECOVERED ****");
}
}
答案 0 :(得分:0)
您应该移动h:form
以封装p:commandButton
。这会阻止您的p:commandButton
向bean提交任何内容。你应该这样添加:
<h:body>
<h:panelGrid id="login-panel">
<h:form rendered="#{loginManagedBean.loggedClient eq null}">
<h:panelGroup id="loginPanelGrid">
<h:outputLabel for="username" value="Username:" />
<p:inputText value="#{loginManagedBean.username}" id="username" required="true" label="username" />
<h:outputLabel for="password" value="Password:" />
<p:password value="#{loginManagedBean.password}" id="password" required="true" label="password" />
<f:facet name="footer">
<p:commandButton id="loginButton" value="Login" update="login-panel" action="#{loginManagedBean.login(actionEvent)}"/>
</f:facet>
</h:panelGroup>
</h:form>
<p:panelGroup rendered="#{loginManagedBean.loggedClient ne null}" id="okLoginPanelGrid">
<h:outputText id="clienteLogado" value="#{loginManagedBean.loggedClient.client_name}" />
</p:panelGroup>
</h:panelGrid>
</h:body>
不相关:
我还会说豆子越短,你的内存使用就越好。这样,bean LoginManagedBean
不需要SessionScoped
,如果将属性RequestScoped
移动到SessionScoped属性,它甚至可能只有loggedClient
。