Struts 2 簡介 Struts2的基本流程

Struts 2是Struts的下一代產品,是在 struts 1和WebWork的技術基礎上進行了合併的全新的Struts 2框架。Struts 2以WebWork爲核心,採用攔截器的機制來處理用戶的請求,這樣的設計也使得業務邏輯控制器能夠與ServletAPI完全脫離開,所以Struts 2可以理解爲WebWork的更新產品。

 

Struts2的基本流程如下:

 

① Web瀏覽器 發送一個請求(HttpServletRequest)。

 

② 過濾器Dispatcher查找請求,確定適當的Action。

 

③ 攔截器自動對請求應用通用功能,如驗證和文件上傳等操作。

 

④ Action的execute方法通常用來存儲和(或)重新獲得信息(通過數據庫)。

 

⑤ 結果被返回到瀏覽器,可能是HTML、圖片、PDF或其他。

 

下面以一個簡單的實例來說說struts2。

 

首先將strut2所需的jar包導入


接着修改web.xml文件,Struts1的入口點是一個Servlet,而Struts2的入口點是一個過濾器(Filter)。因此,Struts2要按過濾器的方式配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/JavaEE/web-app_2_5.xsd">  
  7.     <filter>  
  8.         <filter-name>struts 2</filter-name>  
  9.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  10.     </filter>  
  11.     <filter-mapping>  
  12.         <filter-name>struts 2</filter-name>  
  13.         <url-pattern>/*</url-pattern>  
  14.     </filter-mapping>  
  15. </web-app>  

創建hello.jsp

 

  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <html>  
  3. <head>  
  4.         <title>struts 2應用</title>  
  5. </head>  
  6. <body>  
  7.         <form action="struts.action" method="post">  
  8.             請輸入姓名:<input type="text" name="name"/><br>  
  9.         <input type="submit" value="提交"/>  
  10.         </form>  
  11. </body>  
  12. </html>  

然後實現Action類,該類需要從com.opensymphony.xwork2.ActionSupport類繼承
  1. package org.action;  
  2. import java.util.Map;  
  3. import com.opensymphony.xwork2.ActionContext;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5. public class StrutsAction extends ActionSupport{  
  6.     private String name;  
  7.     public String getName() {  
  8.         return name;  
  9.     }  
  10.     public void setName(String name) {  
  11.         this.name=name;  
  12.     }  
  13.     public String execute() throws Exception {  
  14.         if(!name.equals("HelloWorld")){  
  15.             Map request=(Map)ActionContext.getContext().get("request");  
  16.             request.put("name",getName());  
  17.             return "success";  
  18.         }else{  
  19.             return "error";  
  20.         }  
  21.     }  
  22. }  

創建struts.xml文件,就是配置Action類

 

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <package name="default" extends="struts-default">  
  7.         <action name="struts" class="org.action.StrutsAction">  
  8.             <result name="success">/welcome.jsp</result>  
  9.             <result name="error">/hello.jsp</result>  
  10.         </action>  
  11.     </package>  
  12. </struts>  

其中<package>標籤中name標籤是指定包名,這個名字將作爲引用該包的鍵。注意,包的名字必須是唯一的,在一個struts.xml文件中不能出現兩個同名的包。extends屬性繼承一個默認的配置文件“struts-default”。

<action>標籤中的name屬性表示動作名,class表示動作類名。

<result>標籤的name實際上就是execute方法返回的字符串,如果返回的是“success”,就跳轉到welcome.jsp頁面,如果是“error”,就跳轉到hello.jsp頁面。

MyEclipse 9.0還提供了.xml文件的可視化功能,由XML文件編輯工作區切換到左下角的【Flow】選項頁(如圖5.4),就可以看到這個struts.xml文件的可視化流程圖


創建welcome.jsp

 

  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="/struts-tags" prefix="s" %>  
  3. <html>  
  4. <head>  
  5.     <title>struts 2應用</title>  
  6. </head>  
  7. <body>  
  8.     hello <s:property value="#request.name"/>!  
  9. </body>  
  10. </html>  

部署和運行

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章