struts2.1兩天快速入門(第一天上午)

 

第一天上午學習目錄列表:

            一、第一個struts2.1應用開發

            二、Action配置的各項默認值

            三、配置的各種視圖轉發類型

            四、Action屬性注入值

            五、Struts 2.1處理的請求

 

開發環境:MyEclipse+Tomcat6.x

一、第一個struts2應用開發

  1.1、開發Struts 2程序最少需要的JAR如下:

      struts2-core-2.x.x.jar :Struts 2框架的核心類庫
      xwork-core-2.x.x.jar :XWork類庫,Struts 2在其上構建
      ognl-2.6.x.jar :對象圖導航語言(Object Graph Navigation Language),struts2框架通過其讀寫對象的屬性
      freemarker-2.3.x.jar :Struts 2的UI標籤的模板使用FreeMarker編寫
      commons-logging-1.x.x.jar :ASF出品的日誌包,Struts 2框架使用這個日誌包來支持Log4J和JDK 1.4+的日誌記錄。
      commons-fileupload-1.2.1.jar 文件上傳組件,2.1.6版本後必須加入此文件

 

  1.2、Struts2默認的配置文件爲struts.xml ,該文件需要存放在WEB-INF/classes下,該文件的配置模板如下:   

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

    知識提示:此模板可在struts-2.1.6\apps\struts2-blank-2.1.6.war的空白項目裏面拷.

    

  1.3、struts2框架是通過Filter啓動的。他在web.xml中的配置如下:

     

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 <!-- 自從Struts 2.1.3以後,下面的FilterDispatcher已經標註爲過時
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> --> 
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

    知識提示:在StrutsPrepareAndExecuteFilter的init()方法中將會加載類路徑下默認的配置文件struts.xml完成初始化操作,些初始化工作只做一次,在服務器啓動過程中完成.

 

  1.4、第一個Struts2版HelloWorld.

      

        1.4.1、新建cn.gkit.action.HelloWorldAction類,代碼如下:

        

public class HelloWorldAction {
	
	private String message;
	public String getMessage() {
		return message;
	}
                 //返回的是一個字符串
	public String execute(){
		message = "struts2版的HelloWorld";
		return "success";
	}
}

    知識提示:可以看到此Action類並沒有繼承其它類,是一個簡單的POJO類.

 

        1.4.2、action在struts.xml裏的配置:

      

<?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>
   <package name="test" namespace="/test" extends="struts-default">
        <action name="helloworld" class="cn.gkit.action.HelloWorldAction" method="execute" >
	<result name="success">/WEB-INF/jsp/helloworld.jsp</result>
        </action>
    </package>   
</struts>

    知識提示:<package>表示一個包,類似java包. 此包可以被其它包繼承.繼承的屬性是extends. test包就繼承了一個struts的默認包struts-default.struts-default包定義在struts-defautl.xml配置文件裏,可以在struts2核心包根路徑下面找到.namespace屬性表示命名空間,可以把請求路徑相同的一部分抽取出來。如訪問helloworld請求的路徑爲:/struts2.1/test/helloworld,其中struts2.1是指向該應用程序的虛擬目錄(即內容路徑).<result>表示返回的視圖,具體配置會在第三章中講到.

 

         1.4.3、在helloworld.jsp顯示message信息:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>struts2.1版HelloWorld</title>
  </head>
  <body>
    ${message}
  </body>
</html>

     知識提示:用el表達式可以將action裏的屬性取出來.

            

二、Action配置(struts.xml--<action>)的各項默認值

 1>如果沒有爲action指定class屬性,默認是ActionSupport。

 2>如果沒有爲action指定method屬性,默認執行action中的execute() 方法。
 3>如果沒有指定result的name屬性,默認值爲success。

    如在test包裏增加如下一個action請求

<action name="registerUI">
     <result>/WEB-INF/page/register.jsp</result>
</action>

    知識提示:當訪問路徑/struts2.1/test/registerUI時,返回給瀏覽器的就是register.jsp頁面.配置相當於struts1.x,如下:

<action path="/test/regiterUI" forward="/WEB-INF/page/register.jsp"/>

 

三、配置的各種視圖轉發類型

    <result>配置類似於struts1中的<forward>,但struts2中提供了多種結果類型,常用的類型有: dispatcher(默認值)、   redirect(重定向) 、 redirectAction 、 plainText (文本).

 

    3.1、redirect

           但在<result>中還可以使用${屬性名}表達式訪問action中的屬性,表達式裏的屬性名對應action中的屬性。

    將1.4.2裏的struts.xml配置文件裏的<result>改成如下:

<result type="redirect">/helloworld.jsp?message=${message}</result>

    知識提示: 此時的helloworld.jsp應放在WebRoot目錄下才可重定向訪問.而在struts1.x的配置文件裏的是做不到這一點的.  除非使用urlrewrite重寫url,但也不能傳一個action裏的屬性值.  重新訪問/struts2.1/test/helloworld請求,瀏覽器地址將變爲/struts2.1/helloworld.jsp?message=struts2H凥elloWorld.這裏先忽略中文亂碼問題.

 

     3.2、plaintext:顯示原始文件內容,例如:當我們需要原樣顯示jsp文件源代碼的時候,我們可以使用此類型。配置如下:

<result name="source" type="plainText ">
	<param name="location">/view.jsp</param>
	<param name="charSet">UTF-8</param><!-- 指定讀取文件的編碼 -->   
</result>

    知識提示:當訪問該請求時,返回的一個jsp頁面代碼文體視圖,即jsp頁面代碼將不會被執行.

    

      3.3、redirectAction作用與redirect類似,不同的是它重定向的是一個action.

四、Action屬性注入值

 

 屬性注入類似於spring的屬性注入,原理上是一樣的, 都是通過反射技術將xml對應的值設值到相對應的java屬性裏.被注入的屬性也要有對應的setter方法.

1、在HelloWorldAction 類裏增加一下setter方法:

 

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

 2、爲了避免覆蓋,將execute方法裏給message賦值的那一段代碼註釋掉.

 

 3、在struts.xml配置文件的<action>增加一個子標籤<param name="">,配置改成如下:

   <package name="test" namespace="/test" extends="struts-default">
        <action name="helloworld" class="cn.gkit.action.HelloWorldAction" method="execute" >
        	<param name="message">屬性注入</param>
	<result name="success">/WEB-INF/jsp/helloworld.jsp</result>
        </action>
    </package>   

    此時訪問該請求,頁面顯示的mesage屬性值爲:   屬性注入

五、Struts 2處理的請求

      1、指定需要Struts 2處理的請求後綴,配置文件struts.xml配置裏增加一下常量標籤<constant>

  

<?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.action.extension" value="do"/>
</struts>

      

      2、如果用戶需要指定多個請求後綴,則多個後綴之間以英文逗號隔開。如:

   

<constant name="struts.action.extension" value="do,htm"/>

 

     3、常量名struts.action.extension可以在default.properties裏找到,裏面包含了struts2默認的常量值.default.properties可以在struts2核心包下的org.apache.struts2下找到.

 

第一天上午內容結束.

 

下午內容目錄如下:

        六、Struts2的處理流程與Action的管理方式

        七、動態方法調用和使用通配符定義action

        八、自定義類型轉換器

        九、自定義攔截器

        十、文件上傳

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