環境
1 、開發環境Eclipse +Myeclipse
2、struts2.01(下文指struts)
3、resin3.1(先擇的原因是支持自動編譯,感覺比tomcat強)
在apache中下載struts2.0.1版本,struts的發布包中一般都帶有例子,我們先把struts2-blank-2.0.1.war這個最簡單的例子解壓并把該項目的相關文件移值到myeclipse開發環竟底下。操作步驟如下:
1 、在myeclipse新建一個Web項目。把struts2-blank-2.0.1\WEB-INF\lib目錄下的jar文件拷貝到新建的Web項目的lib目錄下。
2、把struts.properties,struts.xml考到新建項目的src目錄里。在myeclipse把新的項目發布到resin3.1的deploy目錄里(自動發布的,但我還不懂怎么配置讓resin3.1自動發布delpoy目錄里的項目)把deploy目錄里我們發布的項目拷貝到resin3.1的webapps目錄里。
3、把eclipse自動編譯選項取消,把新建項目的WebRoot刪掉,重新建立一個該名字的虛擬目錄,連接到我們拷貝到resin3.1的webpps目錄中我們發布的目錄,把eclipse中重選擇自動編譯。
這是開發環竟的拾建。接下來慢慢介紹struts2的應用。
========================================================
struts.properties文件配置內容如下:
struts.action.extension
??????????The?URL?extension?to?use?to?determine?if?the?request?is?meant?for?a?Struts?action?
???????????用URL擴展名來確定是否這個請求是被用作Struts?action,其實也就是設置?action的后綴,例如login.do的'do'字。
struts.configuration
??????????The?org.apache.struts2.config.Configuration?implementation?class
????????????org.apache.struts2.config.Configuration接口名
struts.configuration.files
??????????A?list?of?configuration?files?automatically?loaded?by?Struts?
???????????struts自動加載的一個配置文件列表
struts.configuration.xml.reload
??????????Whether?to?reload?the?XML?configuration?or?not
???????????是否加載xml配置(true,false)
struts.continuations.package
???????????The?package?containing?actions?that?use?Rife?continuations
???????????含有actions的完整連續的package名稱
struts.custom.i18n.resources
??????????Location?of?additional?localization?properties?files?to?load?
???????????加載附加的國際化屬性文件(不包含.properties后綴)
struts.custom.properties
??????????Location?of?additional?configuration?properties?files?to?load
???????????加載附加的配置文件的位置
struts.devMode
??????????Whether?Struts?is?in?development?mode?or?not
???????????是否為struts開發模式
struts.dispatcher.parametersWorkaround
??????????Whether?to?use?a?Servlet?request?parameter?workaround?necessary?for?some?versions?of?WebLogic
????????????(某些版本的weblogic專用)是否使用一個servlet請求參數工作區(PARAMETERSWORKAROUND)
struts.enable.DynamicMethodInvocation
??????????Allows?one?to?disable?dynamic?method?invocation?from?the?URL
????????????允許動態方法調用
struts.freemarker.manager.classname
??????????The?org.apache.struts2.views.freemarker.FreemarkerManager?implementation?class?
???????????org.apache.struts2.views.freemarker.FreemarkerManager接口名
struts.i18n.encoding
??????????The?encoding?to?use?for?localization?messages
???????????國際化信息內碼
struts.i18n.reload
??????????Whether?the?localization?messages?should?automatically?be?reloaded
???????????是否國際化信息自動加載?
struts.locale
??????????The?default?locale?for?the?Struts?application
???????????默認的國際化地區信息
struts.mapper.class
??????????The?org.apache.struts2.dispatcher.mapper.ActionMapper?implementation?class
????????????org.apache.struts2.dispatcher.mapper.ActionMapper接口
struts.multipart.maxSize
??????????The?maximize?size?of?a?multipart?request?(file?upload)
???????????multipart請求信息的最大尺寸(文件上傳用)?
struts.multipart.parser
??????????The?org.apache.struts2.dispatcher.multipart.MultiPartRequest?parser?implementation?for?a?multipart?request?(file?upload)?
??????????專為multipart請求信息使用的org.apache.struts2.dispatcher.multipart.MultiPartRequest解析器接口(文件上傳用)
struts.multipart.saveDir
??????????The?directory?to?use?for?storing?uploaded?files?
???????????設置存儲上傳文件的目錄夾
struts.objectFactory
??????????The?com.opensymphony.xwork2.ObjectFactory?implementation?class
???????????com.opensymphony.xwork2.ObjectFactory接口(spring)
struts.objectFactory.spring.autoWire
??????????Whether?Spring?should?autoWire?or?not
???????????是否自動綁定Spring
struts.objectFactory.spring.useClassCache
??????????Whether?Spring?should?use?its?class?cache?or?not
???????????是否spring應該使用自身的cache?
struts.objectTypeDeterminer
??????????The?com.opensymphony.xwork2.util.ObjectTypeDeterminer?implementation?class
????????????com.opensymphony.xwork2.util.ObjectTypeDeterminer接口
struts.serve.static.browserCache
??????????If?static?content?served?by?the?Struts?filter?should?set?browser?caching?header?properties?or?not?
???????????是否struts過濾器中提供的靜態內容應該被瀏覽器緩存在頭部屬性中
struts.serve.static
??????????Whether?the?Struts?filter?should?serve?static?content?or?not?
???????????是否struts過濾器應該提供靜態內容
struts.tag.altSyntax
??????????Whether?to?use?the?alterative?syntax?for?the?tags?or?not?
???????????是否可以用替代的語法替代tags
struts.ui.templateDir
??????????The?directory?containing?UI?templates
???????????UI?templates的目錄夾?
struts.ui.theme
??????????The?default?UI?template?theme
???????????默認的UI?template主題
struts.url.http.port
??????????The?HTTP?port?used?by?Struts?URLs
???????????設置http端口
struts.url.https.port
??????????The?HTTPS?port?used?by?Struts?URLs?
???????????設置https端口
struts.url.includeParams
??????????The?default?includeParams?method?to?generate?Struts?URLs?
??????????在url中產生?默認的includeParams
struts.velocity.configfile
??????????The?Velocity?configuration?file?path
???????????velocity配置文件路徑
struts.velocity.contexts
??????????List?of?Velocity?context?names
???????????velocity的context列表
struts.velocity.manager.classname
??????????org.apache.struts2.views.velocity.VelocityManager?implementation?class
???????????org.apache.struts2.views.velocity.VelocityManager接口名
struts.velocity.toolboxlocation
??????????The?location?of?the?Velocity?toolbox
???????????velocity工具盒的位置?
struts.xslt.nocache
??????????Whether?or?not?XSLT?templates?should?not?be?cached
???????????是否XSLT模版應該被緩存
??????????The?URL?extension?to?use?to?determine?if?the?request?is?meant?for?a?Struts?action?
???????????用URL擴展名來確定是否這個請求是被用作Struts?action,其實也就是設置?action的后綴,例如login.do的'do'字。
struts.configuration
??????????The?org.apache.struts2.config.Configuration?implementation?class
????????????org.apache.struts2.config.Configuration接口名
struts.configuration.files
??????????A?list?of?configuration?files?automatically?loaded?by?Struts?
???????????struts自動加載的一個配置文件列表
struts.configuration.xml.reload
??????????Whether?to?reload?the?XML?configuration?or?not
???????????是否加載xml配置(true,false)
struts.continuations.package
???????????The?package?containing?actions?that?use?Rife?continuations
???????????含有actions的完整連續的package名稱
struts.custom.i18n.resources
??????????Location?of?additional?localization?properties?files?to?load?
???????????加載附加的國際化屬性文件(不包含.properties后綴)
struts.custom.properties
??????????Location?of?additional?configuration?properties?files?to?load
???????????加載附加的配置文件的位置
struts.devMode
??????????Whether?Struts?is?in?development?mode?or?not
???????????是否為struts開發模式
struts.dispatcher.parametersWorkaround
??????????Whether?to?use?a?Servlet?request?parameter?workaround?necessary?for?some?versions?of?WebLogic
????????????(某些版本的weblogic專用)是否使用一個servlet請求參數工作區(PARAMETERSWORKAROUND)
struts.enable.DynamicMethodInvocation
??????????Allows?one?to?disable?dynamic?method?invocation?from?the?URL
????????????允許動態方法調用
struts.freemarker.manager.classname
??????????The?org.apache.struts2.views.freemarker.FreemarkerManager?implementation?class?
???????????org.apache.struts2.views.freemarker.FreemarkerManager接口名
struts.i18n.encoding
??????????The?encoding?to?use?for?localization?messages
???????????國際化信息內碼
struts.i18n.reload
??????????Whether?the?localization?messages?should?automatically?be?reloaded
???????????是否國際化信息自動加載?
struts.locale
??????????The?default?locale?for?the?Struts?application
???????????默認的國際化地區信息
struts.mapper.class
??????????The?org.apache.struts2.dispatcher.mapper.ActionMapper?implementation?class
????????????org.apache.struts2.dispatcher.mapper.ActionMapper接口
struts.multipart.maxSize
??????????The?maximize?size?of?a?multipart?request?(file?upload)
???????????multipart請求信息的最大尺寸(文件上傳用)?
struts.multipart.parser
??????????The?org.apache.struts2.dispatcher.multipart.MultiPartRequest?parser?implementation?for?a?multipart?request?(file?upload)?
??????????專為multipart請求信息使用的org.apache.struts2.dispatcher.multipart.MultiPartRequest解析器接口(文件上傳用)
struts.multipart.saveDir
??????????The?directory?to?use?for?storing?uploaded?files?
???????????設置存儲上傳文件的目錄夾
struts.objectFactory
??????????The?com.opensymphony.xwork2.ObjectFactory?implementation?class
???????????com.opensymphony.xwork2.ObjectFactory接口(spring)
struts.objectFactory.spring.autoWire
??????????Whether?Spring?should?autoWire?or?not
???????????是否自動綁定Spring
struts.objectFactory.spring.useClassCache
??????????Whether?Spring?should?use?its?class?cache?or?not
???????????是否spring應該使用自身的cache?
struts.objectTypeDeterminer
??????????The?com.opensymphony.xwork2.util.ObjectTypeDeterminer?implementation?class
????????????com.opensymphony.xwork2.util.ObjectTypeDeterminer接口
struts.serve.static.browserCache
??????????If?static?content?served?by?the?Struts?filter?should?set?browser?caching?header?properties?or?not?
???????????是否struts過濾器中提供的靜態內容應該被瀏覽器緩存在頭部屬性中
struts.serve.static
??????????Whether?the?Struts?filter?should?serve?static?content?or?not?
???????????是否struts過濾器應該提供靜態內容
struts.tag.altSyntax
??????????Whether?to?use?the?alterative?syntax?for?the?tags?or?not?
???????????是否可以用替代的語法替代tags
struts.ui.templateDir
??????????The?directory?containing?UI?templates
???????????UI?templates的目錄夾?
struts.ui.theme
??????????The?default?UI?template?theme
???????????默認的UI?template主題
struts.url.http.port
??????????The?HTTP?port?used?by?Struts?URLs
???????????設置http端口
struts.url.https.port
??????????The?HTTPS?port?used?by?Struts?URLs?
???????????設置https端口
struts.url.includeParams
??????????The?default?includeParams?method?to?generate?Struts?URLs?
??????????在url中產生?默認的includeParams
struts.velocity.configfile
??????????The?Velocity?configuration?file?path
???????????velocity配置文件路徑
struts.velocity.contexts
??????????List?of?Velocity?context?names
???????????velocity的context列表
struts.velocity.manager.classname
??????????org.apache.struts2.views.velocity.VelocityManager?implementation?class
???????????org.apache.struts2.views.velocity.VelocityManager接口名
struts.velocity.toolboxlocation
??????????The?location?of?the?Velocity?toolbox
???????????velocity工具盒的位置?
struts.xslt.nocache
??????????Whether?or?not?XSLT?templates?should?not?be?cached
???????????是否XSLT模版應該被緩存
其它配置文件介紹
一、Struts2配置文件?
Struts2相關的配置文件有web.xml,struts.xml,struts.properties,?
struts-default.xml,velocity.properties,struts-default.vm。其中web.xml,struts.xml是必須的,其它的配置文件可選擇。它們在web應用中的功能如下:?
web.xml:包含所有必須的框架組件的web部署描述符。?
Struts.xml:配置包含result/view類型、action映射、攔截器等的Struts2的主要配置文件。?
Struts.properties:配置struts2的框架屬性。?
Struts-default.xml:在文件在struts-action-x.x.jar中,該文件是應該被包含在struts.xml中的缺省配置。?
Welocity.properties:重寫了velocity的配置文件。?
Struts-default.vm:相對于velocity的缺省配置。
二、Struts2配置元素?
Struts2核心的配置文件是缺省的struts.xml。?
必要的時候,缺省的配置文件可以包含其它的配置文件;struts文件可以放入jar中,并自動插入應用程序,這樣每個模塊可以包含自己的配置文件并自動配置。在Freemarker和Velocity模塊中,模板也能從classpath中加載,所以整個模塊可以作為一個簡單的jar文件被包含。?
Struts.xml配置文件可以包含Interceptor、Action類和Results。?
Struts.xml配置元素說明:
1、Packages?
Packages:packages把actions、results、results?types、interceptors?
和interceptor-stacks組裝到一個邏輯單元中,從概念上講,packages就像一個對象,可以被其它子包從寫,而且可以擁有自己獨立的部分。?
Name屬性是packages的必填元素,它作為一個關鍵字被后邊的包引用;extends元素是可選的,它允許包擴展一個和多個前邊定義的包。注意,?
struts.xml文件是至上而下處理的,所有被擴展的包,需要在擴展包前定義。?
Abstract元素是可選的,它可以申明一個不包含actions的配置文件。
2、Namespace?
Namespace元素把actions細分到邏輯模塊,每一個namespace都有自己的?
前綴(prefix),namespace避免了action之間的名字沖突,當前綴出現在URI中時,這些標簽都是名字空間感知的(“namespace?aware”),所以這些namespace?prefix不必嵌入到表單或連接中。?
Default的namespace是一個空字符串“”,如果在指定的配置文件中,沒有找到action,缺省的namespace也會被查找。Local/global策略允許應用程序在action?“extends”元素層次結構之外,有全局的action配置。缺省的namespace也可以不在package中申明。?
Namespace?prefix可以注冊為java的申明式安全,以確保授權的用戶才能訪問namespace的資源。?
Root?namespace(“/”)也被支持,root就是當直接請求context?path的時候的namespace。
[個人理解:namespace的用法就像struts1.x中的path一樣,只不過它在package中什么路徑,而在action中申明action名子罷了]
3、Include?
Include元素使得框架能應用“divide?and?conquer”來配置文件。被include?
的每個配置文件必須和struts.xml有一樣的格式,一個大的項目可以采用這樣方式來組織程序模塊。?
Include元素也可以和package交替出現,框架將按照順序加載配置文件。
4、Interceptor?configuration?
Interceptor允許應用程序在Action方法執行前后定義執行代碼,?
Interceptor在應用程序開發中十分重要,對于Interceptor有許多用例,如validation,?property?population,?security,?logging,?和profiling。?
Interceptor被定義為一個Java類,Interceptor也可以組裝成Interceptor-stack,他們將按照定義的順序執行。?
在struts-default.xml中定義了一些缺省的Interceptor-stack,以便框架能很好地運行。
5、Action?
Action是框架的“工作單元”。Action可以指定一個Interceptor-stack、?
一序列的return?type和一序列的異常處理,但只有name屬性是必須的。?
one.xml配置Struts2相關的配置文件有web.xml,struts.xml,struts.properties,?
struts-default.xml,velocity.properties,struts-default.vm。其中web.xml,struts.xml是必須的,其它的配置文件可選擇。它們在web應用中的功能如下:?
web.xml:包含所有必須的框架組件的web部署描述符。?
Struts.xml:配置包含result/view類型、action映射、攔截器等的Struts2的主要配置文件。?
Struts.properties:配置struts2的框架屬性。?
Struts-default.xml:在文件在struts-action-x.x.jar中,該文件是應該被包含在struts.xml中的缺省配置。?
Welocity.properties:重寫了velocity的配置文件。?
Struts-default.vm:相對于velocity的缺省配置。
二、Struts2配置元素?
Struts2核心的配置文件是缺省的struts.xml。?
必要的時候,缺省的配置文件可以包含其它的配置文件;struts文件可以放入jar中,并自動插入應用程序,這樣每個模塊可以包含自己的配置文件并自動配置。在Freemarker和Velocity模塊中,模板也能從classpath中加載,所以整個模塊可以作為一個簡單的jar文件被包含。?
Struts.xml配置文件可以包含Interceptor、Action類和Results。?
Struts.xml配置元素說明:
1、Packages?
Packages:packages把actions、results、results?types、interceptors?
和interceptor-stacks組裝到一個邏輯單元中,從概念上講,packages就像一個對象,可以被其它子包從寫,而且可以擁有自己獨立的部分。?
Name屬性是packages的必填元素,它作為一個關鍵字被后邊的包引用;extends元素是可選的,它允許包擴展一個和多個前邊定義的包。注意,?
struts.xml文件是至上而下處理的,所有被擴展的包,需要在擴展包前定義。?
Abstract元素是可選的,它可以申明一個不包含actions的配置文件。
2、Namespace?
Namespace元素把actions細分到邏輯模塊,每一個namespace都有自己的?
前綴(prefix),namespace避免了action之間的名字沖突,當前綴出現在URI中時,這些標簽都是名字空間感知的(“namespace?aware”),所以這些namespace?prefix不必嵌入到表單或連接中。?
Default的namespace是一個空字符串“”,如果在指定的配置文件中,沒有找到action,缺省的namespace也會被查找。Local/global策略允許應用程序在action?“extends”元素層次結構之外,有全局的action配置。缺省的namespace也可以不在package中申明。?
Namespace?prefix可以注冊為java的申明式安全,以確保授權的用戶才能訪問namespace的資源。?
Root?namespace(“/”)也被支持,root就是當直接請求context?path的時候的namespace。
[個人理解:namespace的用法就像struts1.x中的path一樣,只不過它在package中什么路徑,而在action中申明action名子罷了]
3、Include?
Include元素使得框架能應用“divide?and?conquer”來配置文件。被include?
的每個配置文件必須和struts.xml有一樣的格式,一個大的項目可以采用這樣方式來組織程序模塊。?
Include元素也可以和package交替出現,框架將按照順序加載配置文件。
4、Interceptor?configuration?
Interceptor允許應用程序在Action方法執行前后定義執行代碼,?
Interceptor在應用程序開發中十分重要,對于Interceptor有許多用例,如validation,?property?population,?security,?logging,?和profiling。?
Interceptor被定義為一個Java類,Interceptor也可以組裝成Interceptor-stack,他們將按照定義的順序執行。?
在struts-default.xml中定義了一些缺省的Interceptor-stack,以便框架能很好地運行。
5、Action?
Action是框架的“工作單元”。Action可以指定一個Interceptor-stack、?
一序列的return?type和一序列的異常處理,但只有name屬性是必須的。?
<?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="one"??extends="struts-default">
????????<default-interceptor-ref?name="defaultStack"?/>?
????????<action?name="hello"??class="one.HelloWorld">
????????????<result?name="success"?>/hello.jsp</result>
????????</action>
????</package>
</struts>
struts.xml<!DOCTYPE?struts?PUBLIC
????????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"
????????"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
????<package?name="one"??extends="struts-default">
????????<default-interceptor-ref?name="defaultStack"?/>?
????????<action?name="hello"??class="one.HelloWorld">
????????????<result?name="success"?>/hello.jsp</result>
????????</action>
????</package>
</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>
????<include??file="one.xml"/>
????<include?file="webwork-default.xml"?/>
????<package?name="default"?extends="webwork-default">
????</package>
</struts>
<!DOCTYPE?struts?PUBLIC
????????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"
????????"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
????<include??file="one.xml"/>
????<include?file="webwork-default.xml"?/>
????<package?name="default"?extends="webwork-default">
????</package>
</struts>
struts.properties
struts.devMode?=?true
struts.enable.DynamicMethodInvocation?=?false
struts.enable.DynamicMethodInvocation?=?false
HelloWorld.java
package?one;
import?com.opensymphony.xwork2.ActionSupport;
public?class?HelloWorld?extends?ActionSupport{
????private?String?message;
????public?void?setMessage(String?message)?{
????????this.message?=?message;
????}
????
????public?String?getMessage()?{
????????return?message;
????}
????public?String?execute(){
????????System.out.println("execute?

測試

..");
????????message="Hello?peidw";
????????
????????return?ActionSupport.SUCCESS;
????}
}
就這么簡單,還有一個就hello.jsp頁面了,該頁面的作用是顯示了上類的message屬性值。import?com.opensymphony.xwork2.ActionSupport;
public?class?HelloWorld?extends?ActionSupport{
????private?String?message;
????public?void?setMessage(String?message)?{
????????this.message?=?message;
????}
????
????public?String?getMessage()?{
????????return?message;
????}
????public?String?execute(){
????????System.out.println("execute?






????????message="Hello?peidw";
????????
????????return?ActionSupport.SUCCESS;
????}
}
hello.jsp文件內容如下:
<%@?page?language="java"?contentType="text/html;?charset=gb2312"
????pageEncoding="gb2312"%>
<%@?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=gb2312">
<title>sruts2?</title>
</head>
<body>
????hello?<s:property?value="message"/>
</body>
</html>
????pageEncoding="gb2312"%>
<%@?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=gb2312">
<title>sruts2?</title>
</head>
<body>
????hello?<s:property?value="message"/>
</body>
</html>