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

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang