• Thế Giới Giải Mã

    Bí ẩn nhân loại Leonardo Da Vinci

  • Thế Giới Giải Mã

    Anh hùng thầm lặng Nikola Tesla

  • Thế Giới Giải Mã

    Thần đèn Thomas Edison

  • Thế Giới Giải Mã

    Người thôi miên Adolf Hitler

Showing posts with label JSF. Show all posts
Showing posts with label JSF. Show all posts

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

13 September 2016

Test Introduction to Web and Enterprise Applications (GC4-WEBENT)

Validation from
Enter value
Enter success
Project
MyName.xhtml
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Welcome to Aptech Computer Education </title>
    </h:head>
    <h:body>
        <h1>Welcome to Aptech Computer Education</h1>
        <f:view>
            <h:form>
                Enter your first name<br/>
                <h:inputText id="first" value="#{myNameManagedBean.firstname}" required="true"  requiredMessage="Enter your firstname!"></h:inputText>
                <h:message for="first" style="color: red"/>
                <br/>
                Enter your last name<br/>
                <h:inputText id="last" value="#{myNameManagedBean.lastname}" required="true"  requiredMessage="Enter your lastname!"></h:inputText>
                <h:message for="last" style="color: red"/>
                <br/>
                <h:commandButton value="Enter" action="#{myNameManagedBean.login()}"></h:commandButton>
            </h:form>
        </f:view>

    </h:body>
</html> 
Welcome.xhtml
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Welcome</title>
    </h:head>
    <h:body>
        You have successfully Logged in #{myNameManagedBean.firstname} #{myNameManagedBean.lastname}
    </h:body>
</html>
MyNameManagedBean.java
Java 2016
package controller;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class MyNameManagedBean {

    private String firstname;
    private String lastname;

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String login() {
        if (firstname.equals("Duong") && lastname.equals("Dai")) {
            return "Welcome";
        } else {
            return "MyName";
        }
    }
}














Trắc nghiệm đúng 50%

12 September 2016

JSF Validate Form Register Java NetBeans


index.xhtml
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h1>Register</h1>
        <h:form>
            Name: <h:inputText id="name" value="#{registerManagedBean.r.name}" required="true" requiredMessage="Enter your name!" />
            
            <h:message for="name" style="color: red"/>
            <br/>
           
            Address: <h:inputText id="address" value="#{registerManagedBean.r.address}" required="true" requiredMessage="Enter your address"/>
            
            <h:message for="address" style="color: red"/>
            <br/>
          
            Email: <h:inputText id="email" value="#{registerManagedBean.r.email}" required="true" requiredMessage="Enter your email!" validatorMessage="Khong dung dinh dang!">
            <f:validateRegex pattern="^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"/>
            </h:inputText>
            <h:message for="email" style="color: red"/>
            <br/>
           
            Phone: <h:inputText id="phone" value="#{registerManagedBean.r.phone}" required="true" requiredMessage="Enter your phone!" validatorMessage="Khong dung dinh dang">
            <f:validateRegex pattern="^[0-9]+$"/>
            </h:inputText>
            <h:message for="phone" style="color: red"/>
            <br/>
            
            Password: <h:inputSecret id="password" value="#{registerManagedBean.r.password}" required="true" requiredMessage="Enter your password!"/>
            
            <h:message for="password" style="color: red"/>
            <br/>
            
            <h:commandButton value="Register" action="#{registerManagedBean.hello()}"/>
        </h:form>
    </h:body>
</html>

RegisterManagedBean.java
Java 2016
package controller;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import model.Register;

/**
 *
 * @author Lonely
 */
@ManagedBean
@SessionScoped
public class RegisterManagedBean {

    private Register r = new Register();

    public Register getR() {
        return r;
    }

    public void setR(Register r) {
        this.r = r;
    }
    public String hello(){
        return "hello";
    }
}
Register.java
Java 2016
package model;

public class Register {

    private String name;
    private String address;
    private String email;
    private String phone;
    private String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Register() {
    }

}
hello.jsp
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h1>Wellcome to user</h1>
    </h:body>
</html>
Cách viết style cho JSF: 
Markup
<div class="buttons">
    <h:commandButton value="Button Text" styleClass="apply"/>
    <h:commandButton value="Button Text" styleClass="cross"/>
</div>
CSS

.buttons .apply {
    background: #eee url(path/to/apply.png) no-repeat 0 50%;
} 

.buttons .cross {
    background: #eee url(path/to/cross.png) no-repeat 0 50%;
} 
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");
}

11 September 2016

Assignment JSF Java NetBeans Login, Insert, Update, Delete template

==============Assignment JSF Java web==============
index.xhtml
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Trang chu</title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        
        <link href="css/forever_style.css" rel="stylesheet" type="text/css" />
        <link href="css/jquery.lightbox-0.5.css" rel="stylesheet" type="text/css" />
        <link href="css/nivo-slider.css" rel="stylesheet" type="text/css" />
        <link href="css/tooplate_style.css" rel="stylesheet" type="text/css" />

    </h:head>
    <h:body>
        <div id="tooplate_wrapper">
        <ui:include src="header.xhtml"></ui:include>
        <ui:include src="content.xhtml"></ui:include>
        <ui:include src="footer.xhtml"></ui:include>
        </div>
    </h:body>
</html>

header.xhtml

Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>

        <div id="tooplate_header">

            <div id="tooplate_menu">
                <ul>
                    <li><a href="index.xhtml" class="current">Home</a></li>
                    <li><a href="#">Gallery</a></li>
                    <li><a href="#">News</a></li>
                    <li><a href="#">Blog</a></li>
                    <li><a href="#">Contact</a></li>

                    <li><a href="faces/login.xhtml" style="float: right; ">Login</a></li>
                </ul>
                <div class="cleaner"></div>
            </div> <!-- end of menu -->

            <div id="site_title"><h1><a href="#">Blue Wave</a><span>Your Tagline Goes Here</span></h1></div>

        </div> <!-- end of header -->
        <div id="tooplate_middle">
            <div id="search_box">
                <form action="#" method="get">
                    <input type="text" value="Search" name="q" size="10" id="searchfield" title="searchfield" onfocus="clearText(this)" onblur="clearText(this)" />
                    <input type="submit" name="Search" value="" id="searchbutton" title="Search" />
                </form>
            </div> <!-- end of search b-->
        </div>
    </h:body>
</html>
content.xhtml
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <title>Content</title>
    </h:head>
    <h:body>
        <div id="tooplate_main">
            <div id="tooplate_content">

                <div class="col_w880 home_intro">
                    <p><em>BLUE WAVE LÀ THÀNH VIÊN CỦA TẬP ĐOÀN 
                            UNC WATERS VỚI 67 NHÃN HIỆU 
                            VÀ ĐÃ CÓ MẶT Ở 130 QUỐC GIA</em></p>
                    <a href="#" class="learnmore"></a>
                    <div class="cleaner"></div>
                </div>

                <div class="col_w880 portfolio">
                    <h2>Portfolio</h2>
                    <ui:repeat value="#{bluewaveManagedBean.allBluewaveShow}" var="item">
                        <div class="col_w260">
                            <h6>#{item.name}</h6>
                            <img src="images/#{item.image}" alt="#{item.image}" />
                            <p>#{item.content}</p>
                            <a class="more" href="#">Learn more</a>
                        </div>
                    </ui:repeat>
                    <div class="cleaner"></div>
                </div>

            </div> <!-- end of content -->
        </div> <!-- end of main -->
    </h:body>
</html>
footer.xhtml
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <div id="tooplate_footer">

            Copyright © 2016 <a href="#"> BLUE WAVE</a>

        </div> <!-- end of tooplate_footer -->

    </h:body>
</html>
login.xhtml
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Login</title>
        <link href="css/login.css" rel="stylesheet" type="text/css" />

    </h:head>
    <h:body>
        <f:view>
            <h:form>
                <fieldset>

                    <legend>Log in</legend>

                    <label for="login">Username</label>
                    <h:inputText id="login" value="#{userManagedBean.username}"/>
                    <div class="clear"></div>
                    <label for="password">Password</label>
                    <h:inputSecret id="password" value="#{userManagedBean.password}"/>
                    <div class="clear"></div>

                    <label for="remember_me" style="padding: 0;">Remember me?</label>
                    <input type="checkbox" id="remember_me" style="position: relative; top: 3px; margin: 0; " name="remember_me"/>
                    <div class="clear"></div><br />

                    <h:commandButton type="submit" style="margin: -20px 0 0 287px;" class="button" action="#{userManagedBean.login}" value="Log in"/> 
                </fieldset>
            </h:form>
        </f:view>

    </h:body>
</html>
manager.xhtml
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html 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">
    <h:head>
        <title>Manager Bluewave</title>
        <link href="css/forever_style.css" rel="stylesheet" type="text/css" />
        <link href="css/jquery.lightbox-0.5.css" rel="stylesheet" type="text/css" />
        <link href="css/nivo-slider.css" rel="stylesheet" type="text/css" />
        <link href="css/tooplate_style.css" rel="stylesheet" type="text/css" />
        <link href="css/manager_style.css" rel="stylesheet" type="text/css" />

        <!-- import icon boostrap-->
<!--        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>-->
    </h:head>
    <h:body>
        <div id="tooplate_wrapper">
            <ui:include src="header.xhtml"></ui:include>

            <h1>Manager Bluewave</h1>
            <f:view>
                <h:form>
                    <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:column>
                            <f:facet name="header" >Name</f:facet>
                            <h:outputText value="#{item.name}"></h:outputText>
                        </h:column>
                        <h:column>
                            <f:facet name="header" >Image Url</f:facet>
                            <h:outputText value="#{item.image}"></h:outputText>
                        </h:column>
                        <h:column>
                            <f:facet name="header" >Content</f:facet>
                            <h:outputText value="#{item.content}"></h:outputText>
                        </h:column>
                        <h:column>
                            <f:facet name="header" ><h:commandLink value="Insert" action="insert" actionListener="#{bluewaveManagedBean.resetInsert()}" ></h:commandLink></f:facet>
                            <h:commandLink action="update" actionListener="#{bluewaveManagedBean.updateShow(item.id, item.name, item.image, item.content)}" value="Update" ></h:commandLink><span>||</span>
                            //Action manager là di chuyển đến manager.xhtml
                            //ActionListener là gọi bean thực hiện boolean delete
                            <h:commandLink action="manager" actionListener="#{bluewaveManagedBean.deleteB(item.id)}" value="Delete" ></h:commandLink>
                        </h:column>
                    </h:dataTable>
                </h:form>
            </f:view>

            <ui:include src="footer.xhtml"></ui:include>
        </div>
    </h:body>
</html>
insert.xhtml
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html 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">
    <h:head>
        <title>Insert Bluewave</title>
        <link href="css/forever_style.css" rel="stylesheet" type="text/css" />
        <link href="css/jquery.lightbox-0.5.css" rel="stylesheet" type="text/css" />
        <link href="css/nivo-slider.css" rel="stylesheet" type="text/css" />
        <link href="css/tooplate_style.css" rel="stylesheet" type="text/css" />
        <link href="css/manager_style.css" rel="stylesheet" type="text/css" />
    </h:head>
    <h:body>
        <div id="tooplate_wrapper">
            <ui:include src="header.xhtml"></ui:include>
            <h1 id="h1">Insert Buewave</h1>
            <f:view>
                <h:form id="form">
                    ID:<br></br>
                    <h:inputText value="#{bluewaveManagedBean.id}"></h:inputText><br></br>
                    Name:<br></br>
                    <h:inputText value="#{bluewaveManagedBean.name}"></h:inputText><br></br>
                    Image Url:<br></br>
                    <h:inputText  value="#{bluewaveManagedBean.imageurl}"></h:inputText><br></br>
                    Content:<br></br>
                    <h:inputTextarea value="#{bluewaveManagedBean.content}"></h:inputTextarea><br></br>
                    <h:commandButton value="Insert" action="manager" actionListener="#{bluewaveManagedBean.insertB()}"></h:commandButton>
                </h:form>
            </f:view>
            <ui:include src="footer.xhtml"></ui:include>
        </div>       
    </h:body>
</html>
update.xhtml
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html 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">
    <h:head>
        <title>Update Bluewave</title>
        <link href="css/forever_style.css" rel="stylesheet" type="text/css" />
        <link href="css/jquery.lightbox-0.5.css" rel="stylesheet" type="text/css" />
        <link href="css/nivo-slider.css" rel="stylesheet" type="text/css" />
        <link href="css/tooplate_style.css" rel="stylesheet" type="text/css" />
        <link href="css/manager_style.css" rel="stylesheet" type="text/css" />
    </h:head>
    <h:body>
        <div id="tooplate_wrapper">
            <ui:include src="header.xhtml"></ui:include>
            <h1 id="h1">Insert Buewave</h1>
            <f:view>
                <h:form id="form">
                    ID:<br></br>
                    <h:inputText value="#{bluewaveManagedBean.id}"></h:inputText><br></br>
                    Name:<br></br>
                    <h:inputText value="#{bluewaveManagedBean.name}"></h:inputText><br></br>
                    Image Url:<br></br>
                    <h:inputText  value="#{bluewaveManagedBean.imageurl}"></h:inputText><br></br>
                    Content:<br></br>
                    <h:inputTextarea value="#{bluewaveManagedBean.content}"></h:inputTextarea><br></br>
                    <h:commandButton value="Insert" action="manager" actionListener="#{bluewaveManagedBean.updateB()}"></h:commandButton>
                </h:form>
            </f:view>
            <ui:include src="footer.xhtml"></ui:include>
        </div>       
    </h:body>
</html>
DBUtil.java
Java 2016
package Util;


import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtil {

    private static Connection conn;
    private static String DRIVER = "com.mysql.jdbc.Driver";
    private static String URL = "jdbc:mysql://localhost:3306/product";
    private static String USER = "root";
    private static String PASS = "1234567";

    public static Connection getConnection() {
        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USER, PASS);
        } catch (Exception e) {
            System.out.println(e);
        }
        return conn;
    }

    //Run checking
//    public static void main(String[] args) {
//        getConnection();
//    }
}
BluewaveManagedBean.java
Java 2016
package controller;

import dao.BlueDeleteDAO;
import dao.BlueInsertDAO;
import dao.BlueUpdateDAO;
import dao.BluewaveDAO;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import model.Bluewave;

@ManagedBean
@SessionScoped
public class BluewaveManagedBean {

    private int id;
    private String name, imageurl, content;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImageurl() {
        return imageurl;
    }

    public void setImageurl(String imageurl) {
        this.imageurl = imageurl;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
    //Show all
    public ArrayList<Bluewave> getAllBluewaveShow(){
        return new BluewaveDAO().getAllBluewave();
    }
    //Insert
    public boolean insertB() throws SQLException{
        Bluewave b = new Bluewave(id, name, imageurl, content);
        return new BlueInsertDAO().insertBluewave(b);
    }
    //Reset 0 & null - khi vua dung xong update va chuyen sang insert
    public void resetInsert(){
        this.id =0;
        this.name =null;
        this.imageurl =null;
        this.content =null;
    }
    //Setter ghi nhan value khi bam vao update
    public void updateShow(int id, String name, String imageurl, String content) {
        this.id = id;
        this.name = name;
        this.imageurl = imageurl;
        this.content = content;
    }
    
    //Update
    public boolean updateB() throws SQLException{
        Bluewave b = new Bluewave(id, name, imageurl, content);
        return new BlueUpdateDAO().updateBluewave(b, id);
    }
    //Delete by id
    public boolean deleteB(int id) throws SQLException{
        return new BlueDeleteDAO().deleteBluewave(id);
    }
}
UserManagedBean.java
Java 2016
package controller;

import dao.UserDAO;
import java.sql.SQLException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class UserManagedBean {

    private String username, password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserManagedBean(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public UserManagedBean() {
    }

    public String login() throws SQLException {
        UserDAO udao = new UserDAO();
        if (udao.check(username, password)) {
            return "manager";
        } else {
            FacesContext fc = FacesContext.getCurrentInstance();
            fc.addMessage(null, new FacesMessage("Sai username hoặc password!"));
            return null;
        }
    }

}

BlueInsertDAO.java
Java 2016
package dao;

import Util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import model.Bluewave;

/**
 *
 * @author Lonely
 */
public class BlueInsertDAO {

    public boolean insertBluewave(Bluewave b) throws SQLException {
        Connection conn = DBUtil.getConnection();
        String sql = "Insert Into product.bluewave Values (?,?,?,?)";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setInt(1, b.getId());
        stmt.setString(2, b.getName());
        stmt.setString(3, b.getImage());
        stmt.setString(4, b.getContent());
        if (stmt == null) {
            throw new SQLException();
        } else {
            return stmt.executeUpdate() > 0;
        }
    }
    //Test insertBluewave
//    public static void main(String[] args) throws SQLException {
//        BlueInsertDAO b = new BlueInsertDAO();
//        Bluewave b1 = new Bluewave(120,"A","B","C");
//        System.out.println(b.insertBluewave(b1));
//    }
}
BlueUpdateDAO.java
Java 2016
package dao;

import Util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import model.Bluewave;

/**
 *
 * @author Lonely
 */
public class BlueUpdateDAO {
     public boolean updateBluewave(Bluewave b, int id) throws SQLException {
        Connection conn = DBUtil.getConnection();
        String sql = "Update product.bluewave Set name=?, image=?, content=? Where id='"+id+"'";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1, b.getName());
        stmt.setString(2, b.getImage());
        stmt.setString(3, b.getContent());
        if (stmt == null) {
            throw new SQLException();
        } else {
            return stmt.executeUpdate() > 0;
        }
    }
    //Test updateBluewave
//    public static void main(String[] args) throws SQLException {
//        BlueUpdateDAO b = new BlueUpdateDAO();
//        Bluewave b1 = new Bluewave(120,"A","B","C");
//        System.out.println(b.updateBluewave(b1, 120));
//    }
}
BlueDeleteDAO.java
Java 2016
package dao;

import Util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class BlueDeleteDAO {

    public boolean deleteBluewave(int id) throws SQLException {
        Connection conn = DBUtil.getConnection();
        String sql = "Delete From product.bluewave Where id='" + id + "'";
        PreparedStatement stmt = conn.prepareStatement(sql);
        if (stmt == null) {
            throw new SQLException();
        } else {
            return stmt.executeUpdate()>0;
        }
    }
    //Test deleteBluewave
//    public static void main(String[] args) throws SQLException {
//        BlueDeleteDAO b = new BlueDeleteDAO();
//        System.out.println(b.deleteBluewave(120));
//    }
}
BluewaveDAO.java
Java 2016
package dao;

import Util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Bluewave;

/**
 *
 * @author Lonely
 */
public class BluewaveDAO {

    public ArrayList<Bluewave> getAllBluewave() {
        ArrayList<Bluewave> list = new ArrayList();
        Connection conn = DBUtil.getConnection();

        try {
            PreparedStatement stmt = conn.prepareStatement("Select * From product.bluewave");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String image = rs.getString("image");
                String content = rs.getString("content");
                Bluewave bluewave = new Bluewave(id, name, image, content);
                list.add(bluewave);
            }
            conn.close();
        } catch (SQLException e) {
        }
        return list;
    }
    //Test getAllBluewave
//    public static void main(String[] args) {
//        BluewaveDAO b = new BluewaveDAO();
//        System.out.println(b.getAllBluewave());
//    }
}
UserDAO.java
Java 2016
package dao;

import Util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAO {

    public boolean check(String u, String p) throws SQLException {
        Connection conn = DBUtil.getConnection();
        PreparedStatement stmt = conn.prepareStatement("Select * From product.user Where user='" + u + "' and pass='" + p + "'");
        ResultSet rs = stmt.executeQuery();
        int count = 0;
        while (rs.next()) {
            count++;
        }
        if (count == 1) {
            return true;
        }
        return false;
    }
//    public static void main(String[] args) throws SQLException {
//        UserDAO u = new UserDAO();
//        System.out.println(u.check("tony", "12345"));
//    }
}
Bluewave.java
Java 2016
package model;

public class Bluewave {

    private int id;

    public Bluewave(int id, String name, String image, String content) {
        this.id = id;
        this.name = name;
        this.image = image;
        this.content = content;
    }
    private String name, image, content;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
 
}
ADD Libraries connect database mysql
C:\Program Files\MySQL\Connector.J 5.1

 

BACK TO TOP

Xuống cuối trang