0

在使用 ajax 行为成功登录后,我正在尝试为相同的 primefaces 组件充电,但我不知道该怎么做。充值的组件必须显示用户的全名,该用户名是通过hibernate 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>

豆 :

@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 ****");
    }
}
4

1 回答 1

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 的寿命越短,你的内存使用就越好。这样,beanLoginManagedBean不需要是SessionScoped,甚至只有RequestScoped当您将属性移动loggedClient到 SessionScoped 属性时才可能是。

于 2013-06-03T23:52:12.657 回答