Struts 2 (P.2)

Tìm hiểu về Struts 2 (P.2)

Xây dựng ứng dụng Hello World Hiển thị thông điệp “Hello world” cùng với thời gian của server 2.1.Xây dựng - Action (tương tác với Model) Tạo lớp Struts2HelloWorld.java trong struts2tutorial \WEB-INF\src\java\exam. Đây là một lớp action có nhiệm vụ tạo thông điệp Hello World và hiển thị ra

package java.exam;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;

public class Struts2HelloWorld extends ActionSupport {

   public static final String MESSAGE = "Struts 2 Hello World !";

   public String execute() throws Exception {
       setMessage(MESSAGE);
       return SUCCESS;
   }

   private String message;

   public void setMessage(String message){
       this.message = message;
   }

   public String getMessage() {
       return message;
   }

   public String getCurrentTime(){
     return new Date().toString();
   }
}
- Control Tạo file cấu hình điều khiển struts.xml trong thư mục struts2tutorial\WEB-INF\src với nội dung
<?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.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="true" />

  <package name="exam" namespace="/exam" extends="struts-default">

     <action name="HelloWorld" class="java.exam.Struts2HelloWorld">
        <result>/pages/HelloWorld.jsp</result>
     </action>

     <!-- Add actions here -->
  </package>

  <!-- Add packages here -->
</struts>
File struts.xml sẽ có mặt trong classpath của ứng dụng, ta có thể đặt file này trong jar và trong thư mục lib của ứng dụng hoặc cũng có thể đặt nó trong thư mục classes của ứng dụng web Cấu hình file web.xml của ứng dụng web
<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> 
- View Tạo trang HelloWorld.jsp trong thư mục struts2tutorial/pages với nội dụng
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
 <head>
   <title>Struts 2 Hello World Application!</title>
 </head>
 <body>
    <h2><s:property value="message" /></h2>
    <p>Current date and time is: <b><s:property value="currentTime" /></b>
 </body>
</html>
Dòng <%@ taglib prefix="s" uri="/struts-tags" %> khai báo dữ liệu thư viện thẻ của struts. Dữ liệu thẻ của struts được sử dụng để hiển thị dữ liệu động. Thẻ
<s:property value="message"/> và <s :property value="currentTime"/>
gọi lần lượt các phương thức getMessage() và getCurrentTime() của lớp action HelloWorld và kết hợp các giá trị với response (lưu ý: có thể message hoặc Message… các property này sẽ được lấy kết quả từ phương thức get[Property]) 2.2.Luồng hoạt động - Chạy ứng dụng này http://localhost:8080/struts2tutorial/ - Gõ vào link http://localhost:8080/struts2tutorial/exam/HelloWorld.action. Khi đó trình chứa sẽ yêu cầu tài nguyên HelloWorld.action. File web.xml trong ứng dụng này đã cấu hình việc định tuyến tất cả các yêu cầu *.action đều thông qua org.apache.struts2.dispatcher.FilterDispatcher - Framework sẽ tìm mapping cho action HelloWorld, sau đó tạo một thể hiện của lớp thích hợp và gọi phương thức execute() của lớp aciotn đó. Trong trường hợp này là lớp Struts2HelloWorld. Dưới đây là đoạn cấu hình mapping cho action này trong file struts.xml
<action name="HelloWorld" class="java.exam.Struts2HelloWorld">
  <result>/pages/HelloWorld.jsp</result>
</action>

- Phương thức execute() được gọi và trả về SUCCESS Struts Framework xác định trang thích hợp để nạp nếu kết quả SUCCESS được trả về, trong trường hợp này thì HelloWorld.jsp được gọi và sinh ra kết quả hiển thị Trong struts2, action được sử dụng để xử lý form và yêu cầu của người dùng. Phương thức execute của action trả về các giá trị SUCCESS hoặc ERROR hoặc INPUT. Đây là 3 giá trị cơ bản mà framework chỉ cho trình chứa biết để nạp và sinh ra kết quả thích hợp - Trình chứa nạp HelloWorld.jsp và sinh ra kết quả hiển thị - Hiển thị kết quả định dạng HTML

Tham khảo:

http://my.opera.com/nguyend/blog

About Langthang

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment