本站不再更新,歡迎光臨 java開(kāi)發(fā)技術(shù)網(wǎng)
          隨筆-230  評(píng)論-230  文章-8  trackbacks-0

          環(huán)境
          1 、開(kāi)發(fā)環(huán)境Eclipse +Myeclipse
          2、struts2.01(下文指struts)
          3、resin3.1(先擇的原因是支持自動(dòng)編譯,感覺(jué)比tomcat強(qiáng))

          在apache中下載struts2.0.1版本,struts的發(fā)布包中一般都帶有例子,我們先把struts2-blank-2.0.1.war這個(gè)最簡(jiǎn)單的例子解壓并把該項(xiàng)目的相關(guān)文件移值到myeclipse開(kāi)發(fā)環(huán)竟底下。操作步驟如下:
          1 、在myeclipse新建一個(gè)Web項(xiàng)目。把struts2-blank-2.0.1\WEB-INF\lib目錄下的jar文件拷貝到新建的Web項(xiàng)目的lib目錄下。
          2、把struts.properties,struts.xml考到新建項(xiàng)目的src目錄里。在myeclipse把新的項(xiàng)目發(fā)布到resin3.1的deploy目錄里(自動(dòng)發(fā)布的,但我還不懂怎么配置讓resin3.1自動(dòng)發(fā)布delpoy目錄里的項(xiàng)目)把deploy目錄里我們發(fā)布的項(xiàng)目拷貝到resin3.1的webapps目錄里。
          3、把eclipse自動(dòng)編譯選項(xiàng)取消,把新建項(xiàng)目的WebRoot刪掉,重新建立一個(gè)該名字的虛擬目錄,連接到我們拷貝到resin3.1的webpps目錄中我們發(fā)布的目錄,把eclipse中重選擇自動(dòng)編譯。

          這是開(kāi)發(fā)環(huán)竟的拾建。接下來(lái)慢慢介紹struts2的應(yīng)用。
          ========================================================
          struts.properties文件配置內(nèi)容如下:

          struts.action.extension
          ??????????The?URL?extension?to?use?to?determine?if?the?request?is?meant?for?a?Struts?action?
          ???????????用URL擴(kuò)展名來(lái)確定是否這個(gè)請(qǐng)求是被用作Struts?action,其實(shí)也就是設(shè)置?action的后綴,例如login.do的'do'字。

          struts.configuration
          ??????????The?org.apache.struts2.config.Configuration?implementation?class
          ????????????org.apache.struts2.config.Configuration接口名

          struts.configuration.files
          ??????????A?list?of?configuration?files?automatically?loaded?by?Struts?
          ???????????struts自動(dòng)加載的一個(gè)配置文件列表

          struts.configuration.xml.reload
          ??????????Whether?to?reload?the?XML?configuration?or?not
          ???????????是否加載xml配置(true,false)

          struts.continuations.package
          ???????????The?package?containing?actions?that?use?Rife?continuations
          ???????????含有actions的完整連續(xù)的package名稱(chēng)

          struts.custom.i18n.resources
          ??????????Location?of?additional?localization?properties?files?to?load?
          ???????????加載附加的國(guó)際化屬性文件(不包含.properties后綴)

          struts.custom.properties
          ??????????Location?of?additional?configuration?properties?files?to?load
          ???????????加載附加的配置文件的位置


          struts.devMode
          ??????????Whether?Struts?is?in?development?mode?or?not
          ???????????是否為struts開(kāi)發(fā)模式

          struts.dispatcher.parametersWorkaround
          ??????????Whether?to?use?a?Servlet?request?parameter?workaround?necessary?for?some?versions?of?WebLogic
          ????????????(某些版本的weblogic專(zhuān)用)是否使用一個(gè)servlet請(qǐng)求參數(shù)工作區(qū)(PARAMETERSWORKAROUND)

          struts.enable.DynamicMethodInvocation
          ??????????Allows?one?to?disable?dynamic?method?invocation?from?the?URL
          ????????????允許動(dòng)態(tài)方法調(diào)用

          struts.freemarker.manager.classname
          ??????????The?org.apache.struts2.views.freemarker.FreemarkerManager?implementation?class?
          ???????????org.apache.struts2.views.freemarker.FreemarkerManager接口名

          struts.i18n.encoding
          ??????????The?encoding?to?use?for?localization?messages
          ???????????國(guó)際化信息內(nèi)碼

          struts.i18n.reload
          ??????????Whether?the?localization?messages?should?automatically?be?reloaded
          ???????????是否國(guó)際化信息自動(dòng)加載?

          struts.locale
          ??????????The?default?locale?for?the?Struts?application
          ???????????默認(rèn)的國(guó)際化地區(qū)信息

          struts.mapper.class
          ??????????The?org.apache.struts2.dispatcher.mapper.ActionMapper?implementation?class
          ????????????org.apache.struts2.dispatcher.mapper.ActionMapper接口

          struts.multipart.maxSize
          ??????????The?maximize?size?of?a?multipart?request?(file?upload)
          ???????????multipart請(qǐng)求信息的最大尺寸(文件上傳用)?

          struts.multipart.parser
          ??????????The?org.apache.struts2.dispatcher.multipart.MultiPartRequest?parser?implementation?for?a?multipart?request?(file?upload)?
          ??????????專(zhuān)為multipart請(qǐng)求信息使用的org.apache.struts2.dispatcher.multipart.MultiPartRequest解析器接口(文件上傳用)


          struts.multipart.saveDir
          ??????????The?directory?to?use?for?storing?uploaded?files?
          ???????????設(shè)置存儲(chǔ)上傳文件的目錄夾

          struts.objectFactory
          ??????????The?com.opensymphony.xwork2.ObjectFactory?implementation?class
          ???????????com.opensymphony.xwork2.ObjectFactory接口(spring)

          struts.objectFactory.spring.autoWire
          ??????????Whether?Spring?should?autoWire?or?not
          ???????????是否自動(dòng)綁定Spring

          struts.objectFactory.spring.useClassCache
          ??????????Whether?Spring?should?use?its?class?cache?or?not
          ???????????是否spring應(yīng)該使用自身的cache?

          struts.objectTypeDeterminer
          ??????????The?com.opensymphony.xwork2.util.ObjectTypeDeterminer?implementation?class
          ????????????com.opensymphony.xwork2.util.ObjectTypeDeterminer接口

          struts.serve.static.browserCache
          ??????????If?static?content?served?by?the?Struts?filter?should?set?browser?caching?header?properties?or?not?
          ???????????是否struts過(guò)濾器中提供的靜態(tài)內(nèi)容應(yīng)該被瀏覽器緩存在頭部屬性中

          struts.serve.static
          ??????????Whether?the?Struts?filter?should?serve?static?content?or?not?
          ???????????是否struts過(guò)濾器應(yīng)該提供靜態(tài)內(nèi)容

          struts.tag.altSyntax
          ??????????Whether?to?use?the?alterative?syntax?for?the?tags?or?not?
          ???????????是否可以用替代的語(yǔ)法替代tags

          struts.ui.templateDir
          ??????????The?directory?containing?UI?templates
          ???????????UI?templates的目錄夾?

          struts.ui.theme
          ??????????The?default?UI?template?theme
          ???????????默認(rèn)的UI?template主題

          struts.url.http.port
          ??????????The?HTTP?port?used?by?Struts?URLs
          ???????????設(shè)置http端口

          struts.url.https.port
          ??????????The?HTTPS?port?used?by?Struts?URLs?
          ???????????設(shè)置https端口

          struts.url.includeParams
          ??????????The?default?includeParams?method?to?generate?Struts?URLs?
          ??????????在url中產(chǎn)生?默認(rèn)的includeParams


          struts.velocity.configfile
          ??????????The?Velocity?configuration?file?path
          ???????????velocity配置文件路徑

          struts.velocity.contexts
          ??????????List?of?Velocity?context?names
          ???????????velocity的context列表


          struts.velocity.manager.classname
          ??????????org.apache.struts2.views.velocity.VelocityManager?implementation?class
          ???????????org.apache.struts2.views.velocity.VelocityManager接口名

          struts.velocity.toolboxlocation
          ??????????The?location?of?the?Velocity?toolbox
          ???????????velocity工具盒的位置?
          struts.xslt.nocache
          ??????????Whether?or?not?XSLT?templates?should?not?be?cached
          ???????????是否XSLT模版應(yīng)該被緩存

          其它配置文件介紹
          一、Struts2配置文件?
          Struts2相關(guān)的配置文件有web.xml,struts.xml,struts.properties,?
          struts-default.xml,velocity.properties,struts-default.vm。其中web.xml,struts.xml是必須的,其它的配置文件可選擇。它們?cè)趙eb應(yīng)用中的功能如下:?
          web.xml:包含所有必須的框架組件的web部署描述符。?
          Struts.xml:配置包含result/view類(lèi)型、action映射、攔截器等的Struts2的主要配置文件。?
          Struts.properties:配置struts2的框架屬性。?
          Struts-default.xml:在文件在struts-action-x.x.jar中,該文件是應(yīng)該被包含在struts.xml中的缺省配置。?
          Welocity.properties:重寫(xiě)了velocity的配置文件。?
          Struts-default.vm:相對(duì)于velocity的缺省配置。

          二、Struts2配置元素?
          Struts2核心的配置文件是缺省的struts.xml。?
          必要的時(shí)候,缺省的配置文件可以包含其它的配置文件;struts文件可以放入jar中,并自動(dòng)插入應(yīng)用程序,這樣每個(gè)模塊可以包含自己的配置文件并自動(dòng)配置。在Freemarker和Velocity模塊中,模板也能從classpath中加載,所以整個(gè)模塊可以作為一個(gè)簡(jiǎn)單的jar文件被包含。?
          Struts.xml配置文件可以包含Interceptor、Action類(lèi)和Results。?
          Struts.xml配置元素說(shuō)明:

          1、Packages?
          Packages:packages把a(bǔ)ctions、results、results?types、interceptors?
          和interceptor-stacks組裝到一個(gè)邏輯單元中,從概念上講,packages就像一個(gè)對(duì)象,可以被其它子包從寫(xiě),而且可以擁有自己獨(dú)立的部分。?
          Name屬性是packages的必填元素,它作為一個(gè)關(guān)鍵字被后邊的包引用;extends元素是可選的,它允許包擴(kuò)展一個(gè)和多個(gè)前邊定義的包。注意,?
          struts.xml文件是至上而下處理的,所有被擴(kuò)展的包,需要在擴(kuò)展包前定義。?
          Abstract元素是可選的,它可以申明一個(gè)不包含actions的配置文件。

          2、Namespace?
          Namespace元素把a(bǔ)ctions細(xì)分到邏輯模塊,每一個(gè)namespace都有自己的?
          前綴(prefix),namespace避免了action之間的名字沖突,當(dāng)前綴出現(xiàn)在URI中時(shí),這些標(biāo)簽都是名字空間感知的(“namespace?aware”),所以這些namespace?prefix不必嵌入到表單或連接中。?
          Default的namespace是一個(gè)空字符串“”,如果在指定的配置文件中,沒(méi)有找到action,缺省的namespace也會(huì)被查找。Local/global策略允許應(yīng)用程序在action?“extends”元素層次結(jié)構(gòu)之外,有全局的action配置。缺省的namespace也可以不在package中申明。?
          Namespace?prefix可以注冊(cè)為java的申明式安全,以確保授權(quán)的用戶(hù)才能訪(fǎng)問(wèn)namespace的資源。?
          Root?namespace(“/”)也被支持,root就是當(dāng)直接請(qǐng)求context?path的時(shí)候的namespace。

          [個(gè)人理解:namespace的用法就像struts1.x中的path一樣,只不過(guò)它在package中什么路徑,而在action中申明action名子罷了]

          3、Include?
          Include元素使得框架能應(yīng)用“divide?and?conquer”來(lái)配置文件。被include?
          的每個(gè)配置文件必須和struts.xml有一樣的格式,一個(gè)大的項(xiàng)目可以采用這樣方式來(lái)組織程序模塊。?
          Include元素也可以和package交替出現(xiàn),框架將按照順序加載配置文件。

          4、Interceptor?configuration?
          Interceptor允許應(yīng)用程序在Action方法執(zhí)行前后定義執(zhí)行代碼,?
          Interceptor在應(yīng)用程序開(kāi)發(fā)中十分重要,對(duì)于Interceptor有許多用例,如validation,?property?population,?security,?logging,?和profiling。?
          Interceptor被定義為一個(gè)Java類(lèi),Interceptor也可以組裝成Interceptor-stack,他們將按照定義的順序執(zhí)行。?
          在struts-default.xml中定義了一些缺省的Interceptor-stack,以便框架能很好地運(yùn)行。

          5、Action?
          Action是框架的“工作單元”。Action可以指定一個(gè)Interceptor-stack、?
          一序列的return?type和一序列的異常處理,但只有name屬性是必須的。?
          one.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="one"??extends="struts-default">
          ????????
          <default-interceptor-ref?name="defaultStack"?/>?
          ????????
          <action?name="hello"??class="one.HelloWorld">
          ????????????
          <result?name="success"?>/hello.jsp</result>
          ????????
          </action>
          ????
          </package>
          </struts>
          struts.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="one.xml"/>
          ????
          <include?file="webwork-default.xml"?/>
          ????
          <package?name="default"?extends="webwork-default">
          ????
          </package>
          </struts>

          struts.properties

          struts.devMode?=?true
          struts.enable.DynamicMethodInvocation?
          =?false

          HelloWorld.java
          package?one;
          import?com.opensymphony.xwork2.ActionSupport;

          public?class?HelloWorld?extends?ActionSupport{
          ????
          private?String?message;

          ????
          public?void?setMessage(String?message)?{
          ????????
          this.message?=?message;
          ????}
          ????
          ????
          public?String?getMessage()?{
          ????????
          return?message;
          ????}

          ????
          public?String?execute(){
          ????????System.out.println(
          "execute?測(cè)試..");
          ????????message
          ="Hello?peidw";
          ????????
          ????????
          return?ActionSupport.SUCCESS;
          ????}
          }
          就這么簡(jiǎn)單,還有一個(gè)就hello.jsp頁(yè)面了,該頁(yè)面的作用是顯示了上類(lèi)的message屬性值。
          hello.jsp文件內(nèi)容如下:
          <%@?page?language="java"?contentType="text/html;?charset=gb2312"
          ????pageEncoding
          ="gb2312"%>
          <%@?taglib?prefix?=?"s"?uri?=?"/struts-tags"?%>?
          <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <meta?http-equiv="Content-Type"?content="text/html;?charset=gb2312">
          <title>sruts2?</title>
          </head>
          <body>
          ????hello?
          <s:property?value="message"/>
          </body>
          </html>
          posted on 2007-03-21 11:31 有貓相伴的日子 閱讀(2715) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): j2ee

          評(píng)論:
          # re: struts2入門(mén)--1 2007-04-03 16:31 | 壞男孩
          學(xué)習(xí)Struts2的好地方啊  回復(fù)  更多評(píng)論
            
          # re: struts2入門(mén)--1[未登錄](méi) 2008-05-17 19:13 | sara
          thank you!!!  回復(fù)  更多評(píng)論
            
          本站不再更新,歡迎光臨 java開(kāi)發(fā)技術(shù)網(wǎng)
          主站蜘蛛池模板: 府谷县| 探索| 崇礼县| 丰都县| 蓬安县| 大安市| 体育| 黔江区| 张家界市| 托克逊县| 苏州市| 岚皋县| 茌平县| 三穗县| 绥棱县| 巩义市| 大关县| 同仁县| 孝义市| 拉萨市| 怀来县| 会泽县| 若尔盖县| 灵川县| 商河县| 芷江| 内乡县| 西乌珠穆沁旗| 郴州市| 洮南市| 射洪县| 青冈县| 洛隆县| 忻州市| 子洲县| 伊吾县| 曲阜市| 九龙坡区| 化隆| 长阳| 台湾省|