VietTuts

Tự Học Lập Trình Online

  • Home
  • Java
  • Servlet
  • JSP
  • Struts2
  • Hibernate
  • Spring
  • MyBatis
  • Java WS
  • C
  • C++
  • Python
  • PHP
  • Eclipse
  • VBA
  • Web
    • JavaScript
    • JQUERY
    • JSON
    • AJAX
    • CSS
    • HTML
    • HTML5
    • Node.js
    • Angular 7
  • SQL
    • MySQL
    • SQL Server
  • Misc
    • Phần mềm tiện ích
    • Cấu trúc dữ liệu và giải thuật
    • Học lập trình C#
    • Selenium Test

Struts2 Tuts

MVC là gì? Struts2 là gì? Struts2 - Kiến trúc Struts2 - Thiết lập môi trường Struts2 - Hello World Struts2 - Cấu hình

Struts2 Các Ví Dụ

Ví dụ login trong struts2 Truyền list từ jsp tới action trong struts2

Ví dụ login trong Struts2


Cấu hình Struts2
Truyền list từ JSP tới action trong Struts2

Nội dung chính

  • Các công nghệ sử dụng trong ví dụ này
  • Cấu trúc của project "struts2-login-example"
  • Tạo project "struts2-login-example"
  • Cấu hình web.xml
  • Tạo file struts.xml
  • Tạo trang chứa login form
  • Tạo trang welcome.jsp
  • Tạo action
  • Tạo lớp Constant.java
  • Thực thi project "struts2-login-example"
  • Download Source Code

Các công nghệ sử dụng trong ví dụ này

  • eclipse mar2
  • apache-tomcat-7.0.75
  • struts-2.3.3
  • jdk 1.8
  • Maven 3.5


Cấu trúc của project "struts2-login-example"

Ví dụ login trong struts2

Tạo project "struts2-login-example"

Tạo project bằng cách chọn File-->New-->Dynamic Web Project.

Add struts2 library

Cách 1: Sao chép các tập tin sau từ struts2 thư mục lib C:\lib\struts-2.3.3\lib vào thư mục WEB-INF\lib của project. Để thực hiện việc này, bạn chỉ cần kéo và thả tất cả các tệp sau vào thư mục WEB-INF\lib.

  • asm-xy.jar
  • asm-commons-xy.jar
  • asm-tree-xy.jar
  • commons-fileupload-xyz.jar
  • commons-langxy.jar
  • commons-io-xy.jar
  • freemarker-xyz.jar
  • javassist-.xyz.GA
  • ognl-xyz.jar
  • struts2-core-xyz.jar
  • xwork-core.xyz.jar

Cách 2: convert project sang maven project rồi add dependency vào file pom.xml như sau:

<dependencies>
  <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
  <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.3.3</version>
  </dependency>
</dependencies>


Cấu hình web.xml

Sửa file web.xml như sau:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">
  <display-name>struts2-login-example</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
    <!-- STRUTS2 DEFINITION -->
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>  
  
   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>  
  
</web-app>

Tạo file struts.xml

Tạo file strtus.xml tại thư mục resources của project:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        <action name="login" class="vn.viettuts.action.LoginAction" method="login">
            <result name="success">/welcome.jsp</result>
            <result name="error">/index.jsp</result>
        </action>
    </package>
</struts>


Tạo trang chứa login form

Tạo file index.jsp tại thư mục WebContent của project. Trong file này chúng ta định nghĩa một form gồm 2 thuộc tính có tên userName và password.

File: index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts2 - Login Example</title>
<style type="text/css">
.error-msg {
  color: red;
}
</style>
</head>
<body>
  <s:form id="loginForm" class="loginForm" theme="css_xhtml">
    <div class="error-msg">
      <s:actionerror />
    </div>
    <table class="login">
      <tbody>
        <tr>
          <td><s:label value="UserName:" for="userName" /></td>
          <td><s:textfield name="userName" id="userName" /></td>
        </tr>
        <tr>
          <td><s:label value="Password:" for="password" /></td>
          <td><s:password name="password" id="password" /></td>
        </tr>
        <tr>
          <td><s:submit name="login" value="Login" 
                        onclick="this.form.action='login'" />
          </td>
        </tr>
      </tbody>
    </table>
  </s:form>
</body>
</html>

Tạo trang welcome.jsp

Tạo file welcome.jsp tại thư mục WebContent của project. Trang này hiện thị tên userName được nhập từ trang index.jsp.

File: welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts2 - Welcome</title>
</head>
<body>
  Welcome, 
  <s:property value="userName" />
</body>
</html>

Tạo action

Tạo action login LoginAction.java tại package vn.viettuts.action của project.

File: LoginAction.java

package vn.viettuts.action;

import com.opensymphony.xwork2.ActionSupport;
import vn.viettuts.common.Constant;

public class LoginAction extends ActionSupport {
    private String userName;
    private String password;

    /**
     * login method
     * 
     * @return success or error
     */
    public String login() {
        if (!(Constant.USERNAME.equals(userName) 
                && Constant.PASSWORD.equals(password))) {
            addActionError(Constant.INVALID_ACCOUNT_MSG);
            return ERROR;
        }
        return SUCCESS;
    }

    // define setter & setter
    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;
    }
}
Note: Chúng ta định nghĩa các thuộc tính userName và password có tên trùng với thuộc tính name được định nghĩa trong các thẻ input trong form của trang jsp. Và phải cài đặt các phương thức getter, setter cho các thuộc tính đó vì struts framework làm việc trực tiếp với các phương thức này để truyền dữ liệu từ jsp đến action và ngược lại.

Tạo lớp Constant.java

Tạo lớp Constant.java tại package vn.viettuts.common của project. Vì đây là ứng dụng nhỏ chưa có kết nối đến database nên chúng ta định nghĩa các username, password, và error mesage giả định trong lớp Constant.java.

File: Constant.java

package vn.viettuts.common;

public class Constant {
    public static final String USERNAME = "admin";
    public static final String PASSWORD = "123456";
    public static final String INVALID_ACCOUNT_MSG = 
            "username or password is incorrect!";
}


Thực thi project "struts2-login-example"

Hiển thị ban đầu:

Ví dụ login trong struts2

Login fail:

Ví dụ login trong struts2

Login success:

Ví dụ login trong struts2


Download Source Code

Download source code + thư viện:

Download Now!

Download source code maven:

Download Now!

Cấu hình Struts2
Truyền list từ JSP tới action trong Struts2

Recent Updates

Struts2 là gì?Cấu hình Struts2Kiến trúc Struts2Struts2 - Hello WorldThiết lập môi trường cho Struts2Truyền list từ JSP tới action trong Struts2Ví dụ login trong Struts2Màu trong HTMLThiết lập màu nền (background) trong HTMLBlock trong HTMLIframe trong HTMLFrame trong HTMLEmail Link trong HTMLImage Link trong HTMLText Link trong HTMLCú pháp HTML5 cơ bảnHTML5 là gìTkinter Button trong PythonTkinter Canvas trong Python

VietTuts on facebook

Học Lập Trình Online Miễn Phí - VietTuts.Vn

Danh sách bài học

Học java
Học servlet
Học jsp
Học Hibernate
Học Struts2
Học Spring
Học SQL

Câu hỏi phỏng vấn

201 câu hỏi phỏng vấn java
25 câu hỏi phỏng vấn servlet
75 câu hỏi phỏng vấn jsp
52 câu hỏi phỏng vấn Hibernate
70 câu hỏi phỏng vấn Spring
57 câu hỏi phỏng vấn SQL

About VietTuts.Vn

Hệ thống bài học trên VietTuts.Vn bao gồm các bài lý thuyết và thực hành về các công nghệ java và công nghệ web. Các bài lý thuyết trên hệ thống VietTuts.Vn được tham khảo và tổng hợp từ các trang http://javatpoint.com, http://www.tutorialspoint.com, http://docs.oracle.com/en …

Scroll back to top

Copyright © 2016 VietTuts.Vn all rights reserved. | VietTuts.Vn team | Liên hệ | Chính sách - riêng tư | sitemap.html | sitemap_index.xml