struts2 配置

計應134(實驗班)   韓凱麗

一.Struts2配置html

  Struts2框架中涉及兩個重要的文件:java

   1).struts.xml文件web

   2).web.xml文件ajax

1.Struts2的主要組件:spring

組  apache

做    瀏覽器

 Filterispatcher緩存

起中央控制器做用的過濾器服務器

Actionapp

處於Model層的Action,調用JavaBean實現業務邏輯

Struts.xml

核心配置文件,配置有Action、Result等

2.Struts2框架配置文件:

文件名

文件路徑

做     

是否必須

Web.xml

/WEB-INF/

描述Web部署,包括全部必須的框架組件,由開發人員編寫

Struts.xml

/WEB-INF/classe(通常直接在src中定義)

核心配置文件,包括Result映射、Action映射、攔截器配置等,由開發人員編寫

Struts-default.xml

/WEB-INF/lib/struts2-core.jar

Struts2提供的默認配置,有框架提供

Struts-pugin.xml

/WEB-INF/lib/struts2-xxx-plugin.jar

Struts2框架的插件所用的配置,由插件提供

 

3.web.xml配置:

核心代碼以下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- 配置struts2框架的核心Filter -->

    <filter>

        <!-- 配置struts2核心的Filter的名字 -->

        <filter-name>struts2</filter-name>

        <!-- 配置Struts2核心的Filter的實現類 -->

        <filter-class>

    org.apache.struts2.dispatcher.FilterDispatcher

        </filter-class>

    </filter>

    <!-- 配置Filter的映射 -->

   <filter-mapping>

       <!-- 指定Filter的名稱 -->

      <filter-name>struts2</filter-name>

       <!-- 指定對應Filter的訪問路徑 -->

       <url-pattern>/ *</url-pattern>

   </filter-mapping>

   </web-app>

2.struts.xm配置文件:

在struts2中一些配置(好比常量)能夠同時在struts-default.xml(只讀性),strtus-plguin.xml(只讀性),struts.xml,struts.properties和web.xml文件中配置,它們的優先級逐步升高,便是說後面的配置會覆蓋掉前面相同的配置。

2.配置形式

下面以對struts.i18n.encoding=UTF-8的配置爲例進行說明:

在struts.xml配置形式以下:

[html] view plaincopy

  1. <constant name="struts.i18n.encoding" value="gbk"></constant>  

在struts.properties的配置形式以下:

[html] view plaincopy

  1. struts.i18n.encoding=UTF-8  

在web.xml中配置以下:

[html] view plaincopy

  1. <filter>  
  2.   
  3. <filter-name>struts2</filter-name>  
  4.   
  5. <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  6.   
  7. </filter-class>  
  8.   
  9. <init-param>  
  10. 10.   

11. <param-name>struts.i18n.encoding</param-name>  

  1. 12.   

13. <param-value>UTF-8</param-value>  

  1. 14.   

15. </init-param>  

  1. 16.   

17. </filter>  

說明:官方聲稱配置了此常量能夠解決中文亂碼問題,但實事上並不能達到目的,在前面的三個項目中,若是咱們在表單中輸入中文,其結果是會出現亂碼。解決此問題參看[一.7的注意]。這是struts2.1.6中的一bug,它的下一版2.1.8已解決此問題。

3.package配置相關

屬性名

是否必須

  

Name

Package的惟一標識,不容許同名

Extends 

指定要繼承的包

Namespace

指定名稱空間

Abstract

聲明包爲抽象否

 

一常量配置

1. 在struts2中配置常量的方式有三種:

       X 在struts.xml文件中配置 
       X 在web.xml文件中配置  
       X 在sturts.propreties文件中配置

     注意: 1.之因此使用struts.propreties文件配置,是由於爲了保持與WebWork的向後兼容

            2.在實際開發中,在web.xml中配置常量相比其餘兩種,須要更多的代碼量,會下降了web.xml的可讀性

            3.一般推薦在struts.xml文件中配置struts2的常量,並且便於集中管理  

2. 在sturt2中搜索加載常量的順序是:

     struts-default.xml      (在struts2-core-2.0.6.jar文件中)
     struts-plugin.xml      (在struts2-Xxx-2.0.6.jar等Struts2插件JAR文件中)
     struts.xml             (Web應用默認的Struts2的配置文件)
     sturts.propreties       (Web應用默認的Struts2的配置文件)
     web.xml                 (Web應用下的配置文件)

    注意:1.若在不一樣的配置文件中同時配置了相同的Struts2常量,則後一個配置文件的常量值覆蓋前一個配置的常量值

3. 配置文件的屬性:name和value

         name:指定屬性的常量名
   value:指定屬性的值

   注意:1.在不一樣的配置文件中配置常量,雖然方式各異,可是都必須指定name、value這兩個屬性

Xml代碼  
  1. <!-指定struts2的SilterDispatcher的Filter-!>  
  2. <filter>  
  3.   <!-指定Struts2的核心Filter-!>  
  4.     
  5.     <filter-name>strurs2</filter-name>  
  6.     <filter-class>org.apache.struts2.dispatcher.FilterDispaycher  
  7.     </filter-class>  
  8.   
  9.     <!-經過init-param元素配置struts2常量-!>  
  10.   
  11.     <init-param>  
  12.         <param-name>struts.custom.il8n.resources</param-name>  
  13.         <param-va>

   常量詳解

Xml代碼  " quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> 
  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.   
  6. <struts>  
  7.     <!-- 指定Web應用的默認編碼集,至關於調用HttpServletRequest的setCharacterEncoding方法 -->  
  8.     <constant name="struts.i18n.encoding" value="UTF-8" />  
  9.   
  10.     <!--  
  11.         該屬性指定須要Struts 2處理的請求後綴,該屬性的默認值是action,即全部匹配*.action的請求都由Struts2處理。  
  12.         若是用戶須要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開。  
  13.     -->  
  14.     <constant name="struts.action.extension" value="do" />  
  15.   
  16.     <!-- 設置瀏覽器是否緩存靜態內容,默認值爲true(生產環境下使用),開發階段最好關閉 -->  
  17.     <constant name="struts.serve.static.browserCache" value="false" />  
  18.   
  19.     <!-- 當struts的配置文件修改後,系統是否自動從新加載該文件,默認值爲false(生產環境下使用),開發階段最好打開 -->  
  20.     <constant name="struts.configuration.xml.reload" value="true" />  
  21.   
  22.     <!-- 開發模式下使用,這樣能夠打印出更詳細的錯誤信息 -->  
  23.     <constant name="struts.devMode" value="true" />  
  24.   
  25.     <!-- 默認的視圖主題 -->  
  26.     <constant name="struts.ui.theme" value="simple" />  
  27.   
  28.     <!-- spring 託管 -->  
  29.     <constant name="struts.objectFactory" value="spring" />  
  30.   
  31.     <!--  
  32.         指定加載struts2配置文件管理器,默認爲org.apache.struts2.config.DefaultConfiguration  
  33.         開發者能夠自定義配置文件管理器,該類要實現Configuration接口,能夠自動加載struts2配置文件。  
  34.     -->  
  35.     <constant name="struts.configuration"  
  36.         value="org.apache.struts2.config.DefaultConfiguration" />  
  37.   
  38.     <!-- 設置默認的locale和字符編碼 -->  
  39.     <constant name="struts.locale" value="zh_CN" />  
  40.     <constant name="struts.i18n.encoding" value="GBK" />  
  41.   
  42.     <!-- 指定Struts的工廠類 -->  
  43.     <constant name="struts.objectFactory" value="spring"></constant>  
  44.   
  45.     <!--  
  46.         指定spring框架的裝配模式,裝配方式有: name, type, auto, and constructor (name  
  47.         是默認裝配模式)  
  48.     -->  
  49.     <constant name="struts.objectFactory.spring.autoWire" value="name" />  
  50.   
  51.     <!-- 該屬性指定整合spring時,是否對bean進行緩存,值爲true or false,默認爲true -->  
  52.     <cosntant name="struts.objectFactory.spring.useClassCache" />  
  53.   
  54.     <!-- 指定類型檢查,包含tiger和notiger -->  
  55.     <cosntant name="struts.objectTypeDeterminer" value="tiger" />  
  56.   
  57.     <!-- 該屬性指定處理 MIME-type multipart/form-data,文件上傳 -->  
  58.     <constant name="struts.multipart.parser" value="cos" />  
  59.     <constant name="struts.multipart.parser" value="pell" />  
  60.     <constant name="struts.multipart.parser" value="jakarta" />  
  61.   
  62.     <!-- 指定上傳文件時的臨時目錄,默認使用 javax.servlet.context.tempdir -->  
  63.     <constant name="struts.multipart.saveDir" value="/tmpuploadfiles" />  
  64.   
  65.     <!-- 該屬性指定Struts 2文件上傳中整個請求內容容許的最大字節數 -->  
  66.     <constant name="struts.multipart.maxSize" value="2097152" />  
  67.   
  68.     <!--  
  69.         該屬性指定Struts2應用加載用戶自定義的屬性文件,該自定義屬性文件指定的屬性不會覆蓋  
  70.         struts.properties文件中指定的屬性。若是須要加載多個自定義屬性文件,多個自定義屬性文  
  71.         件的文件名以英文逗號(,)隔開。(也就是說不要改寫struts.properties!)  
  72.     -->  
  73.     <constant name="struts.custom.properties"  
  74.         value="application,org/apache/struts2/extension/custom" />  
  75.   
  76.     <!-- 
  77.         指定請求url與action映射器,默認爲org.apache.struts2.dispatcher.mapper.DefaultActionMapper 
  78.     -->  
  79.     <constant name="struts.mapper.class"  
  80.         value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper" />  
  81.   
  82.     <!-- 指定action的後綴,默認爲action -->  
  83.     <constant name="struts.action.extension" value="do" />  
  84.   
  85.     <!-- 被 FilterDispatcher使用指定瀏覽器是否緩存靜態內容,測試階段設置爲false,發佈階段設置爲true. -->  
  86.     <constant name="struts.serve.static.browserCache" value="true" />  
  87.   
  88.     <!-- 設置是否支持動態方法調用,true爲支持,false不支持. -->  
  89.     <constant name="struts.enable.DynamicMethodInvocation" value="true" />  
  90.   
  91.     <!-- 設置是否能夠在action中使用斜線,默認爲false不能夠,想使用需設置爲true. -->  
  92.     <constant name="struts.enable.SlashesInActionNames" value="true" />  
  93.   
  94.     <!-- 是否容許使用表達式語法,默認爲true. -->  
  95.     <constant name="struts.tag.altSyntax" value="true" />  
  96.   
  97.     <!-- 設置當struts.xml文件改動時,是否從新加載 -->  
  98.     <cosntant name="struts.configuration.xml.reload" value="true" />  
  99.   
  100.     <!-- 設置struts是否爲開發模式,默認爲false,測試階段通常設爲true. -->  
  101.     <cosntant name="struts.devMode" value="true" />  
  102.   
  103.     <!-- 設置是否每次請求,都從新加載資源文件,默認值爲false. -->  
  104.     <cosntant name="struts.i18n.reload" value="false" />  
  105.   
  106.     <!-- 標準的UI主題,默認的UI主題爲xhtml,能夠爲simple,xhtml或ajax -->  
  107.     <cosntant name="struts.ui.theme" value="xhtml" />  
  108.   
  109.     <!-- 模板目錄 -->  
  110.     <cosntant name="struts.ui.templateDir" value="template" />  
  111.   
  112.     <!-- 設置模板類型. 能夠爲 ftl, vm, or jsp -->  
  113.     <cosntant name="struts.ui.templateSuffix" value="ftl" />  
  114.   
  115.     <!-- 定位velocity.properties 文件. 默認velocity.properties -->  
  116.     <cosntant name="struts.velocity.configfile" value="velocity.properties" />  
  117.   
  118.     <!-- 設置velocity的context. -->  
  119.     <cosntant name="struts.velocity.contexts" value="...." />  
  120.   
  121.     <!-- 定位toolbox -->  
  122.     <cosntant name="struts.velocity.toolboxlocation" value="...." />  
  123.   
  124.     <!-- 指定web應用的端口 -->  
  125.     <cosntant name="struts.url.http.port" value="80" />  
  126.   
  127.     <!-- 指定加密端口 -->  
  128.     <cosntant name="struts.url.https.port" value="443" />  
  129.   
  130.     <!-- 設置生成url時,是否包含參數.值能夠爲: none,get or all -->  
  131.     <cosntant name="struts.url.includeParams" value="get" />  
  132.   
  133.     <!-- 設置要加載的國際化資源文件,以逗號分隔. -->  
  134.     <cosntant name="struts.custom.i18n.resources" value="application" />  
  135.   
  136.     <!--  
  137.         對於一些web應用服務器不能處理HttpServletRequest.getParameterMap(), 像  
  138.         WebLogic,Orion, and OC4J等,須設置成true,默認爲false.  
  139.     -->  
  140.     <cosntant name="struts.dispatcher.parametersWorkaround" value="false" />  
  141.   
  142.     <!-- 指定freemarker管理器 -->  
  143.     <cosntant name="struts.freemarker.manager.classname"  
  144.         value="org.apache.struts2.views.freemarker.FreemarkerManager" />  
  145.   
  146.     <!-- 設置是否對freemarker的模板設置緩存,效果至關於把template拷貝到 WEB_APP/templates. -->  
  147.     <cosntant name="struts.freemarker.templatesCache" value="false" />  
  148.   
  149.     <!-- 一般不須要修改此屬性. -->  
  150.     <cosntant name="struts.freemarker.wrapper.altMap" value="true" />  
  151.   
  152.     <!-- 指定xslt result是否使用樣式表緩存.開發階段設爲true,發佈階段設爲false. -->  
  153.     <cosntant name="struts.xslt.nocache" value="false" />  
  154.   
  155.     <!-- 設置struts自動加載的文件列表. -->  
  156.     <cosntant name="struts.configuration.files"  
  157.         value="struts-default.xml,struts-plugin.xml,struts.xml" />  
  158.   
  159.     <!-- 設定是否一直在最後一個slash以前的任何位置選定namespace. -->  
  160.     <cosntant name="struts.mapper.alwaysSelectFullNamespace"  
  161.         value="false" />  
  162. </struts>  

2、包配置

1. 圖片解釋:

     1.struts2框架的核心組件是 Action和攔截器等,struts2框架使用包來管理Action和攔截器等的

     2.每一個包就是多個Action、多個攔截器、多個攔截器引用的集合

2. 抽象包:除了正常的包以外,struts2還提供了抽象包

         定義:不包含action定義的包

         區分:該包的package元素增長abstract="true"

3. 包配置:

      在struts.xml中,使用package元素配置包的信息,每一個package元素定義一個包的配置

      package元素能夠定義的屬性:

         name:必填屬性,指定該包的名字,是引用該包的key

         extends:可選屬性,指定該包是否能夠繼承其餘的包:能夠繼承一個或多個父包的中Action定義、爛機器定義、攔截器棧等配置

         namespace:可選屬性,定義該包的命名空間

         abstract:可選屬性,指定該包是個抽象包,包中不能有Action的定義

    注意:Struts2的配置文件是從上到下處理的,因此父包應該在子包的前面~!(其實全部xml文件都是自上往下順序執行的)

4. 理解示例:

Xml代碼  showcase.jsp /date.jsp /empmanager/editSkill.jsp /empmanager/editSkill.jsp edit.action?skillName=${currentSkill.name} " quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">  收藏代碼
  1. <struts>  
  2.     <!-- 配置第一個包,該包名爲defalut,繼承struts-defalut -->  
  3.     <package name="default" extends="struts-defalut">  
  4.   
  5.         <!-- 下面定義了攔截器部分-->  
  6.         <interceptors>  
  7.   
  8.             <!--定義攔截器棧-->  
  9.             <interceptor-stack name="crudStack">  
  10.                 <interceptor-ref name="params" />  
  11.                 <interceptor-ref name="defalutStack" />  
  12.             </interceptor-stack>  
  13.         </interceptors>  
  14.   
  15.         <default-action-ref name="showcase" />  
  16.   
  17.         <!--定義一個Action,該Action直接映射到showcase.jsp頁面-->  
  18.         <action name="Showcase">  
  19.             <result> showcase.jsp</result>  
  20.         </action>  
  21.   
  22.         <!--定義一個Action,該Action類爲com.opensymphony.webwork.showcase.DateAction-->  
  23.         <action name="Date" class="lee.DateAction">  
  24.             <result name="success">/date.jsp </result>  
  25.         </action>  
  26.     </package>  
  27.   
  28.     <!--配置第二個包,該包名爲skill,該包繼承default包-->  
  29.     <package name="skill" extends="defalut" name="/skill">  
  30.   
  31.         <!-- 定義默認你的攔截器引用-->   
  32.                 <defalut-interceptor-ref name="crudStack"/>  
  33.   
  34.         <!--定義名爲Edit的Action,該類對應Action對應的處理類爲lee.SkillAction 
  35. -->  
  36.         <action name="Edit" class="lee.SkillAction">  
  37.             <result>/empmanager/editSkill.jsp</result>  
  38.             <intercepor-ref name="params" />  
  39.             <interceptor-ref name="basicStack" />  
  40.   
  41.         </action>  
  42.   
  43.         <!--定義名爲Save的Action,該Action對應的處理類爲lee.SkillAction,使用save方法做爲處理方法-->  
  44.         <action name="Save" class="lee.SkillAction" method="save">  
  45.             <result name="input">/empmanager/editSkill.jsp</result>  
  46.             <result type="redirect">edit.action?skillName=${currentSkill.name}  
  47.             </result>  
  48.         </action>  
  49.     </package>  
  50. </struts>    

3、命名空間配置

1.不使用命名空間的方式:

struts配置:

Xml代碼  /success.jsp /index.jsp /index.jsp " quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">  收藏代碼
  1. <struts>  
  2.     <include file="struts-default.xml" />  
  3.     <package name="com.casc.manager" extends="struts-default"   
  4.         <action name="xxn" class="com.casc.manager.XxnAction">  
  5.             <result name="success">/success.jsp</result>  
  6.              <result name="error">/index.jsp</result>  
  7.              <result name ="input" >/index.jsp</result >   
  8.         </action>  
  9.           
  10.     </package>  
  11. </struts>  

 

Html代碼 
<s:text name=<"/> " quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> 
  1. <form action="xxn.action" method="post">  
  2.         <s:text name="user.name"></s:text><input type="text" name="name"><br>  
  3.           <s:text name="user.password"></s:text><input type="password" name="password"><br>  
  4.           <input type="submit" value="<s:text name="user.submit"/>"/>  
  5. </form>  
相關文章
相關標籤/搜索