隨筆 - 312, 文章 - 14, 評(píng)論 - 1393, 引用 - 0
          數(shù)據(jù)加載中……

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

          本文為原創(chuàng),如需轉(zhuǎn)載,請(qǐng)注明作者和出處,謝謝!

          上一篇:Struts2教程2:處理一個(gè)form多個(gè)submit

          在本文中將詳細(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)簽來引用這些配置文件,這樣可以減少工作量。

          假設(shè)有一個(gè)配置文件,文件名為newstruts.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="demo" extends="struts-default" >
                  
          <action name="submit"  class="action.MoreSubmitAction">
                      
          <result name="save" >
                          /result.jsp
                      
          </result>
                      
          <result name="print">
                          /result.jsp
                      
          </result>
                  
          </action>            
              
          </package>    
          </struts>

           struts.xml引用newstruts.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>
              
          <include file="newstruts.xml"/>
              
          <package name="test" extends="struts-default">
               
              
          </package>    
          </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)作起不同的名子(也稱為別名)。如下面代碼所示:

          <?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="demo" extends="struts-default" >
              
          <action name="test"  class="action.MyAction">
                  
              
          </action>            
              
          <action name="my"  class="action. MyAction" method="my">
                   
              
          </action>            
          </package>    
          </struts>

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

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

          package action;

          import com.opensymphony.xwork2.ActionSupport;

          public class MyAction extends ActionSupport
          {
               
              
          public String execute() throws Exception
              {
                  
          // 處理test動(dòng)作的代碼
              }
              
          public String my() throws Exception
              {
                    
          // 處理my動(dòng)作的代碼
              }
               
          }


          除了在struts.xml中配置別名,還可以通過請(qǐng)求參數(shù)來描述指定動(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ù)。代碼如下:

          <action name="submit"  class="action.MyAction">
          <param name="param1">value1</param>
          <param name="param2">value2</param>
              
          <result name="save" >
                  /result.jsp
              
          </result>
               
          </action>       

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

          import com.opensymphony.xwork2.ActionSupport;

          public class MyAction extends ActionSupport
          {
              
          private String param1;
              
          private String param2;

              
          public String execute() throws Exception
              {
                  System.out.println(param1 
          + param2);
              }
              
          public void setParam1(String param1)
              {
                  
          this.param1 = param1;
              }
              
          public void setParam2(String param2)
              {
                  
          this.param2 = param2;
              }
               
          }

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

          4.        選擇result類型

           

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

          <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都在里面定義了。代碼如下:

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


          5.        全局result

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


          <struts>
              
          <package name="demo" extends="struts-default">
                  
          <global-results>
                      
          <result name="print">/result.jsp</result>
                  
          </global-results>
                  
          <action name="submit" class="action.MoreSubmitAction">
                   
                  
          </action>
                  
          <action name="my" class="action.MoreSubmitAction" method="my">
                   
                  
          </action>
              
          </package>
          </struts>

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


          下一篇:Struts2教程4:使用validate方法驗(yàn)證數(shù)據(jù)






          Android開發(fā)完全講義(第2版)(本書版權(quán)已輸出到臺(tái)灣)

          http://product.dangdang.com/product.aspx?product_id=22741502



          Android高薪之路:Android程序員面試寶典 http://book.360buy.com/10970314.html


          新浪微博:http://t.sina.com.cn/androidguy   昵稱:李寧_Lining

          posted on 2008-04-16 15:25 銀河使者 閱讀(47110) 評(píng)論(17)  編輯  收藏 所屬分類: Struts2 原創(chuàng)

          評(píng)論

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          樓主可以出書了
          2008-08-13 16:15 | 匿名

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          出書吧 樓主 我要買
          2008-08-20 16:32 | fu

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          謝謝博主的博客...

          初學(xué)struts2收益了...
          2008-10-08 13:49 | changwei

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          真的好啊
          學(xué)習(xí)了
          2008-10-22 16:50 | lyshyhaungli

          # re: Struts2教程3:struts.xml常用配置解析[未登錄]  回復(fù)  更多評(píng)論   

          "如果<action>中沒有相應(yīng)的<result>,Struts2就會(huì)使用全局的<result>。",global是否可以多個(gè)result?若有多個(gè)result,在這種情況下會(huì)使用個(gè)result?
          2009-03-12 23:14 | yxy

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          @yxy
          全局當(dāng)然可以有多個(gè)result,每個(gè)result都有有name哦,當(dāng)然是使用與name相符的result了。如果沒有,則拋出異常。
          2009-03-13 08:37 | 銀河使者

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          <param name="param1">value1</param>
          <param name="param2">value2</param>
          請(qǐng)教樓主,如果我在提交時(shí)要帶多個(gè)參數(shù),按照樓主這樣賦值是要在xml文件里寫死的;那我如果參數(shù)值是變化的,比如param1有可能是1,也有可能是2,我該如何提交?
          2009-05-21 09:59 | 海洋女神

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          struts 2的Action屬性可以讀取請(qǐng)求參數(shù)值,如果是變化的,可以通過請(qǐng)求參數(shù)值提交。
          2009-05-21 10:05 | 銀河使者

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          樓主,幫忙調(diào)一BUG 你看看這個(gè)問題出在哪》?謝謝
          Could not find action or result
          There is no Action mapped for namespace / and action name login. - [unknown location]
          2009-06-13 10:16 |

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          @亮
          你的action路徑寫錯(cuò)了,,在/ namespace里沒有這個(gè)action
          2009-06-13 11:45 | 銀河使者

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          @銀河使者
          真的很感謝你能幫幫我,我是一個(gè)出學(xué)者!這個(gè)bug已經(jīng)折磨我三天了。 你說我的路徑錯(cuò)了!我檢查過了! 沒錯(cuò)啊! 這是我的配置文件! 你看看吧,還有namespace 到底是一個(gè)什么東西。
          <struts>

          <package name="struts2" extends="struts-default">
          <action name="login" class="com.cn.LoginAction">
          <result name="error">/error.jsp</result>
          <result name="success">/success.jsp</result>
          </action>
          </package>

          <!-- Add packages here -->

          </struts>

          我的action 是寫在com.cn包下的啊。
          2009-06-13 13:28 |

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          是啊! 是寫在com.cn 下啊!就寫了一個(gè)LoginAction
          2009-06-13 13:43 |

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          你在<package name="struts2" extends="struts-default"> 中加一個(gè)namespace試試

          <package name="struts2" namespace="/" extends="struts-default">
          2009-06-13 17:02 | 銀河使者

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          努力向您學(xué)習(xí)
          2012-06-21 21:16 | 何冬

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          很不錯(cuò),除了有個(gè)別錯(cuò)別字,謝了~
          2012-09-10 21:43 | shukouen

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          非常有用呢!謝謝!
          2014-03-01 20:38 | Dolan.Deng

          # re: Struts2教程3:struts.xml常用配置解析  回復(fù)  更多評(píng)論   

          不錯(cuò),學(xué)習(xí)了
          2014-04-02 00:10 | shunshine
          主站蜘蛛池模板: 曲阳县| 翼城县| 红原县| 水城县| 广宁县| 黑山县| 漠河县| 陇西县| 鄂托克旗| 泾川县| 阳曲县| 墨江| 响水县| 古田县| 郯城县| 中江县| 始兴县| 炎陵县| 武宣县| 津市市| 丹寨县| 临泉县| 壶关县| 武定县| 连州市| 锦州市| 延安市| 榆林市| 平阴县| 延庆县| 株洲县| 精河县| 临高县| 赣州市| 同心县| 东至县| 金溪县| 资兴市| 泸溪县| 家居| 元氏县|