1. Trang chủ
  2. » Công Nghệ Thông Tin

Thực hành Strust JSF Lab 1

15 201 0

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Architecting Applications for the Web Lab 01 Introduction to Struts Framework Mục tiêu - Hiểu rõ kiến trúc mơ hình MVC - Nắm vững luồng liệu framework Struts - Tạo ứng dụng đơn giản với framework Struts Phần I Bài tập step by step Bài 1.1 Tạo Project với Struts - Tạo Project Web Application Netbeans - Sử dụng framwork Struts tích hợp sẵn Java EE Step 1: Tạo project Netbeans  File  New Project  Web  Web Application IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE  Name and Location:  Project: Đặt tên Project Lab01  Project Location: Trỏ đến thư mục đặt thư mục Project  Project Folder: Thư mục Project  Server and Settings: Chọn GlassFish server IT Research Department @BKAP 2015 Page / 15 Architecting Applications for the Web  Frameworks: Chọn Struts 1.3.10 tích hợp Netbeans  Kiến trúc project sau hồn thành Step 2: Cấu hình chạy Project  Lab01  RC  Properties IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE  Sources:  Source/Binary Format: Chọn JDK cài  Encoding: Chọn UTF8  Frameworks: Các framework dùng project  Libraries: Các thư viện dùng project  Run: Browser: IDE dùng để chạy Project Step 3: Build and Run Ứng dụng Bài 1.2 Xây dựng trang đăng nhập cho User - Trang đăng nhập có thông tin: User Name Passwork - Người dùng nhập thơng tin submit - Nếu User Name có giá trị Passwork: Hiển thị  Login Success Wellcome UserName - Nếu User Name có giá trị khác Passwork: Hiển thị  Invalid UserName Step 1: Tạo trang đăng nhập, hiển thị thông báo (VIEW)  Trang nhập login.jsp  Lab01  Web Pages  RC  New  Other  Web  JSP IT Research Department @BKAP 2015 Page / 15 Architecting Applications for the Web  Tạo form đăng nhập IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE Login Page STRUTS FRAMEWORK - LOGIN     UserName Passwork        Tạo trang hiển thị thông báo thành công success.jsp JSP Page IT Research Department @BKAP 2015 Page / 15 Architecting Applications for the Web Login Success Welcome  Tạo trang hiển thị thông báo thất bại failure.jsp JSP Page Invalid User Name  Step 2: Tạo ActionForm Bean để chứa thông tin UserName Password nhập từ form đăng nhập  Lab01  Source Packages  New  Other  Struts  Struts ActionForm Bean IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE  1: Tên ActionForm Bean  LoginForm  2: Package  actionForm  3: Kế thừa lớp org.apache.struts.action.ActionForm  4: Khi tạo cấu hình struts-config.xml  5: Finish  Kết thúc tạo ActionForm Bean  Sau thành công, ActionForm Bean tự động khai báo strutsconfig.xml (Web Pages  WEB-INF  struts-config.xml) IT Research Department @BKAP 2015 Page / 15 Architecting Applications for the Web ========================= Tiles plugin =============================== > > ========================= Validator plugin ================================= >  Code LoginForm.java  Khai báo tạo phương thức get/set cho thuộc tính userName, password error  Tạo phương thức validate bắt buộc nhập UserName Password package actionForm; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; /** * * @author Quang */ public class LoginForm extends org.apache.struts.action.ActionForm { private String userName; private String password; private String error; /** * */ IT Research Department @BKAP 2015 Page 10 / 15 Architecting Applications for the Web public LoginForm() { super(); // TODO Auto-generated constructor stub } /** * This is the action called from the Struts framework * * @param mapping The ActionMapping used to select this instance * @param request The HTTP Request we are processing * @return */ @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if (getUserName() == null || getUserName().length() < 1) { errors.add("userName", new ActionMessage("error.userName.required")); // TODO: add 'error.name.required' key to your resources } if (getPassword() == null || getPassword().length() < 1) { errors.add("password", new ActionMessage("error.password.required")); // TODO: add 'error.name.required' key to your resources } return errors; } 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 String getError() { IT Research Department @BKAP 2015 Page 11 / 15 Enterprise Application Development in Java EE return error; } public void setError(String error) { this.error = error; } } Step 3: Tạo lớp action  Chứa business login (Model) ứng dụng nhận liệu từ ActionForm Bean  Thực thi hàm Excecute(): Kiểm tra tính hợp lệ UserName Password nhập vào  Dựa vào kết nhận xác định trang jsp (View) thực  Tạo Struts Action  Lab01  Source Packages  New  Other  Struts  Struts Action  1: Tên Action  LoginAction  2: Package  action IT Research Department @BKAP 2015 Page 12 / 15 Architecting Applications for the Web  3: Configuration File  /WEB-INF/struts-config.xml: Tự động khai báo action file struts-config.xml  4: Action Path: Khai báo tên action Form login gọi  /login  5: Next  Sau hoàn thành, action khai báo tự động struts-config.xml  Code LoginAction.java package action; import actionForm.LoginForm; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; IT Research Department @BKAP 2015 Page 13 / 15 Enterprise Application Development in Java EE import org.apache.struts.action.ActionMapping; /** * * @author Quang */ public class LoginAction extends org.apache.struts.action.Action { /* forward name="success" path="" */ private static final String SUCCESS = "success"; private static final String FAILURE = "failure"; /** * This is the action called from the Struts framework * * @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request * @param request The HTTP Request we are processing * @param response The HTTP Response we are processing * @throws java.lang.Exception * @return */ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginForm loginForm = (LoginForm) form; String a1 = loginForm.getUserName(); String a2 = loginForm.getPassword(); if (loginForm.getUserName().equals(loginForm.getPassword())) { return mapping.findForward(SUCCESS); } else { return mapping.findForward(FAILURE); } } } Step 4: Điều chuyển hướng thẻ global-forwards struts-config.xml IT Research Department @BKAP 2015 Page 14 / 15 Architecting Applications for the Web Step 5: Cấu hình web.xml login.jsp Step 6: Build and Run Ứng dụng  Đăng nhập thành công  Đăng nhập thất bại IT Research Department @BKAP 2015 Page 15 / 15 ... login.jsp  Lab0 1  Web Pages  RC  New  Other  Web  JSP IT Research Department @BKAP 2015 Page / 15 Architecting Applications for the Web  Tạo form đăng nhập IT Research Department @BKAP 2015 Page... Netbeans  Kiến trúc project sau hồn thành Step 2: Cấu hình chạy Project  Lab0 1  RC  Properties IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE  Sources:... tên Project Lab0 1  Project Location: Trỏ đến thư mục đặt thư mục Project  Project Folder: Thư mục Project  Server and Settings: Chọn GlassFish server IT Research Department @BKAP 2015 Page /

Ngày đăng: 07/05/2018, 15:56

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w