成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

開發(fā) 后端
本文為Struts2教程,本部分講解struts.xml常用配置解析。Struts2雖然在大版本號(hào)上是第二個(gè)版本,但基本上在配置和使用上已經(jīng)完全顛覆了Struts1.x的方式。

在本文中將詳細(xì)講述struts.xml文件的常用配置及注意事項(xiàng)。

1. 使用< include>標(biāo)簽重用配置文件

在Struts2中提供了一個(gè)默認(rèn)的struts.xml文件,但如果package、action、interceptors等配置比較多時(shí),都放到一個(gè)struts.xml文件不太容易維護(hù)。因此,就需要將struts.xml文件分成多個(gè)配置文件,然后在struts.xml文件中使用< include>標(biāo)簽引用這些配置文件。這樣做的優(yōu)點(diǎn)如下:

結(jié)構(gòu)更清晰,更容易維護(hù)配置信息。

配置文件可以復(fù)用。如果在多個(gè)Web程序中都使用類似或相同的配置文件,那么可以使用< include>標(biāo)簽來(lái)引用這些配置文件,這樣可以減少工作量。

假設(shè)有一個(gè)配置文件,文件名為newstruts.xml,代碼如下:

  1. < ?xml version="1.0" encoding="UTF-8" ?> 
  2. < !DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5. < struts> 
  6.     < package name="demo" extends="struts-default" > 
  7.         < action name="submit"  class="action.MoreSubmitAction"> 
  8.             < result name="save" > 
  9.                 /result.jsp  
  10.             < /result> 
  11.             < result name="print"> 
  12.                 /result.jsp  
  13.             < /result> 
  14.         < /action>              
  15.     < /package>      
  16. < /struts> 

 則struts.xml引用newstruts.xml文件的代碼如下:

  1. < ?xml version="1.0" encoding="UTF-8" ?> 
  2. < !DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5. < struts> 
  6.     < include file="newstruts.xml"/> 
  7.     < package name="test" extends="struts-default"> 
  8.        
  9.     < /package>      
  10. < /struts> 

大家要注意一下,用< include>引用的xml文件也必須是完成的struts2的配置。實(shí)際上< include>在引用時(shí)是單獨(dú)解析的xml文件,而不是將被引用的文件插入到struts.xml文件中。

2. action的別名

在默認(rèn)情況下,Struts2會(huì)調(diào)用動(dòng)作類的execute方法。但有些時(shí)候,我們需要在一個(gè)動(dòng)作類中處理不同的動(dòng)作。也就是用戶請(qǐng)求不同的動(dòng)作時(shí),執(zhí)行動(dòng)作類中的不同的方法。為了達(dá)到這個(gè)目的,可以在< action>標(biāo)簽中通過method方法指定要指行的動(dòng)作類的方法名,并且需要為不同的動(dòng)作起不同的名子(也稱為別名)。如下面代碼所示:

  1. < ?xml version="1.0" encoding="UTF-8" ?> 
  2. < !DOCTYPE struts PUBLIC  
  3.    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5. < struts> 
  6. < package name="demo" extends="struts-default" > 
  7.     < action name="test"  class="action.MyAction"> 
  8.           
  9.     < /action>              
  10.     < action name="my"  class="action. MyAction" method="my"> 
  11.            
  12.     < /action>              
  13. < /package>      
  14. < /struts> 

上面代碼的兩個(gè)動(dòng)作的class屬性都指向同一個(gè)類,name為這個(gè)類起了兩個(gè)動(dòng)作別名:test和my。在動(dòng)作my中,使用了method屬性指定要要運(yùn)行的方法名為my。

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

  1. package action;  
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.  
  5. public class MyAction extends ActionSupport  
  6. {  
  7.        
  8.     public String execute() throws Exception  
  9.     {  
  10.         // 處理test動(dòng)作的代碼  
  11.     }  
  12.     public String my() throws Exception  
  13.     {  
  14.           // 處理my動(dòng)作的代碼  
  15.     }  
  16.        
  17. }  

除了在struts.xml中配置別名,還可以通過請(qǐng)求參數(shù)來(lái)描述指定動(dòng)作(并不需要在struts.xml中配置)。請(qǐng)求參數(shù)的格式如下:

http://localhost:8080/contextPath/actionName!method.action

關(guān)于通過請(qǐng)求指定動(dòng)作的詳細(xì)內(nèi)容,請(qǐng)參閱筆者寫的《Struts2教程2:處理一個(gè)form多個(gè)submit》。

3. 為action指定參數(shù)

在struts2中還可以為action指定一個(gè)或多個(gè)參數(shù)。大家還記著struts1.x是如何設(shè)置的action參數(shù)不? 在struts1.x中可以使用< action>標(biāo)簽的parameter屬性為其指定一個(gè)action參數(shù),如果要指定多個(gè),就只能通過逗號(hào)(,)或其他的分隔符將不同的參數(shù)隔開。而在struts2中可以通過< param>標(biāo)簽指定任意多個(gè)參數(shù)。代碼如下:

  1. < action name="submit"  class="action.MyAction"> 
  2. < param name="param1">value1< /param> 
  3. < param name="param2">value2< /param> 
  4.     < result name="save" > 
  5.         /result.jsp  
  6.     < /result> 
  7.        
  8. < /action> 

當(dāng)然,在action中讀這些參數(shù)也非常簡(jiǎn)單,只需要象獲取請(qǐng)求參數(shù)一樣在action類中定義相應(yīng)的setter方法即可(一般不用定義getter方法)。如下面的代碼將讀取param1和param2參數(shù)的值:

  1. package action;  
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.  
  5. public class MyAction extends ActionSupport  
  6. {  
  7.     private String param1;  
  8.     private String param2;  
  9.  
  10.     public String execute() throws Exception  
  11.     {  
  12.         System.out.println(param1 + param2);  
  13.     }  
  14.     public void setParam1(String param1)  
  15.     {  
  16.         this.param1 = param1;  
  17.     }  
  18.     public void setParam2(String param2)  
  19.     {  
  20.         this.param2 = param2;  
  21.     }  
  22.        
  23. }  

當(dāng)struts2在調(diào)用execute之前,param1和param2的值就已經(jīng)是相應(yīng)參數(shù)的值了,因此,在execute方法中可以直接使用param1和param2。

4. 選擇result類型

在默認(rèn)時(shí),< result>標(biāo)簽的type屬性值是“dispatcher”(實(shí)際上就是轉(zhuǎn)發(fā),forward)。開發(fā)人員可以根據(jù)自己的需要指定不同的類型,如redirect、stream等。如下面代碼所示:

< result name="save" type="redirect">

       /result.jsp

< /result>

這此result-type可以在struts2-core-2.0.11.1.jar包或struts2源代碼中的struts-default.xml文件中找到,在這個(gè)文件中找到< result-types>標(biāo)簽,所有的result-type都在里面定義了。代碼如下:

  1. < result-types> 
  2.        < result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> 
  3.        < result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> 
  4.        < result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> 
  5.        < result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> 
  6.        < result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> 
  7.        < result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> 
  8.        < result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> 
  9.        < result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> 
  10.        < result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> 
  11.        < result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> 
  12.        < !-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 --> 
  13.        < result-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> 
  14.        < result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult" /> 
  15. < /result-types> 

5. 全局result

有很多時(shí)候一個(gè)< result>初很多< action>使用,這時(shí)可以使用< global-results>標(biāo)簽來(lái)定義全局的< result>,代碼如下:

  1. < struts> 
  2.     < package name="demo" extends="struts-default"> 
  3.         < global-results> 
  4.             < result name="print">/result.jsp< /result> 
  5.         < /global-results> 
  6.         < action name="submit" class="action.MoreSubmitAction"> 
  7.            
  8.         < /action> 
  9.         < action name="my" class="action.MoreSubmitAction" method="my"> 
  10.            
  11.         < /action> 
  12.     < /package> 
  13. < /struts> 

如果< action>中沒有相應(yīng)的< result>,Struts2就會(huì)使用全局的< result>。

【編輯推薦】

  1. Struts2教程:處理一個(gè)form多個(gè)submit
  2. Struts2教程:第一個(gè)Struts2程序
  3. Struts2中POI在內(nèi)存中生成文件并下載
  4. Struts2深入詳解properties配置文件
  5. Struts2 iterator介紹及功能詳解
責(zé)任編輯:yangsai 來(lái)源: BlogJava
相關(guān)推薦

2009-02-04 11:37:15

2009-06-04 08:34:24

Struts2配置struts.xml

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-02-04 10:51:07

2009-06-05 10:55:07

struts2 web

2009-07-29 09:54:34

struts2和str

2009-06-25 16:04:30

2009-06-03 14:19:34

Struts2Guice

2009-02-04 15:04:13

2009-06-25 15:50:03

Struts2教程上傳任意多個(gè)文件

2009-06-25 15:54:42

Struts2教程攔截器

2009-06-25 15:33:12

Struts2教程使用validate驗(yàn)證數(shù)據(jù)

2009-02-04 14:19:38

2009-06-25 15:37:12

Struts2教程Validation框

2009-02-04 14:00:59

2009-07-03 09:35:57

Struts2 JSP

2009-06-05 10:52:45

struts2深入詳解配置文件

2009-06-05 10:05:50

struts menustruts2

2009-02-04 12:00:08

2009-06-25 15:59:21

Struts2教程攔截器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 亚洲成人精品一区 | 久一久 | 精品国产乱码久久久久久图片 | 怡红院成人在线视频 | 成人av电影在线 | 97精品超碰一区二区三区 | 一级a性色生活片久久毛片波多野 | 亚洲午夜精品视频 | 亚洲区中文字幕 | 国产精品美女久久久久 | 日本一区二区三区四区 | av手机在线免费观看 | 特级做a爱片免费69 精品国产鲁一鲁一区二区张丽 | 色爱综合网 | 精品视频一区二区三区在线观看 | 国产精品视频不卡 | 国产一区二区三区在线 | 亚洲综合二区 | 亚洲69p| 欧美九九九 | 成人av高清在线观看 | 97伦理影院 | 免费一区 | 日韩高清电影 | 亚洲最大的黄色网址 | 国产视频精品在线观看 | 久久国产一区二区三区 | 国产精品福利网站 | 久久av一区二区三区 | 男女啪啪高潮无遮挡免费动态 | 天天操天天射综合网 | 国产区视频在线观看 | 久久在线| 久久久免费电影 | 成人性视频免费网站 | 91精品国产一区二区在线观看 | 成人精品国产一区二区4080 | 欧美一级观看 | 亚洲久草| 国产精品久久久久久久久久 | 欧美一区二区三区在线观看 |