熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts2教程3:struts.xml常用配置解析

2022-06-13   來源: Java開源技術 

  使用<include>標簽重用配置文件

  在Struts中提供了一個默認的strutsxml文件但如果packageactioninterceptors等配置比較多時都放到一個strutsxml文件不太容易維護因此就需要將strutsxml文件分成多個配置文件然後在strutsxml文件中使用<include>標簽引用這些配置文件這樣做的優點如下

  結構更清晰更容易維護配置信息

  配置文件可以復用如果在多個Web程序中都使用類似或相同的配置文件那麼可以使用<include>標簽來引用這些配置文件這樣可以減少工作量

  假設有一個配置文件文件名為newstrutsxml代碼如下

   <?xmlversion=encoding=UTF?>
<!DOCTYPEstrutsPUBLIC
//ApacheSoftwareFoundation//DTDStrutsConfiguration//EN
dtd
<struts>
<packagename=demoextends=strutsdefault
<actionname=submit class=actionMoreSubmitAction
<resultname=save
/resultjsp
</result>
<resultname=print
/resultjsp
</result>
</action>
</package>
</struts>

  則strutsxml引用newstrutsxml文件的代碼如下

  

  <?xmlversion=encoding=UTF?>
<!DOCTYPEstrutsPUBLIC
//ApacheSoftwareFoundation//DTDStrutsConfiguration//EN
dtd
<struts>
<includefile=newstrutsxml/>
<packagename=testextends=strutsdefault
……
</package>
</struts>

  大家要注意一下用<include>引用的xml文件也必須是完成的struts的配置實際上<include>在引用時是單獨解析的xml文件而不是將被引用的文件插入到strutsxml文件中

  action的別名

  在默認情況下Struts會調用動作類的execute方法但有些時候我們需要在一個動作類中處理不同的動作也就是用戶請求不同的動作時執行動作類中的不同的方法為了達到這個目的可以在<action>標簽中通過method方法指定要指行的動作類的方法名並且需要為不同的動作起不同的名子(也稱為別名)如下面代碼所示

  

  <?xmlversion=encoding=UTF?>
<!DOCTYPEstrutsPUBLIC
 //ApacheSoftwareFoundation//DTDStrutsConfiguration//EN
 dtd
<struts>
<packagename=demoextends=strutsdefault
<actionname=test class=actionMyAction
……
</action>
<actionname=my class=actionMyActionmethod=my
……
</action>
</package>
</struts>

  上面代碼的兩個動作的class屬性都指向同一個類name為這個類起了兩個動作別名test和my在動作my中使用了method屬性指定要要運行的方法名為my

  在MyAction類中必須要有my方法代碼如下

  

  packageaction;
importcomopensymphonyxworkActionSupport;
publicclassMyActionextendsActionSupport
{
……
publicStringexecute()throwsException
{
//處理test動作的代碼
}
publicStringmy()throwsException
{
 //處理my動作的代碼
}
……
}

  除了在strutsxml中配置別名還可以通過請求參數來描述指定動作(並不需要在strutsxml中配置)請求參數的格式如下

  //localhost/contextPath/actionName!methodaction

  關於通過請求指定動作的詳細內容請參閱筆者寫的《Struts教程處理一個form多個submit》

  為action指定參數

  在struts中還可以為action指定一個或多個參數大家還記著strutsx是如何設置的action參數不? 在strutsx中可以使用<action>標簽的parameter屬性為其指定一個action參數如果要指定多個就只能通過逗號()或其他的分隔符將不同的參數隔開而在struts中可以通過<param>標簽指定任意多個參數代碼如下

  

  <actionname=submit class=actionMyAction
<paramname=param>value</param>
<paramname=param>value</param>
<resultname=save
/resultjsp
</result>
 ……
</action>

  當然在action中讀這些參數也非常簡單只需要象獲取請求參數一樣在action類中定義相應的setter方法即可(一般不用定義getter方法)如下面的代碼將讀取param和param參數的值

   packageaction;
importcomopensymphonyxworkActionSupport;
publicclassMyActionextendsActionSupport
{
privateStringparam;
privateStringparam;
publicStringexecute()throwsException
{
Systemoutprintln(param+param);
}
publicvoidsetParam(Stringparam)
{
thisparam=param;
}
publicvoidsetParam(Stringparam)
{
thisparam=param;
}
……
}

  當struts在調用execute之前param和param的值就已經是相應參數的值了因此在execute方法中可以直接使用param和param

  選擇result類型

  在默認時標簽的type屬性值是dispatcher(實際上就是轉發forward)開發人員可以根據自己的需要指定不同的類型如redirectstream等如下面代碼所示

  

  <result name=savetype=redirect
  /resultjsp
</result>

  這此resulttype可以在strutscorejar包或struts源代碼中的strutsdefaultxml文件中找到在這個文件中找到<resulttypes>標簽所有的resulttype都在裡面定義了代碼如下

  

  <resulttypes>
 <resulttypename=chainclass=comopensymphonyxworkActionChainResult/>
 <resulttypename=dispatcherclass=orgapachestrutsdispatcherServletDis
patcherResultdefault=true/>
 <resulttypename=freemarkerclass=orgapachestrutsviewsfreemarkerFree
markerResult/>
 <resulttypename=httpheaderclass=orgapachestrutsdispatcherHttpHeader
Result/>
 <resulttypename=redirectclass=orgapachestrutsdispatcherServletRedir
ectResult/>
 <resulttypename=redirectActionclass=orgapachestrutsdispatcherServle
tActionRedirectResult/>
 <resulttypename=streamclass=orgapachestrutsdispatcherStreamResult/

 <resulttypename=velocityclass=orgapachestrutsdispatcherVelocityResu
lt/>
 <resulttypename=xsltclass=orgapachestrutsviewsxsltXSLTResult/>
 <resulttypename=plainTextclass=orgapachestrutsdispatcherPlainTextRe
sult/>
 <!DeprecatednameformscheduledforremovalinStrutsThecamelCaseversionsa
repreferredSeeww
 <resulttypename=redirectactionclass=orgapachestrutsdispatcherServl
etActionRedirectResult/>
 <resulttypename=plaintextclass=orgapachestrutsdispatcherPlainTextRe
sult/>
</resulttypes>

  全局result

  有很多時候一個<result>初很多<action>使用這時可以使用<globalresults>標簽來定義全局的<result>代碼如下

  

  <struts>
<packagename=demoextends=strutsdefault
<globalresults>
<resultname=print>/resultjsp</result>
</globalresults>
<actionname=submitclass=actionMoreSubmitAction
 ……
</action>
<actionname=myclass=actionMoreSubmitActionmethod=my
……
</action>
</package>
</struts>

  如果<action>中沒有相應的<result>Struts就會使用全局的<result>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/27860.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.