1. Trang chủ
  2. » Thể loại khác

truy cap database trong struts 2

7 181 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Truy cập Database trong Struts 2

    • Tạo Action trong Struts 2

    • Tạo index.jsp

    • Tạo các thành phần view

    • Các configuration file

Nội dung

http://vietjack.com/struts_2/index.jsp Copyright © vietjack.com Truy cập Database Struts Chương tìm hiểu cách truy cập Database sử dụng Struts theo bước đơn giản Bước đầu tiên, bạn thiết lập Database Chúng tơi sử dụng MySQL ví dụ tạo Cơ sở liệu có tên struts_vietjack, có username mặc định root mật root123 Tạo bảng login cung cấp cho số giá trị Dưới code CREATE TABLE `struts_vietjack`.`login` ( `user` VARCHAR( 10 ) NOT NULL , `password` VARCHAR( 10 ) NOT NULL , `name` VARCHAR( 20 ) NOT NULL , PRIMARY KEY ( `user` ) ) ENGINE = InnoDB; INSERT INTO `struts_vietjack`.`login` (`user`, `password`, `name`) VALUES ('scott', 'navy', 'Scott Burgemott'); Tiếp theo, bạn tải MySQL Connector jar file đặt file WEB-INF\lib folder Sau tạo action class Tạo Action Struts Lớp action có thuộc tính tương ứng với cột bảng liệu Database Đó user, password name Trong phương thức, sử dụng tham số user password để kiểm tra xem người dùng tồn hay khơng, có, hiển thị tên người dùng bước Nếu người dùng nhập sai thông tin, gửi lại cho họ hình đăng nhập Dưới nội dung LoginAction.java file: package com.vietjack.struts2; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/struts_2/index.jsp Copyright © vietjack.com import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private String user; private String password; private String name; public String execute() { String ret = ERROR; Connection conn = null; try { String URL = "jdbc:mysql://localhost/struts_vietjack"; Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(URL, "root", "root123"); String sql = "SELECT name FROM login WHERE"; sql+=" user = ? AND password = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, user); ps.setString(2, password); ResultSet rs = ps.executeQuery(); while (rs.next()) { name = rs.getString(1); ret = SUCCESS; } } catch (Exception e) { ret = ERROR; } finally { if (conn != null) { try { conn.close(); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/struts_2/index.jsp Copyright © vietjack.com } catch (Exception e) { } } } return ret; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } } http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/struts_2/index.jsp Copyright © vietjack.com Tạo index.jsp JSP file để thu thập username password, mà kiểm tra đối chiếu với Database Login User: Password: Tạo thành phần view Bây bạn tạo success.jsp mà triệu hồi action trả SUCCESS error.jsp trường hợp trả ERROR success.jsp Dang nhap cong http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/struts_2/index.jsp Copyright © vietjack.com Hello World, error.jsp Ten dang nhap hoac mat khau khong hop le Sai ten dang nhap hoac mat khau Các configuration file Cuối cùng, bạn đặt tất thứ sử dụng struts.xml, sau: /success.jsp /error.jsp http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/struts_2/index.jsp Copyright © vietjack.com Còn nội dung web.xml file: Struts 2 index.jsp struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /* Sau đó, bạn chạy ứng dụng kiểm tra http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/struts_2/index.jsp http://vietjack.com/ Trang chia sẻ học online miễn phí Copyright © vietjack.com Page ... dụng struts. xml, sau: ... struts2 org.apache .struts2 .dispatcher.FilterDispatcher struts2 /*... "http:/ /struts. apache.org/dtds /struts- 2. 0.dtd">

Ngày đăng: 02/12/2017, 21:07

TỪ KHÓA LIÊN QUAN

w