一:struts2的配置文件有哪些,相对应的作用是什么? Web.xml:包含所有必须的框架组件的web部署描述符.Struts2主要加载前端控制器及初始化参数. 1 z# P2 f3 E% [- {, o+ X! j
Struts.xml:定义应用自身使用的action映射,拦截器及常量,result/view定义.
' R$ ?* d2 U. S4 PStruts-default.xml:定义框架自身用的action映射及result
5 u3 \5 d+ u1 q) Y/ WStruts-plugin.xml:struts插件使用的配置文件
$ p" @+ v+ l8 w& U! U/ s) k8 d) [Struts.properties:定义框架自身的全局变量及框架属性.
' ^( o8 m }1 v+ }3 ~+ p
# \( e. u1 L4 R0 @7 a+ T O4 b二:配置文件的加载顺序
1 Y# d, v4 G# F% A+ R2 y4 aStruts-default.xml-->struts-plugin.xml-->struts.xml-->struts.properties-->web.xml
% u2 x& L6 }( p5 `注:若之前的配置文件设置过某一属性,则以后加载的配置文件对于相同的属性的设置,会覆盖之前的配置 三:Struts2常量的具体用法实例Xml代码 - <font color="rgb(0, 0, 0)"><font face="SimSun"><font size="2"><b><?xml version="1.0" encoding="UTF-8" ?> % ^9 |' s' {& l* b& w2 y+ X( Z& o2 h
- <!DOCTYPE struts PUBLIC ; D9 |' s# I1 [* [/ Y1 |7 a
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 9 \& m* C; _6 E6 |2 q. U
- "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 S( n& X# ]. n- C1 I; u* x
-
: g, l: k( _3 x - <struts>
5 x) P$ g& W- j/ l! X - <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
8 }8 z: \$ c# k" N+ C - <constant name="struts.i18n.encoding" value="UTF-8" /> " Y2 Z( S0 t& `3 {& f
-
1 s7 \, v6 T- ]; n! P& @ - <!--
+ q: L; L4 b5 d. P- m3 C - 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
- |8 U( s3 k, X- t6 M& G+ l! V, c7 a- E - 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 0 Y$ P, y4 V2 c7 R( _
- -->
3 c4 L% l3 C" [0 M - <constant name="struts.action.extension" value="do" />
: w4 E& z6 z8 v -
8 Y% q& J$ ~) Y" m - <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
( L' u+ C9 g% ~4 y- Y! J - <constant name="struts.serve.static.browserCache" value="false" />
2 m# F3 H+ }& F" I& O -
2 v% @' q Y8 l3 J5 W - <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 --> ! w5 `3 n! \" g; ^* k$ [! G
- <constant name="struts.configuration.xml.reload" value="true" /> + d: E! O5 ~& v, ^* h
- 1 h& {( w% O. o; Z" W/ o B, L/ n Y
- <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
5 u; I4 w% Y3 T$ h- E' N, t - <constant name="struts.devMode" value="true" />
t2 x/ i) q! L6 r -
! l2 H8 s1 v# [2 Q* t" B - <!-- 默认的视图主题 --> % p0 H0 {2 I6 j& R7 t# `: k
- <constant name="struts.ui.theme" value="simple" />
5 P: S9 u& {1 H% M -
- L s/ Z8 d; |- U - <!-- spring 托管 -->
3 a- s* ]# P. R9 M - <constant name="struts.objectFactory" value="spring" /> - z' h0 g. B3 e( o: ]8 E
-
1 e) d8 w% b5 Y* e' H - <!-- # v) M6 l& B: x+ K
- 指定加载struts2配置文件管理器,默认为org.apache.struts2.config.DefaultConfiguration & ]9 j5 p5 g1 o' s! ?. N
- 开发者可以自定义配置文件管理器,该类要实现Configuration接口,可以自动加载struts2配置文件。
( k) q: v5 i/ h& D; s! j - --> % e/ V2 X# _& }) q! t
- <constant name="struts.configuration" ! s- F) D7 t% W# m, x! B( e
- value="org.apache.struts2.config.DefaultConfiguration" />
0 B3 }9 K3 a$ p7 z - : F2 m) V1 d" M
- <!-- 设置默认的locale和字符编码 -->
/ y1 t& Z8 q3 c+ M F8 v - <constant name="struts.locale" value="zh_CN" /> 7 z0 b" \ E% X' n% W5 X; V
- <constant name="struts.i18n.encoding" value="GBK" /> 5 D! Z1 y9 g8 h; D2 _7 L
- 0 n/ y: H9 F; q% i8 B! K. W
- <!-- 指定Struts的工厂类 -->
5 Z/ m+ B2 ?4 `- S, a/ S( ` - <constant name="struts.objectFactory" value="spring"></constant>
" ?( A8 u1 a, M2 y- [ Z9 ^ - 8 f( A6 [& b& \: f' u' f+ c4 [
- <!-- 0 {" G* P9 z% Q i; w+ z! `6 P
- 指定spring框架的装配模式,装配方式有: name, type, auto, and constructor (name
) L% a1 [3 h$ D+ v K* { - 是默认装配模式)
7 f2 \" L: Z+ w. x* |: P! T/ s - -->
. d' @9 S5 w, _. c - <constant name="struts.objectFactory.spring.autoWire" value="name" /> : K- S. H/ S! L, I1 m( R( A
- ( j/ p1 [& o% [: d2 w8 G
- <!-- 该属性指定整合spring时,是否对bean进行缓存,值为true or false,默认为true --> K1 Y. l, q8 d2 K% D3 C# z
- <cosntant name="struts.objectFactory.spring.useClassCache" />
$ `# e5 I: ~2 G8 ^ - . m! P$ u$ [% W1 M
- <!-- 指定类型检查,包含tiger和notiger --> & [% d% o4 y# Y/ f$ u7 B8 s
- <cosntant name="struts.objectTypeDeterminer" value="tiger" /> ' v" S. S, j& Y, C
- 9 K6 d9 h/ p6 Y+ F
- <!-- 该属性指定处理 MIME-type multipart/form-data,文件上传 -->
- z, x; B) c, B7 T7 U* k4 i - <constant name="struts.multipart.parser" value="cos" />
- v# S5 b- i" U3 s' E) P/ j1 o - <constant name="struts.multipart.parser" value="pell" />
O, d9 `6 {2 M2 y+ `+ h X - <constant name="struts.multipart.parser" value="jakarta" />
, M- w0 l$ Z# V -
, J/ g l% S6 N) a - <!-- 指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdir --> " p0 B- [ F- b" \* s: [! K1 e1 r
- <constant name="struts.multipart.saveDir" value="/tmpuploadfiles" /> & H; G2 S o1 n1 ?4 k
- + N E) w, {; b6 N6 L) e* z
- <!-- 该属性指定Struts 2文件上传中整个请求内容允许的最大字节数 --> 8 f. S! O: f4 n
- <constant name="struts.multipart.maxSize" value="2097152" />
, K2 Q) D: z0 K6 [( M; Y" Y -
W8 B; ]' ~# T+ U+ m - <!-- 5 S3 N3 ]0 d" Z- K. z5 l
- 该属性指定Struts2应用加载用户自定义的属性文件,该自定义属性文件指定的属性不会覆盖
$ B& o" x6 ? P- s: S- X5 T/ @( ] - struts.properties文件中指定的属性。如果需要加载多个自定义属性文件,多个自定义属性文
/ E( _4 C8 p7 O0 E# F: i6 g% a - 件的文件名以英文逗号(,)隔开。(也就是说不要改写struts.properties!)
& A; c( y# W7 M. N4 M+ G$ j: ` - --> + b5 V5 M. C4 l2 p' X: N: o
- <constant name="struts.custom.properties"
- B2 U6 k$ a7 `7 J4 Y5 x - value="application,org/apache/struts2/extension/custom" /> ( e8 c6 f( X; Z% ]# g* d3 p
- , b, i' C0 {2 w
- <!-- 指定请求url与action映射器,默认为org.apache.struts2.dispatcher.mapper.DefaultActionMapper -->
, q6 n- W$ j' J. _6 ^; }7 E - <constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper" />
" m: V6 A% ^, `2 M - - ~: F4 d% z% i8 C& V5 I. \
- <!-- 指定action的后缀,默认为action -->
- U$ u" k* {. K - <constant name="struts.action.extension" value="do" />
( w5 u7 q3 p5 I Y - , N9 ^+ K" Q0 j2 N+ B- s$ S- f G
- <!-- 被 FilterDispatcher使用指定浏览器是否缓存静态内容,测试阶段设置为false,发布阶段设置为true. --> 8 |- G' H/ |7 ^/ _
- <constant name="struts.serve.static.browserCache" value="true" />
$ i0 O1 g* f, v2 Q: m - 4 U, j) I2 |% G# K: I! D2 J
- <!-- 设置是否支持动态方法调用,true为支持,false不支持. --> 0 ?1 }; Z2 B$ w; \) \7 O' Y
- <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 7 C9 T H; x2 i! D1 w
-
1 V; K6 V* X9 S; M8 ~* u5 C! H; n% d - <!-- 设置是否可以在action中使用斜线,默认为false不可以,想使用需设置为true. --> : s- n# o' D1 N9 {
- <constant name="struts.enable.SlashesInActionNames" value="true" /> 7 }$ v9 x; w, v: U- U8 _
- 7 A; X$ m- I4 r/ E' ?
- <!-- 是否允许使用表达式语法,默认为true. --> $ z7 n7 i( m4 ^- U4 t
- <constant name="struts.tag.altSyntax" value="true" />
$ h9 k# V. ?5 i0 ^: g2 V -
* Y v+ B$ j1 z - <!-- 设置当struts.xml文件改动时,是否重新加载 --> 6 L6 e: i8 ]1 b# m" F; c
- <cosntant name="struts.configuration.xml.reload" value="true" /> # p. g1 S0 f: M/ ?2 {+ q4 c# M
- 5 d8 Y( F( T+ K9 ~
- <!-- 设置struts是否为开发模式,默认为false,测试阶段一般设为true. --> 9 A% C7 H3 e5 o0 D. ` ~, y% e
- <cosntant name="struts.devMode" value="true" />
$ l% t" J: I5 @ - - K; I6 n6 m: A. j# a
- <!-- 设置是否每次请求,都重新加载资源文件,默认值为false. -->
- e# T# ?" }# ?: q7 c& Z# n - <cosntant name="struts.i18n.reload" value="false" /> 9 o" H0 k8 d2 M. j
- - R* Y0 [' v+ Y
- <!-- 标准的UI主题,默认的UI主题为xhtml,可以为simple,xhtml或ajax -->
+ P4 ?0 q0 X, I5 o: [2 s; } - <cosntant name="struts.ui.theme" value="xhtml" />
( ~) o0 T! y( ^ l3 V/ @ -
G2 D9 f2 z$ z' i* V - <!-- 模板目录 -->
) N; r7 u- K) I* C" u - <cosntant name="struts.ui.templateDir" value="template" />
# a" @2 ^: l# } - % U- e! {0 \8 H1 n9 Q" C
- <!-- 设置模板类型. 可以为 ftl, vm, or jsp -->
7 }; h" }3 W9 }3 d - <cosntant name="struts.ui.templateSuffix" value="ftl" /> ( P9 Q& C1 Y) g( O- p3 H
- 9 T7 |; L1 v8 n! p
- <!-- 定位velocity.properties 文件. 默认velocity.properties --> 1 L; D- O! }, S2 V: C" E
- <cosntant name="struts.velocity.configfile" value="velocity.properties" /> ' o! d0 W, J5 |
-
: O; S+ o$ k8 C% h r o' J# \8 F - <!-- 设置velocity的context. --> ) |* u9 S7 N2 p: l Y
- <cosntant name="struts.velocity.contexts" value="...." />
0 H& P4 ^2 A V H6 r/ G -
7 p4 m7 u0 L V! e* m - <!-- 定位toolbox --> : l3 |) v6 d# ^6 |& k1 y# x; O
- <cosntant name="struts.velocity.toolboxlocation" value="...." /> " c; f# F F3 ~
- / }8 H: C U9 W+ a5 h
- <!-- 指定web应用的端口 -->
; w: V9 T% E* B2 Z - <cosntant name="struts.url.http.port" value="80" /> ' f6 S; O: Z2 |) b
- 2 S; {3 B$ L2 F1 j% `9 R- ^% k5 U
- <!-- 指定加密端口 -->
" ?/ [6 k: N" |' p; x - <cosntant name="struts.url.https.port" value="443" /> % `) X2 d6 [8 [) c: y) W
- ; m- u2 ?) b& l* V" F! T
- <!-- 设置生成url时,是否包含参数.值可以为: none,get or all -->
7 z* \6 Y3 l) [0 o - <cosntant name="struts.url.includeParams" value="get" /> ; E0 C/ A) C9 J6 w8 N( O: o. E
-
9 r$ b, S' c; z! Y, ^# n# | - <!-- 设置要加载的国际化资源文件,以逗号分隔. --> ) G7 j! G- @, D. k, H
- <cosntant name="struts.custom.i18n.resources" value="application" /> 9 F0 y4 L- L0 }* m1 i
- 8 e8 J, z/ Y& M" R
- <!-- 对于一些web应用服务器不能处理HttpServletRequest.getParameterMap(),
5 V& s3 ]8 g- x; ^ z2 M" \( \ - 像 WebLogic,Orion, and OC4J等,须设置成true,默认为false. --> |' J8 d2 P1 o( h/ I
- <cosntant name="struts.dispatcher.parametersWorkaround" value="false" />
) r$ I8 G1 e2 R) Z. Y5 J- Z -
- ?5 h" o0 k3 A4 Y! l3 \ - <!-- 指定freemarker管理器 --> , E" T: ^& b# |2 I
- <cosntant name="struts.freemarker.manager.classname" value="org.apache.struts2.views.freemarker.FreemarkerManager" /> $ ]& v; M( t$ g% Q: P* f
-
6 C2 b; C. y Y* z- Y& j4 {: V - <!-- 设置是否对freemarker的模板设置缓存,效果相当于把template拷贝到 WEB_APP/templates. -->
' {; X. s/ k r) N+ } - <cosntant name="struts.freemarker.templatesCache" value="false" />
! W0 a, r( Q. ~9 ~6 _- p% e - - A# V* D# O7 r0 P
- <!-- 通常不需要修改此属性. --> % c, Q8 ~% |7 e( h3 |& M
- <cosntant name="struts.freemarker.wrapper.altMap" value="true" />
: O: g4 R0 s v' t) j1 i - + G S5 ^! w1 h! d ^9 u# h
- <!-- 指定xslt result是否使用样式表缓存.开发阶段设为true,发布阶段设为false. --> 4 ?( T# q6 r4 L1 Z7 `4 l, V ]
- <cosntant name="struts.xslt.nocache" value="false" />
, M2 o. j# L, S -
% J' I$ m5 O0 ?1 l+ _ - <!-- 设置struts自动加载的文件列表. -->
* O) L: a" I% m6 y; g: P) d+ U) x! H - <cosntant name="struts.configuration.files" value="struts-default.xml,struts-plugin.xml,struts.xml" /> $ T# g; B- _3 r8 q# l3 _- Q
- . l, f$ }& r+ m4 y. }2 P. S
- <!-- 设定是否一直在最后一个slash之前的任何位置选定namespace. --> 3 }- @0 g, T7 g0 Z. \' s
- <cosntant name="struts.mapper.alwaysSelectFullNamespace" value="false" />
' ]9 k- q: c' g8 S- i - </struts>
* d s1 @/ l0 B' t) C -
+ \1 f; l4 f9 f1 `6 w2 y$ G -
- {( ~# F9 T4 w, f8 y* x' w5 W* K' U -
+ q8 Q4 R! @+ R2 I+ E - <struts>
# [& x6 }; q& x% g+ t4 w0 [$ e - + D0 B0 V0 |6 o, I: D+ I
- <!-- include节点是struts2中组件化的方式 可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 --> . [7 f; L* V1 a3 J3 Y6 Y
- <include file="struts-default.xml"></include> ! n% \7 G" H9 u+ X5 Z3 o1 u3 D
- 1 `% X' S- Z! `
-
1 ?6 n8 C. ~2 h; K0 X - <!-- package提供了将多个Action组织为一个模块的方式 2 u& r T- R( R
- package的名字必须是唯一的 package可以扩展 当一个package扩展自 # N# f* w- l5 o# H3 ^# ]3 A
- 另一个package时该package会在本身配置的基础上加入扩展的package ; |$ {* ?- \, q% N4 f4 k! d$ g
- 的配置 父package必须在子package前配置 & J1 G8 ^! i' {( Y0 w- ~3 O/ w( z
- name:package名称
/ J8 d' ~4 u7 P1 w* q5 M _ - extends:继承的父package名称
- J( D% [" {$ i2 o - abstract:设置package的属性为抽象的 抽象的package不能定义action 值true:false
9 o9 {6 l4 k; J& u - namespace:定义package命名空间 该命名空间影响到url的地址,例如此命名空间为/test那么访问是的地址为http://localhost:8080/struts2/test/XX.action
3 j1 Z! Q8 h) A4 _2 k( j - --> ( ^ S; y- n$ o, N, E
- <package name="com.kay.struts2" extends="struts-default" namespace="/test"> L1 @9 T# M$ \" ^2 b/ B' p( L
- <interceptors> ; e$ j1 ~& }; F
- <!-- 定义拦截器
. I+ ] ~; j2 e+ F/ ^, T; c9 x! y - name:拦截器名称 % @* ]8 V) @5 D0 Y5 O L
- class:拦截器类路径 2 n9 e7 j2 n& }* f- M# t% T
- --> & P b7 s3 O- t: h3 b$ \2 A4 o: H
- <interceptor name="timer" class="com.kay.timer"></interceptor>
9 T, l7 E3 z4 ~7 l: g* w - <interceptor name="logger" class="com.kay.logger"></interceptor> 2 S! ?4 S1 U, {6 n: Q
- <!-- 定义拦截器栈 --> ; {/ X5 ?' X8 C
- <interceptor-stack name="mystack"> $ U' _9 p2 k% T1 l" a& d1 K. f
- <interceptor-ref name="timer"></interceptor-ref> : {, B* p$ f8 h( J
- <interceptor-ref name="logger"></interceptor-ref> 6 `0 L. E; C$ w* L$ b. y: P* {3 s
- </interceptor-stack>
, Z( }% G% d4 K3 k6 L& {4 |) l - </interceptors> 2 F: S' ^: ~0 o. M! c0 Y% g" W
-
: f; t2 i+ U& w, H8 m% b - <!-- 定义默认的拦截器 每个Action都会自动引用 8 h6 Y$ D8 R* ?/ _: h2 ~; u
- 如果Action中引用了其它的拦截器 默认的拦截器将无效 --> . h$ s- ^0 \& K) T) B
- <default-interceptor-ref name="mystack"></default-interceptor-ref> ! ]4 l) `4 z$ U3 R! e
-
# ~, }' { i: i -
! Y" D) E. Q$ x4 o% u3 C- m; J+ h. I - <!-- 全局results配置 -->
! V' t+ x8 X' { - <global-results> & ?9 r8 C$ o7 @; ^8 T) a
- <result name="input">/error.jsp</result> 8 ^2 r6 [; G; p& A( ~/ a: g) i: c
- </global-results> * ~4 F4 D* J* W9 ]# f8 j1 W6 ]/ Y
- + _: @3 i# N7 M1 d% ^7 N8 S
- <!-- Action配置 一个Action可以被多次映射(只要action配置中的name不同) & V. q/ H, C2 \6 I/ E! h6 @
- name:action名称 * H; j! z3 K; F
- class: 对应的类的路径
. M* w+ O5 h+ j2 V# g1 z# _ - method: 调用Action中的方法名 0 b% p2 }% t% M
- -->
; p- c0 A3 i7 M& Y# m8 o8 [5 h - <action name="hello" class="com.kay.struts2.Action.LoginAction"> ' @4 B- |' \8 n- [
- <!-- 引用拦截器
' z$ w) z+ k7 u3 {/ q h- `& O - name:拦截器名称或拦截器栈名称 - s9 Z3 f& C! u- b
- -->
$ J2 c4 b: ]0 Z' M - <interceptor-ref name="timer"></interceptor-ref> & U2 }* J" Z- R) y( ?9 g- ~* Z! U x
-
8 i# j' }2 p) i H* X9 k - <!-- 节点配置 " _. s# C9 [+ S& s
- name : result名称 和Action中返回的值相同
$ T) m' H% N9 R3 ?; ^! N - type : result类型 不写则选用superpackage的type struts-default.xml中的默认为dispatcher $ E+ e0 O' B! Y
- --> 7 L7 @/ F4 i& k# L3 d5 d
- <result name="success" type="dispatcher">/talk.jsp</result>
3 Q/ u8 G9 V4 F, |3 Y* v9 _* H4 m - <!-- 参数设置
) n9 k U7 Y# i, H - name:对应Action中的get/set方法
* I5 `7 Y7 B3 X6 Z$ x$ j - -->
2 }/ ~0 r. |/ f, h) r+ ]/ r; w - <param name="url">http://www.sina.com</param>
1 t; b% d. Z" Y p - </action> 6 [; {! e: O' w i: K
- </package> 2 i: h, Z- w- Q
- </struts></b></font></font></font>
复制代码 这些是常用的struts配置文件常量,如果想把struts用好,就要懂得每个配置是怎么一回事.不光光需要学习技术,更要学习好理论知识.技术的高度是由理论知识的深度决定的.
1 v k, A6 p. I" }) M0 Z4 B" Y
) x6 J: m5 T, R3 }2 q |