struts part1

phrash1 2012-5-9 start------------------
1.配置Struts步驟:1.將現有項目中的struts.xml拷貝到src包下;2.將struts用到的.jar文件拷貝到lib包

下;3.配置web.xml,拷貝filter的兩段配置即<filter>...</filter> 和<filter-mapping>...</filter-mapping>

 

2.ng即next generation,struts2.0用的filter爲dispatcherfilter;struts2.1用的filter爲

strutsprepareandexecutefilter.

 

3.struts.xml下添加<package ...>...</package>裏的action等,struts.xml中的package類似於j2se中的

package,package中result默認爲name="success"

 

4.查看源代碼(或javadoc)配置,在reference libraries右鍵--properties--java attachment--location path

 

5.xml語法提示設置:window-preference-type filter text輸入xml-xml catalog--add..--struts-dtd
   jsp中文配置:window-preference-jsp-encoding選擇Chinese National Standard

 

6.struts是將請求與視圖分離;執行過程爲:url--http--tomcat--webapp--web.xml--filter--struts.xml--

namespace--action--result ,namespace決定了action的訪問路徑,默認爲"",可接收所有路徑的

action,namespace 可以寫成/或/xxx或/xxx/yyy,對應的action訪問路徑爲/index.action

或/xxx/index.action或/xxx/yyy/index.action,namespace 最好以模塊來進行命名

 

7.設計模式好處:靈活性,易擴展

 

8.在myeclipse中拷貝web項目時訪問不生效,可能是新項目右鍵preference--myeclipse--web--web

context root沒有變成最新的項目名稱。另引入已有項目時可能JRE版本路徑會有不一樣,可以先刪

除再導入現在的JRE

 

9.具體視圖action的返回可以用戶通過class來指定,如<action name="index" class="xxx1">或<action

name="index" class="xxx2">,類xxx1或xxx2中只要有execute()方法且其返回值爲String時,此類就

被struts認爲是一個action。struts1只new一次,以後每次訪問都爲同一個class,會有同步問題;而

struts2每訪問一次就new一個class。如果沒有配置class則默認調用ActionSupport類(此類在xwork-

2.1.2.jar包中)中的execute()方法。xxx1類還可以實現Action接口或繼承ActionSupport類,開發中使用

最後一種方式即繼承ActionSupport類,前兩種都用不了ActionSupport類中的其他方法。

 

10.訪問路徑問題:basePath 默認jsp文件會設置好,可以在<head>中添加<base href="<%

=basePath%>"/>,就能處理頁面跳轉路徑問題,redirect也可以,但可以忽略。統一使用絕對路徑(在

jsp中用request.getContextRoot方法得到webapp路徑或使用myeclipse常用的指定basePath),struts中

路徑是根據action的路徑而不是jsp路徑來確定的,所以儘量不使用相對路徑。

phrash1 2012-5-9 end------------------

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