0

i'm used jsf template....

login.xhtml is as fallows

<ui:composition     template="/WEB-INF/template/layout.xhtml"
        xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
        xmlns:p="http://primefaces.org/ui">

    <ui:define name="title">Login Page</ui:define>

<ui:define name="header"><center><H3>Please Login here</H3></center></ui:define>

<ui:define name="content">
<center>

<h:form id="loginForm">
        <p:messages id="messages" showDetail="false" autoUpdate="false" closable="true" />

    <h:panelGrid id="grid" columns="2" cellpadding="5">

        <p:outputLabel for="default" value="Username: " />
        <p:inputText id="default" value="#{userController.username}" required="true" requiredMessage=" * please enter username" validatorMessage=" * Username must between 5 - 20 characters">
            <f:validateLength minimum="5" maximum="20" />
        </p:inputText>  

        <p:outputLabel for="feedback" value="Password: " />
        <p:password id="feedback" value="#{userController.password}" required="true" requiredMessage=" * please enter password" validatorMessage=" * Password must between 5 - 30 characters">
            <f:validateLength minimum="5" maximum="30"/>
        </p:password>

        <p:outputLabel value="" />
        <p:column>
        <p:commandButton value="Login" update="myForm:logGrowl" icon="ui-icon-check"  actionListener="#{userController.authenticate()}" ajax="false" />
        <p:commandButton value="Reset" icon="ui-icon-refresh">
                <p:ajax update="grid" resetValues="true" />
        </p:commandButton>
        </p:column>

    </h:panelGrid>

</h:form>
</center>
</ui:define>
</ui:composition>

index.xhtml is as fallows

<ui:composition template="/WEB-INF/template/layout.xhtml"
        xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
        xmlns:p="http://primefaces.org/ui"
        xmlns:b="http://bootsfaces.net/ui">

<ui:define name="title">Home Page</ui:define>

<link rel="stylesheet" type="text/css" href="resources/css/myCSS.css" />



<ui:define name="content">
<h:form id="myForm">
        <p:messages id="messages" showDetail="false" autoUpdate="false" closable="true" />
<p:separator />

<DIV id="userInfo" >
    <h:panelGrid columns="4">
        <p:outputLabel value="Welcome: " />
        <p:outputLabel value="#{userController.name}" />
        <p:outputLabel value=" Last Login: " />
        <p:outputLabel value="#{userController.lastLogin}" />
    </h:panelGrid>
</DIV>

    <p:separator />
</h:form>
<b:jumbotron>
<h2>Welcome in Data Table Please select any one operation</h2>
</b:jumbotron>
</ui:define>

</ui:composition>

i have used single-bean as @ViewScoped

    public void authenticate()
    {
            mUsername = getUsername();
            mPassword = getPassword();
            dao = new UserDao();
            ResultSet validate = dao.loginService(mUsername, mPassword);
            if(validate.next())
            {
                HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
                session.setAttribute("userId", validate.getInt(1));
                session.setAttribute("username", mUsername);
                session.setAttribute("lastLogin", validate.getString(5));
                dao.setLastLogin(validate.getInt(1));
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Successfully Login...!!", null));
                FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Successfully Login...!!", null));
            }
}

finally i want to show my user name after redirect to index page and also show if i use

@PostConstruct
public void init(){
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
                String name = session.getAttribute("username");
}

it will give error on loading login form because before login session not create.... give me any idea using programming how i could pass data on next page and show faceMessage as give above..

Abbas
  • 11
  • 1
  • 4

0 Answers0