日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Apache Digester示例–轻松配置

發布時間:2023/12/3 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Apache Digester示例–轻松配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
解決問題–硬編碼,需要為您的應用程序創建自定義配置,例如struts配置文件,以僅通過更改文件來改變應用程序行為。 Apache Digester可以輕松為您完成此任務。

使用Apache Digester相當容易將XML文檔轉換為相應的Java bean對象層次結構。 請參閱下面的“摘要”。
Digester引入了三個重要概念:

  • 元素匹配模式
  • 處理規則
  • 對象堆棧。
  • 元素匹配模式將 XML元素與處理規則相關聯。

    范例:
    你有什么:

  • Java類
  • 包含數據的XML文件
  • 您有Java類和相應的xml文件。 您希望從xml數據創建Java類實例。

    您需要編寫哪些額外的代碼。
    分步任務:

  • 在類路徑中添加Apache Digester 3 jar文件,Commons日志jar,Beanutils jar,cglib jar
  • 如果沒有Java類,則為相應的xml文件創建Java類。或者,如果沒有xml文件,則根據java類創建。注-屬性名稱,xml和java類中的層次結構應匹配,否則,需要在此處未提及的摘要XML規則中提供映射。
  • 如以下示例中所述,創建摘要規則XML文件
  • 用幾行來從XML加載Java對象
  • 現在在行動–

    這是我的Eclipse項目結構:

    任務2 –如下創建數據xml文件,您要從中加載數據, 例如– chain-config.xml

    <?xml version="1.0" encoding="UTF-8"?><catalogs><!-- Default Catalog: "Path Info" example --><catalog><!-- Command that maps "Path Info" patterns to Commands --><chain name="COMMAND_MAPPER"><command className="org.apache.commons.chain.web.servlet.PathInfoMapper"/><command forward="/pathinfo.jsp" className="org.apache.commons.chain.apps.example.ForwardCommand"/></chain><!-- Foo Command --><chain name="/foo"><command attribute="pathinfoFooCount" className="org.apache.commons.chain.apps.example.CountCommand"/></chain><!-- Bar Command --><chain name="/bar"><command attribute="pathinfoBarCount" className="org.apache.commons.chain.apps.example.CountCommand"/></chain></catalog><!-- Catalog for "Request Parameter" example --><catalog name="reqparam"><!-- Command that maps a "Request Parameter" to Commands --><chain name="COMMAND_MAPPER"><command catalogName="reqparam" className="org.apache.commons.chain.web.servlet.RequestParameterMapper"/><command forward="/reqparam.jsp" className="org.apache.commons.chain.apps.example.ForwardCommand"/></chain><!-- Foo Command --><chain name="foo"><command attribute="reqparamFooCount" className="org.apache.commons.chain.apps.example.CountCommand"/></chain><!-- Bar Command --><chain name="bar"><command attribute="reqparamBarCount" className="org.apache.commons.chain.apps.example.CountCommand"/></chain></catalog></catalogs>


    創建相應的Java類 Catalog.java:

    import java.util.ArrayList; import java.util.List;public class Catalog {/*** @uml.property name="name"*/private String name;/*** Getter of the property <tt>name</tt>* @return Returns the name.* @uml.property name="name"*/public String getName() {return name;}/*** Setter of the property <tt>name</tt>* @param name The name to set.* @uml.property name="name"*/public void setName(String name) {this.name = name;}/*** @uml.property name="chains"*/private List<Chain> chains=new ArrayList<Chain>();public void addChains(Chain chain){this.chains.add(chain);}}


    Chain.java:

    import java.util.ArrayList; import java.util.List;public class Chain {/*** @uml.property name="name"*/private String name;/*** Getter of the property <tt>name</tt>* @return Returns the name.* @uml.property name="name"*/public String getName() {return name;}/*** Setter of the property <tt>name</tt>* @param name The name to set.* @uml.property name="name"*/public void setName(String name) {this.name = name;}/*** @uml.property name="commands"*/private List<Command> commands=new ArrayList<Command>();/*** Setter of the property <tt>commands</tt>* @param commands The commands to set.* @uml.property name="commands"*/public void addCommands(Command command) {this.commands.add(command);}}


    Command.java:

    import java.util.ArrayList; import java.util.List;public class Chain {/*** @uml.property name="name"*/private String name;/*** Getter of the property <tt>name</tt>* @return Returns the name.* @uml.property name="name"*/public String getName() {return name;}/*** Setter of the property <tt>name</tt>* @param name The name to set.* @uml.property name="name"*/public void setName(String name) {this.name = name;}/*** @uml.property name="commands"*/private List<Command> commands=new ArrayList<Command>();/*** Getter of the property <tt>commands</tt>* @return Returns the commands.* @uml.property name="commands"*/public List getCommands() {return commands;}/*** Setter of the property <tt>commands</tt>* @param commands The commands to set.* @uml.property name="commands"*/public void addCommands(Command command) {this.commands.add(command);}}

    任務3 –創建 摘要程序 規則 digester-catalog-rules.xml

    <?xml version="1.0"?> <!DOCTYPE digester-rules PUBLIC"-//Apache Commons //DTD digester-rules XML V1.0//EN""http://commons.apache.org/digester/dtds/digester-rules-3.0.dtd"> <digester-rules><pattern value="catalogs/catalog"><object-create-rule classname="Catalog"/><set-properties-rule/><!-- comment :<bean-property-setter-rule pattern="name"/> use as shown above if say <catalog><name>reparam</name> </catalog> instead of <catalog name="reparam"> </catalog>--><!-- Nested Pattern for Characters --><pattern value="chain"><object-create-rule classname="Chain"/><set-properties-rule/> <!-- Nested Pattern for Characters --><pattern value="command"><object-create-rule classname="Command"/><set-properties-rule/><set-next-rule methodname="addCommands" paramtype="Command"/> </pattern><set-next-rule methodname="addChains" paramtype="Chain"/> </pattern><set-next-rule methodname="add" paramtype="Catalog"/> </pattern> </digester-rules>

    任務4 –加載xml數據的客戶端程序

    import java.io.IOException; import java.io.InputStream; import java.net.URL;import org.apache.commons.digester3.Digester; import org.apache.commons.digester3.binder.DigesterLoader; import org.apache.commons.digester3.xmlrules.FromXmlRulesModule; import org.xml.sax.SAXException;import java.util.ArrayList; import java.util.List;public class runProgram {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stub// Create an instance of the Digester from the XML rule setDigesterLoader digesterLoader = DigesterLoader.newLoader(new FromXmlRulesModule() {@Overrideprotected void loadRules() {// TODO Auto-generated method stubloadXMLRules( getClass( ).getResource("/com/tatu/resources/digester-catalog-rules.xml"));}}); Digester digester = digesterLoader.newDigester();List<Catalog> catalogs = new ArrayList<Catalog>();// Push a reference to the plays List on to the Stackdigester.push(catalogs);// Parse the XML documentInputStream input = Digester.class.getClass().getResourceAsStream("/com/tatu/resources/chain-config.xml");try {Object root = digester.parse(input);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SAXException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

    做完了 因此,已經用xml數據加載了catalogs對象。
    上述解決方案要注意的幾點:

  • object-create-rule創建一個對象
  • 其余規則如其名稱所暗示的那樣簡單明了:object-create-rule創建一個新實例,set-properties-rule從xml屬性(如目錄元素的name屬性)設置對象的屬性,而bean-property-setter-rule設置嵌套xml元素中對象的屬性,例如<catalog> <name> reparam </ name> </ catalog>而不是<catalog name ='reparam'> </ catalog>
  • set-next-rule :(用于遞歸)set-next-rulerule移至下一個目錄,鏈和命令標簽。 您還指定了每種情況下要調用的方法,該方法會將對象添加到父類中定義的集合中,例如:<set-next-rule methodname ='addCommands'paramtype ='Command'/>,此處為addCommands()方法將命令對象添加到父鏈類中定義的命令收集對象。
  • 您需要新的自定義規則,請創建自摘要器Rule類派生的自己的Rule類。
  • 有任何問題,請發表您的評論。

    再想一想,您不希望在xml文件和java類之間出現所有這些問題。 猜猜怎么著,有個避免的辦法。 但是除非您著急,否則我不喜歡這個把戲。 但是,每次使用快捷方式時,都必須失去靈活性。

    技巧是使用Apache Betwixt。 記住要使用Betwixt,您需要使用Apache Digester 2.1。 有關更多信息,請訪問apache Betwixt網站。

    使用Betwixt BeanWriter將Java Bean寫入文件,然后使用BeanReader從該文件讀取。 從BeanWriter生成文件后,就可以更改值,并在BeanReader中加載該文件。 (需要在此處省略配置之間的映射)

    祝您編程愉快,別忘了分享!

    參考:在我的軟件開發博客博客上,來自我們的JCG合作伙伴 Bijay Deo的示例中使用Apache Digester-輕松進行配置 。


    翻譯自: https://www.javacodegeeks.com/2012/09/apache-digester-example-make-easy.html

    總結

    以上是生活随笔為你收集整理的Apache Digester示例–轻松配置的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。