計應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
- <constant name="struts.i18n.encoding" value="gbk"></constant>
在struts.properties的配置形式以下:
[html] view plaincopy
- struts.i18n.encoding=UTF-8
在web.xml中配置以下:
[html] view plaincopy
- <filter>
-
- <filter-name>struts2</filter-name>
-
- <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
-
- </filter-class>
-
- <init-param>
- 10.
11. <param-name>struts.i18n.encoding</param-name>
- 12.
13. <param-value>UTF-8</param-value>
- 14.
15. </init-param>
- 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這兩個屬性
- <!-指定struts2的SilterDispatcher的Filter-!>
- <filter>
- <!-指定Struts2的核心Filter-!>
-
- <filter-name>strurs2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispaycher
- </filter-class>
-
- <!-經過init-param元素配置struts2常量-!>
-
- <init-param>
- <param-name>struts.custom.il8n.resources</param-name>
- <param-va>
常量詳解
Xml代碼 " quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
- <?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>
- <!-- 指定Web應用的默認編碼集,至關於調用HttpServletRequest的setCharacterEncoding方法 -->
- <constant name="struts.i18n.encoding" value="UTF-8" />
-
- <!--
- 該屬性指定須要Struts 2處理的請求後綴,該屬性的默認值是action,即全部匹配*.action的請求都由Struts2處理。
- 若是用戶須要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開。
- -->
- <constant name="struts.action.extension" value="do" />
-
- <!-- 設置瀏覽器是否緩存靜態內容,默認值爲true(生產環境下使用),開發階段最好關閉 -->
- <constant name="struts.serve.static.browserCache" value="false" />
-
- <!-- 當struts的配置文件修改後,系統是否自動從新加載該文件,默認值爲false(生產環境下使用),開發階段最好打開 -->
- <constant name="struts.configuration.xml.reload" value="true" />
-
- <!-- 開發模式下使用,這樣能夠打印出更詳細的錯誤信息 -->
- <constant name="struts.devMode" value="true" />
-
- <!-- 默認的視圖主題 -->
- <constant name="struts.ui.theme" value="simple" />
-
- <!-- spring 託管 -->
- <constant name="struts.objectFactory" value="spring" />
-
- <!--
- 指定加載struts2配置文件管理器,默認爲org.apache.struts2.config.DefaultConfiguration
- 開發者能夠自定義配置文件管理器,該類要實現Configuration接口,能夠自動加載struts2配置文件。
- -->
- <constant name="struts.configuration"
- value="org.apache.struts2.config.DefaultConfiguration" />
-
- <!-- 設置默認的locale和字符編碼 -->
- <constant name="struts.locale" value="zh_CN" />
- <constant name="struts.i18n.encoding" value="GBK" />
-
- <!-- 指定Struts的工廠類 -->
- <constant name="struts.objectFactory" value="spring"></constant>
-
- <!--
- 指定spring框架的裝配模式,裝配方式有: name, type, auto, and constructor (name
- 是默認裝配模式)
- -->
- <constant name="struts.objectFactory.spring.autoWire" value="name" />
-
- <!-- 該屬性指定整合spring時,是否對bean進行緩存,值爲true or false,默認爲true -->
- <cosntant name="struts.objectFactory.spring.useClassCache" />
-
- <!-- 指定類型檢查,包含tiger和notiger -->
- <cosntant name="struts.objectTypeDeterminer" value="tiger" />
-
- <!-- 該屬性指定處理 MIME-type multipart/form-data,文件上傳 -->
- <constant name="struts.multipart.parser" value="cos" />
- <constant name="struts.multipart.parser" value="pell" />
- <constant name="struts.multipart.parser" value="jakarta" />
-
- <!-- 指定上傳文件時的臨時目錄,默認使用 javax.servlet.context.tempdir -->
- <constant name="struts.multipart.saveDir" value="/tmpuploadfiles" />
-
- <!-- 該屬性指定Struts 2文件上傳中整個請求內容容許的最大字節數 -->
- <constant name="struts.multipart.maxSize" value="2097152" />
-
- <!--
- 該屬性指定Struts2應用加載用戶自定義的屬性文件,該自定義屬性文件指定的屬性不會覆蓋
- struts.properties文件中指定的屬性。若是須要加載多個自定義屬性文件,多個自定義屬性文
- 件的文件名以英文逗號(,)隔開。(也就是說不要改寫struts.properties!)
- -->
- <constant name="struts.custom.properties"
- value="application,org/apache/struts2/extension/custom" />
-
- <!--
- 指定請求url與action映射器,默認爲org.apache.struts2.dispatcher.mapper.DefaultActionMapper
- -->
- <constant name="struts.mapper.class"
- value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper" />
-
- <!-- 指定action的後綴,默認爲action -->
- <constant name="struts.action.extension" value="do" />
-
- <!-- 被 FilterDispatcher使用指定瀏覽器是否緩存靜態內容,測試階段設置爲false,發佈階段設置爲true. -->
- <constant name="struts.serve.static.browserCache" value="true" />
-
- <!-- 設置是否支持動態方法調用,true爲支持,false不支持. -->
- <constant name="struts.enable.DynamicMethodInvocation" value="true" />
-
- <!-- 設置是否能夠在action中使用斜線,默認爲false不能夠,想使用需設置爲true. -->
- <constant name="struts.enable.SlashesInActionNames" value="true" />
-
- <!-- 是否容許使用表達式語法,默認爲true. -->
- <constant name="struts.tag.altSyntax" value="true" />
-
- <!-- 設置當struts.xml文件改動時,是否從新加載 -->
- <cosntant name="struts.configuration.xml.reload" value="true" />
-
- <!-- 設置struts是否爲開發模式,默認爲false,測試階段通常設爲true. -->
- <cosntant name="struts.devMode" value="true" />
-
- <!-- 設置是否每次請求,都從新加載資源文件,默認值爲false. -->
- <cosntant name="struts.i18n.reload" value="false" />
-
- <!-- 標準的UI主題,默認的UI主題爲xhtml,能夠爲simple,xhtml或ajax -->
- <cosntant name="struts.ui.theme" value="xhtml" />
-
- <!-- 模板目錄 -->
- <cosntant name="struts.ui.templateDir" value="template" />
-
- <!-- 設置模板類型. 能夠爲 ftl, vm, or jsp -->
- <cosntant name="struts.ui.templateSuffix" value="ftl" />
-
- <!-- 定位velocity.properties 文件. 默認velocity.properties -->
- <cosntant name="struts.velocity.configfile" value="velocity.properties" />
-
- <!-- 設置velocity的context. -->
- <cosntant name="struts.velocity.contexts" value="...." />
-
- <!-- 定位toolbox -->
- <cosntant name="struts.velocity.toolboxlocation" value="...." />
-
- <!-- 指定web應用的端口 -->
- <cosntant name="struts.url.http.port" value="80" />
-
- <!-- 指定加密端口 -->
- <cosntant name="struts.url.https.port" value="443" />
-
- <!-- 設置生成url時,是否包含參數.值能夠爲: none,get or all -->
- <cosntant name="struts.url.includeParams" value="get" />
-
- <!-- 設置要加載的國際化資源文件,以逗號分隔. -->
- <cosntant name="struts.custom.i18n.resources" value="application" />
-
- <!--
- 對於一些web應用服務器不能處理HttpServletRequest.getParameterMap(), 像
- WebLogic,Orion, and OC4J等,須設置成true,默認爲false.
- -->
- <cosntant name="struts.dispatcher.parametersWorkaround" value="false" />
-
- <!-- 指定freemarker管理器 -->
- <cosntant name="struts.freemarker.manager.classname"
- value="org.apache.struts2.views.freemarker.FreemarkerManager" />
-
- <!-- 設置是否對freemarker的模板設置緩存,效果至關於把template拷貝到 WEB_APP/templates. -->
- <cosntant name="struts.freemarker.templatesCache" value="false" />
-
- <!-- 一般不須要修改此屬性. -->
- <cosntant name="struts.freemarker.wrapper.altMap" value="true" />
-
- <!-- 指定xslt result是否使用樣式表緩存.開發階段設爲true,發佈階段設爲false. -->
- <cosntant name="struts.xslt.nocache" value="false" />
-
- <!-- 設置struts自動加載的文件列表. -->
- <cosntant name="struts.configuration.files"
- value="struts-default.xml,struts-plugin.xml,struts.xml" />
-
- <!-- 設定是否一直在最後一個slash以前的任何位置選定namespace. -->
- <cosntant name="struts.mapper.alwaysSelectFullNamespace"
- value="false" />
- </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">
- <struts>
- <!-- 配置第一個包,該包名爲defalut,繼承struts-defalut -->
- <package name="default" extends="struts-defalut">
-
- <!-- 下面定義了攔截器部分-->
- <interceptors>
-
- <!--定義攔截器棧-->
- <interceptor-stack name="crudStack">
- <interceptor-ref name="params" />
- <interceptor-ref name="defalutStack" />
- </interceptor-stack>
- </interceptors>
-
- <default-action-ref name="showcase" />
-
- <!--定義一個Action,該Action直接映射到showcase.jsp頁面-->
- <action name="Showcase">
- <result> showcase.jsp</result>
- </action>
-
- <!--定義一個Action,該Action類爲com.opensymphony.webwork.showcase.DateAction-->
- <action name="Date" class="lee.DateAction">
- <result name="success">/date.jsp </result>
- </action>
- </package>
-
- <!--配置第二個包,該包名爲skill,該包繼承default包-->
- <package name="skill" extends="defalut" name="/skill">
-
- <!-- 定義默認你的攔截器引用-->
- <defalut-interceptor-ref name="crudStack"/>
-
- <!--定義名爲Edit的Action,該類對應Action對應的處理類爲lee.SkillAction
- -->
- <action name="Edit" class="lee.SkillAction">
- <result>/empmanager/editSkill.jsp</result>
- <intercepor-ref name="params" />
- <interceptor-ref name="basicStack" />
-
- </action>
-
- <!--定義名爲Save的Action,該Action對應的處理類爲lee.SkillAction,使用save方法做爲處理方法-->
- <action name="Save" class="lee.SkillAction" method="save">
- <result name="input">/empmanager/editSkill.jsp</result>
- <result type="redirect">edit.action?skillName=${currentSkill.name}
- </result>
- </action>
- </package>
- </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">
- <struts>
- <include file="struts-default.xml" />
- <package name="com.casc.manager" extends="struts-default"
- <action name="xxn" class="com.casc.manager.XxnAction">
- <result name="success">/success.jsp</result>
- <result name="error">/index.jsp</result>
- <result name ="input" >/index.jsp</result >
- </action>
-
- </package>
- </struts>
Html代碼
<s:text name=<"/> " quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
- <form action="xxn.action" method="post">
- <s:text name="user.name"></s:text><input type="text" name="name"><br>
- <s:text name="user.password"></s:text><input type="password" name="password"><br>
- <input type="submit" value="<s:text name="user.submit"/>"/>
- </form>