Thứ Năm, 17 tháng 7, 2014

Develop a Website using Struts 2

Đầu tiên ta tạo một ứng dụng với tên là BookSearch sau đó chọn framework là Struts 2.


Sau đó để có thể kết nối với dữ liệu trong database ta phải import một thư viện sqljdbc4.jar vào thư viện. Nếu chưa có các bạn có thể download tại đây




Tiếp theo để có ta sẽ thêm 1 thư viện là jstl1.2.2.



Tiếp theo ta tạo một class với tên là Book như dưới:

public class Book {

    private int id;
    private String title;
    private String author;
    private String price;
    private String desc;

    public Book(int id, String title, String author, String price, String desc) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.price = price;
        this.desc = desc;
    }

    public Book(String title, String author, String price, String desc) {
        this.title = title;
        this.author = author;
        this.price = price;
        this.desc = desc;
    }

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

}

Tạo tiếp một class DataAccess dùng để kết nối đến cơ sở dữ liệu với code như dưới:


package DB.Connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nguyen Minh An
 */
public class DataAccess {

    public static Connection getConnection() throws SQLException {
        String url = "jdbc:sqlserver://localhost:1433;databaseName=BOOKSEARCH";
        String username = "LUCKY";
        String password = "123456";

        try {
            Connection conn = null;
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(url, username, password);
            return conn;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }

}

Tiếp đó ta tạo một lớp với tên BookManagement để xử lý các nghiệp vụ như hiển thị tất cả sách và tìm kiếm sách. Dưới đây là code của lớp này:


public class BookManagement {

    public static List<Book> GetAllBook() {
        try {
            List<Book> listBook = new ArrayList<>();
            Connection conn = DataAccess.getConnection();
            PreparedStatement ps = conn.prepareCall("select * from BOOK");
            ResultSet res = ps.executeQuery();
            while (res.next()) {
                int bId = res.getInt("bId");
                String bTitle = res.getString("bTitle");
                String bAuthor = res.getString("bAuthor");
                String bPrice = res.getString("bPrice");
                String bDesc = res.getString("bDesc");
                Book b = new Book(bId, bTitle, bAuthor, bPrice, bDesc);
                listBook.add(b);
            }
            return listBook;
        } catch (Exception ex) {
            Logger.getLogger(BookManagement.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static Book getBookById(String name) {
        try {
            Book b = null;
            Connection conn = DataAccess.getConnection();
            PreparedStatement ps = conn.prepareCall("select * from BOOK where bTitle like '" + name + "'");
            ResultSet res = ps.executeQuery();
            while (res.next()) {
                int bId = res.getInt("bId");
                String bTitle = res.getString("bTitle");
                String bAuthor = res.getString("bAuthor");
                String bPrice = res.getString("bPrice");
                String bDesc = res.getString("bDesc");
                b = new Book(bId, bTitle, bAuthor, bPrice, bDesc);
            }
            return b;

        } catch (SQLException ex) {
            Logger.getLogger(BookManagement.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

}


Tiếp theo tạo một class với tên là BookBean để tiến hành gọi phương thức từ lớp nghiệp vụ.

public class BookBean {

    public static List<Book> getAllBook() {
        List<Book> list = BookManagement.GetAllBook();
        return list;
    }

    public static Book getBookByName(String name) {
        Book b = BookManagement.getBookById(name);
        return b;
    }

}
Ta tạo một trang với tên là index.jsp để hiển thị tất cả sách trong hệ thống.

<jsp:useBean class="awasj.business.BookManagement" id="book">
            Tất cả sách:
            <table>
                <tr>
                    <td>Id</td>
                    <td>Name</td>
                    <td>Author</td>
                    <td>Price</td>
                    <td>Describe</td>
                </tr>
                <c:forEach items="${book.GetAllBook()}" var="b">
                    <tr>
                        <td>${b.id}</td>
                        <td>${b.title}</td>
                        <td>${b.author}</td>
                        <td>${b.price}</td>
                        <td>${b.desc}</td>
                    </tr>
                </c:forEach>
            </table>
        </jsp:useBean>
        <a href="Search.jsp">Search</a>

Ta tạo một trang với tên là Search.jsp.

      <h2>Search your book</h2>
        <form action="Searchword">
            Nhập tên sách: <input type="text" name="name">
            <input type="submit" value="search">
        </form>

Ta tạo một trang result.jsp để hiển thị kết quả:

<h1>Kết quả tìm kiếm</h1>
<table>
            <s:iterator  value="books">
                <tr>
                    <td><s:property value="id"/></td>
                    <td><s:property value="title"/></td>
                    <td><s:property value="author"/></td>
                    <td><s:property value="price"/></td>
                    <td><s:property value="desc"/></td>
                </tr>
            </s:iterator>
        </table>


Vậy là bạn đã tạo xong ứng dụng tìm kiếm sách. Bạn có thể download demo tại đây

Không có nhận xét nào:

Đăng nhận xét