從實例理解Struts2

       先是一個最最簡單的例子,在瀏覽器中請求一個action,然後返回一個字符串到jsp頁面上顯示出來。


第一:創建web項目,引入struts2要的jar包,目錄如下:

            


第二:web.xml中配置struts2的核心攔截器

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"   
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  <welcome-file-list>  
    <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>  
  <filter>  
    <filter-name>struts2</filter-name>  
    <!--定義struts2的核心Filter的實現類 -->  
    <filter-class>  
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
    </filter-class>  
  </filter>  
  <filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping></web-app>  


第三:MyEclipse項目中的src根目錄下建立一個struts.xml文件

        可以打開struts2安裝包裏的apps目錄下的任意一個jar包,在裏面的WEB_INFR/src目錄下,尋找struts.xml文件,將該文件複製進項目的src根目錄下,將裏面的內容清空(只留下<struts>標籤和頭部標籤即可

<?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>


    <!-- Struts 2 的Action必須放在包空間下 -->  
     <package name="strutsqs" namespace="/zd" extends="struts-default">   
        <!-- 定義action的名字以及action的實現類 -->  
         <action name="index" class="com.zhudan.test.testaction" method="test" >   
            <!-- 定義action的處理結果result,result有兩個屬性,其中name指定返回名稱,tyle指定返回的類型 -->  
                    <result name="suc">/index.jsp</result>   
        </action>   
     </package>   
</struts>
 主要屬性說明:

         package-name:用於區別不同的package;必須是唯一的、可用的變量名;用於其它package來繼承;

         package--namespace:用於減少重複代碼(和struts1比較);是調用action時輸入路徑的組成部分;

         package--extends:用於繼承其它package以使用裏面的過濾器等東東;

         action--name:用於在一個package裏區別不同的action;必須是唯一的、可用的變量名;是調用action時輸入路徑的組成部分;

         action--class:action所在的路徑(包名+類名);

         action--method:action所調用的方法名;
還有其它的屬性,因爲項目裏沒有用到,就沒有解釋。如有需要,請查閱相關文檔。


第四:編寫action類

package com.zhudan.test;

import com.opensymphony.xwork2.ActionSupport;

public class testaction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	//調用test方法,返回index.jsp頁面
	public String test() throws Exception {
		// TODO Auto-generated method stub
		return "suc";
	}
	
}


第五:簡單的jsp頁面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
    測試成功!<br>
  </body>
</html>


第六:啓動tomact,訪問地址:http://localhost:8080/Struts2Test/zd/index.action

        Struts2Test:項目名字
        zd:命名空間,對應namespace裏面的字符串。
        index.action:index對應action裏面的字符串,“.action”表示請求的是一個action。


第七:運行機制

       1)客戶端在瀏覽器中輸入一個url地址;

       2)這個url請求通過http協議發送給tomcat;

       3)tomcat根據url找到對應項目裏面的web.xml文件;

       4)在web.xml裏面會發現有struts2的配置;

       5)然後會找到struts2對應的struts.xml配置文件;

       6)根據url解析struts.xml配置文件就會找到對應的class;

       7)調用完class返回一個字String,根據struts.xml返回到對應的jsp。


第八:strus2大概流程

       1)  客戶端初始化一個指向Servlet容器(例如Tomcat)的請求。

       2)  這個請求經過一系列的過濾器(Filter)。

       3)  接着StrutsPrepareAndExecuteFilter被調用,StrutsPrepareAndExecuteFilter詢問ActionMapper來決定這個請是否需要調用某個Action。

       4)  如果ActionMapper決定需要調用某個Action,StrutsPrepareAndExecuteFilter把請求的處理交給ActionProxy。

       5)  ActionProxy通過Configuration Manager詢問框架的配置文件,找到需要調用的Action類。

       6)  ActionProxy創建一個ActionInvocation的實例。

       7)  ActionInvocation實例使用命名模式來調用,在調用Action的過程前後,涉及到相關攔截器(Intercepter)的調用。

       8)  一旦Action執行完畢,ActionInvocation負責根據struts.xml中的配置找到對應的返回結果。


總結:

       Struts2的核心就是攔截器StrutsPrepareAndExecuteFilter。Struts.xml中所有的package都要extends="struts-default"。同理所有的Java類都要extends自Object一樣。struts-default.xml裏面就是要做以上事情。

   



發佈了160 篇原創文章 · 獲贊 49 · 訪問量 104萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章