02 November 2016

Những vấn đề về JSF JavaServer Faces

Những vấn đề về JSF
public String login() throws SQLException {
        UserDAO udao = new UserDAO();
        if (udao.checkLogin(username, password)) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
            session.setAttribute("user", username);
            return "show";
        } else {
            FacesContext fc = FacesContext.getCurrentInstance();
            fc.addMessage(null, new FacesMessage("Sai username hoặc password!"));
            return null;
        }
    }

public void logout() throws IOException {
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        ec.invalidateSession();
        ec.redirect(ec.getRequestContextPath() + "/faces/login.xhtml");
}
Log Out
Welcome: <h:outputText value="#{user}"/> 
<h:form class="icon-key">
         <h:commandLink action="#{loginManagedBean.logout()}"> LogOut</h:commandLink>
</h:form>
CSS cho JSF: 
<div class="buttons">
    <h:commandButton value="Button Text" styleClass="apply"/>
    <h:commandButton value="Button Text" styleClass="cross"/>
</div>
Vòng Lặp list 
<ui:repeat value="#{bluewaveManagedBean.allBluewaveShow}" var="item">
       <h:outputText value="#{item.name}"/>
</ui:repeat>
Vòng lặp list
<h:dataTable value="#{bluewaveManagedBean.allBluewaveShow}" var="item">
      <h:column>
              <f:facet name="header" >ID</f:facet>
              <h:outputText value="#{item.id}"></h:outputText>
      </h:column>
</h:dataTable>
action: đi đến trang .jsp
actionListener: đi đến ManagedBean gọi hàm delete không có return String

<h:commandLink action="manager" actionListener="#{bluewaveManagedBean.deleteB(item.id)}" value="Delete" />
action: đi đến Managedbean có return String
<h:commandButton action="#{login.validateUsernamePassword}"
   value="Login"/>
<h:commandLink action="#{login.logout}" value="Logout"/>
Empty
<h:outputLabel value="#{row==10 ? '10' : '15'}"/>
Empty
<h:outputText value="Table is empty!" rendered="#{empty bean.list}" />

<h:dataTable value="#{bean.list}" rendered="#{not empty bean.list}">
    ...
</h:dataTable>

Empty
<h:form rendered="#{bean.booleanValue}" />
<h:form rendered="#{bean.intValue gt 10}" />
<h:form rendered="#{bean.objectValue eq null}" />
<h:form rendered="#{bean.stringValue ne 'someValue'}" />
<h:form rendered="#{not empty bean.collectionValue}" />
<h:form rendered="#{not bean.booleanValue and bean.intValue ne 0}" />
<h:form rendered="#{bean.enumValue eq 'ONE' or bean.enumValue eq 'TWO'}" />
ForEach
<c:forEach items="#{menuView.menu}" var="entry">
     <c:if test='#{entry.key} == #{name}'>
            <h:dataTable value="#{entry.value}" var="submenu">
                   <h:column>
                           <h:outputText value="#{submenu}" />
                   </h:column>
            </h:dataTable>
     </c:if>
 </c:forEach>
IF ELSE
<c:if test="${'#{user.userId}' == '0'}">
  <a href="Images/thumb_02.jpg" target="_blank" ></a>
  <img src="Images/thumb_02.jpg" />
</c:if>
<c:otherwise>
  <a href="/DisplayBlobExample?userId=#{user.userId}" target="_blank"</a>
  <img src="/DisplayBlobExample?userId=#{user.userId}" />
</c:otherwise>


<c:if test="#{not empty user or user.userId eq 0}">
    <a href="Images/thumb_02.jpg" target="_blank" ></a>
    <img src="Images/thumb_02.jpg" />
</c:if>
<c:if test="#{empty user or user.userId eq 0}">
    <a href="/DisplayBlobExample?userId=#{user.userId}" target="_blank"></a>
    <img src="/DisplayBlobExample?userId=#{user.userId}" />
</c:if>
IF ELSE
<a href="#{not empty user or user.userId eq 0 ? '/Images/thumb_02.jpg' : '/DisplayBlobExample?userId='}
#{not empty user or user.userId eq 0 ? '' : user.userId}" target="_blank"></a>

<img src="#{not empty user or user.userId eq 0 ? '/Images/thumb_02.jpg' : '/DisplayBlobExample?userId='}
#{not empty user or user.userId eq 0 ? '' : user.userId}" target="_blank"></img>
Display message login fail JSF
<h:form> 

    <h1>Login</h1>
    <!--Message ManagedBean-->
    <h:messages globalOnly="true" styleClass="message_loginfail" />

    <!--Message Input-->
    <h:message for="user" style="color: red"/>
    <h:inputText id="user" value="#{loginManagedBean.username}" required="true" requiredMessage="Please Enter your username" pt:placeholder=" Enter your username"/>

    <!--Massage Input-->
    <h:message for="pass" style="color: red"/>
    <h:inputSecret id="pass" value="#{loginManagedBean.password}" required="true" requiredMessage="Please Enter your username" pt:placeholder=" Enter your password"/>

    <h:commandButton styleClass="button" action="#{loginManagedBean.login() }" value="Login"/> 

</h:form>
Kiến thức cần có:
1,   <h:message/> là hiển thị lỗi từ input được for="id"
2,   <h:messages/> 
    * Display =>> <h:message/> + ManagedBean
    * globalOnly="true" Display =>> ManagedBean

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang