该用户从未签到
|
1.1.自从struts2.13、以后之前的FilterDispatcher标注为过时 # w7 x6 l/ h' @/ `" L% K
2.<filter-class> 1 O5 G) d( a0 e, @3 Z6 n
3. org.apache.struts.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter - a' u5 |8 G, u. j3 t
4.</filter-class>
/ z, w, O2 G- K/ `# z5.
1 ?+ w3 h4 o; f2 m6.2.action中默认的method为execute() & r. v& R: J9 i h" \
7. 默认class为ActionSupport
: X/ v7 I0 \" W6 M8. 默认name 为success; % T- z3 W2 G8 Y) V
9.
: E& k* b" t1 m. }10.3.转发类型 传中文参数URLEncoder.encode("","UTF-8");
$ C5 i4 _ r' _11.forward
5 O- ~1 X) C% u7 t/ N P/ B12.
s0 U' y7 I, E/ H13.redirect 浏览器跳转 ) [. C8 Q; }, z9 R; v+ s) N4 `
14.<result type="redirect">/index.jsp</param> & b2 p8 x2 V; u3 H
15. 0 B/ A% Q9 q ~/ d7 C
16.redirectAction(跳转到另一action)
$ h; }: }! Y( U7 V17.<result type="redirectAction">
8 j$ C* {0 Z3 P! r U18.<param name="actionName">xxx</param>
r( d3 [* {+ [( Q& ^19.<param name="namespace">/xxx/xxx</param> : a$ P, C! U& I) e6 Y: k8 }: B
20.</result> * U9 z4 S: g5 t& q# e3 c
21. $ \; w F6 [( c+ O
22.plainText(返回jsp页面的源代码) 中文问题
3 P( L( z3 e1 Z A, |23.<result type="plainText"> : B% Y2 F: N% o0 A: W
24.<param name="location">index.jsp</param> + x) d& x' u* b5 o, ^2 s* n
25.<param name="charSet">UTF-8</param>
4 @4 v6 o! c+ N8 n* g( ?26.</result> . D/ G. `4 E& x7 r% O* h3 C/ N" m
27.
/ o+ ^9 y7 S; T# o/ y3 o* m28.4.共用 9 s! D" R" U! i9 d5 X/ O
29.<global-results>
8 e2 d/ g- V; Y8 t# X5 D9 N! u! c30. <result name="success">index.jsp</result> # o8 O) X {2 a" g) f* ]
31.</global-results> ; F2 ?* U5 M. _7 V2 ?4 U. j
32.
% u5 w! [2 u8 g! M33.5.给action中的参数赋值 4 S2 e9 U" O7 W( }
34.<action ---->
+ G* r5 w0 T4 D& G! f# T T35. <param name="param_name">xxx</param> - o0 g5 J$ P$ I1 z) A. L
36. <result>index.jsp</result>
% X- p( }6 m+ r& n2 ]- Q; y37.</action> 0 b) Z1 _; x! F q1 S9 ]9 Z
38.
5 p0 T0 I/ E7 h39.6.常量标签
6 l5 `* S& B' h" f40.<指定截取的后缀
+ R1 i6 q Q$ C0 N41.<constant name="struts.action.extension" value="do,action"/> 4 n! \8 S" [& Q+ g H
42.
+ H' E% ?0 H/ f; l6 T. r- P" |$ c43.指定默认编码集 作用于HttpServletRequest的setCharacterEncoding的方法 5 j9 ]& Y, b8 a% y9 M) J- ]9 \' x
44.<constant name="struts.i18n.encoding" value="UTF-8"/> / W! m3 b0 `8 L' ]8 E7 j* u4 X
45.
' C7 I% |3 Z8 N46.设置浏览器是否缓存静态页面 默认值为true
2 o( b8 g3 h, K; A47.<constant name="struts.serve.static.browserCache" value="true"/> ~0 I+ v* A, r ?7 N
48. " d z$ J6 @5 h
49.当struts的配置文件被修改后,系统是否自动重新加载文件 默认值为false ( V ~: A' P% `' W
50.<constant name="struts.configuration.xml.reload" value="true"/>
- j. r, f0 J+ a51. H; e0 }, E: V$ C0 \* l/ S
52.打印详细错误信息 开发阶段可以打开
7 S( z2 O5 O0 l y6 \# M) y$ T. u53.<constant name="struts.devMode" value="true"/>
0 [9 j. t+ Q# q4 u4 _54. 2 X/ \. T A. ~
55.与spring集成时,指定由spring负责的action对象创建 5 p2 r: b; S1 M! y" V$ [
56.<constant name="struts.objectFactory" value="spring"/>
T) v$ E: @, j/ }! P- i57. , K9 w5 Z8 @( |. D0 U
58.上传文件大小限制 默认大小为10701096 9 P1 M2 r& c3 Z% U
59.<constant name="struts.multipartmaxSize" value="10701096"/>
+ M, m) Z$ C0 P- A1 j" f60.
' C' M* a. P" W- }6 j: k61.struts2加载常量的搜索顺序 7 l5 }8 w, k0 Q0 a5 X
62.struts-default.xml->struts-plugin.xml->struts.xml->struts.properties->web.xml
5 s1 E0 |7 L1 p) s6 p' L" }8 q( T8 T63.如果在多个配置中定义了同一常量则后一文件中的常量将覆盖前面的常量
! y" {% A- \- S" [64.
& {" v( B( n! D2 q3 h2 ~! |65.主题 * `) `- v& q3 R: n2 T' a# y
66.<constant name="struts.ui.theme" value="simple"/>
4 B% Q( ^6 L! j67.7.指定调用方法 默认是执行execute $ q, e/ F9 e2 U1 ~# L
68.动态方法调用:
" p3 i" Z" ?$ T0 B% g; M/ c69.<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
8 `% c; G( B2 @& ^70.在访问路径后加!methodname 如/example/list!add.action % h( C- ^3 w2 m% @% |1 x; x& S
71. ) s$ w- n% e" L) n
72.也可以用通配符来确认
: @: P5 H) D% s- c73.<action name="list_*_*" class="com.ss.action.{1}Action" method="{1}"> 1 ?( y, t( Y$ X8 s# ~# ]
74. <result name="success">/{2}.jsp</result> & u$ l3 G# h0 i
75.</action> " V+ c. ~& M9 _ k
76. 5 A8 l! a8 C0 B
77.8.2.1.6版本 接受中文请求参数出现乱版 % q5 x1 y/ v! G2 }: X
78.新建一个过滤器filter放到struts2的filter之前,然后在doFilter()方法中添加代码
5 [0 v) H) F, |% E& j! w79.public void doFilter(){ 2 f, m- P2 ?/ d/ c {2 y
80. HttpServletRequest req=(HttpServletRequest)request;
% s' ~2 a7 y) _) N81. req.setCharacterEncoding("UTF-8"); # Q4 O# }& V6 B/ D
82. filterchain.doFilter(request,response);
+ _# O- l, c4 o7 Y* [) O83.}
3 i& e4 u! W4 G3 ^% @7 a84. / Q9 X5 ^7 L; y+ |3 b' R0 o
85.9.定义类型转换器 5 g- s% u1 R. s. k" M( J
86.一、局部
& R( E7 `" Z9 p1 p3 `87.定义一个类并继承DefaultTypeConverter从写 4 c6 l: e* ^3 `% M" w6 l& L( }
88.convertValue(Map<String,Object> context,Object value,Class toType){ / k! @; J) e6 ?$ H" ]7 @
89. SimpleDateFormat date=new SimpleDateFormat("yyyymmdd"); 6 a: z* O I8 r; W& B
90.try{
3 }7 Z/ |6 L% B O91. if(toType==Date.class){
% R, A8 m: x( s6 h$ N" X( j92. String[] params=(String[])value; 3 K* u1 A4 X7 i2 g9 F! x; D: \
93. return dateFormat.parse(params[0]); : R" F+ z& s4 `7 C
94. }else if(toType==String.class){ 7 I; _$ |1 Z* B9 r! P
95. Date date=(Date)value; , X4 y% V! s( i0 Q, l
96. return dateFormat.format(date);
" J2 a6 r2 m. L0 f- Y/ k- b& ?97. }
5 D/ e) G7 x5 u5 O4 d3 J98. }catch(ParseException e){ 7 b; N2 F8 a2 T; ?4 d
99. return null; * B3 P9 ^. Y0 W [& D* L+ m
100. } * ] b0 G* w2 A
101.}
% M9 ~$ m& A- j* S& T( t4 t102.注册:在Action类包下放置<ActionClassName>-conversion.properties文件 . W' X* G+ e9 n. {5 U
103. 属性名=包名.定义的类
) V9 c$ q7 K/ h6 ~' Z% ]# y104.二、全局
8 W x8 j# s2 I% |105.在src下(WEB-INF/class/)下 xwork-conversion.properties % I1 E8 s( N ]) `
106.java.util.Date=包名.类名 6 s. [! x: W. Z* Z9 {* e: M
107. , i; i$ f" k' N
108.10.向request、session、application中添加属性 : M8 q& U6 { ^* t, V
109.ActionContext act=ActionContext.getCOntext(); 2 o4 ^: R. f, t8 O9 U, V9 w6 {" V
110.act.getApplication.put("app","应用范围"); / ]% r0 x7 p. e! J- l
111.act.getSession.put("ses","session应用范围"); " |& o9 Y5 I4 K4 z
112.act.put("req","request范围") 3 n" F0 k7 D0 u+ h: Q' m3 N) U
113. ( N8 S' m# C. k& K/ F; C; ^
114.获取原生的请求对象 6 C, e' u$ M' Q
115.HttpServletRequest request=ServletActionContext.getRequest(); 6 `, N3 k$ i* x" d0 b5 b+ y+ }
116.ServletContext servlet=ServletActionContext.getServletContext(); . I- Z/ D- q& `! L0 c7 x( w8 d
117.servletContext.getRealPath(“”);得到具体文件的绝对路径
2 F) w0 c3 C+ C! u' b8 N, I118.request.getSession().setArrtibute("session","会话范围");
# ]5 h8 `( o8 e: e8 ?: C8 k119.servletContext.setAttribut("app","应用范围"); + Y' P4 `$ u' B( I! y; y- f
120.request.setAttribute("req","request范围"); 5 ~( J0 B4 R' ~, r9 k+ g; a( H
121.页面访问
9 S( a% X" \/ ], _122.${applicationScope.app} ' j/ G+ [/ D3 s2 e
123.${sessionScope.ses}
( l' w4 u, T0 p) A7 j124.${requestScope.req}
) T2 q: d/ v3 H ~125. 1 e* o5 W4 [# o) C, V, X8 G
126.11.struts2上传文件 ; P) g3 k0 X" N1 i
127.第一步:放jar包 commond-fileupload-1.21.art和commons-io-1.3.2.jar 2 u) }+ x7 g. F% U; j- k. H
128.第二步:把form表单的enctype设置为:multipart/form-data 如 + s* h1 u) @; b4 ] J. x* H; @
129.<form enctype="multipart/form-data"
+ v _ D1 j% |% l130.
, C: m+ H6 {8 s; Q; u131.action="${pageContext.request.contextPath/list.action}" method=""> , z: [# V% \& {8 ]7 ^, n/ s
132. <input type="file" name="uploadImage">
/ `8 C, E. x' i* v0 _$ ^$ V/ W# x133. <input type="submit" value="upload"/> 8 f" _7 G4 @& @* H6 G! S9 [
134.</form>
# ?6 O, B8 C4 c2 r. n+ {135.第三步:在action中添加以下属性 命名规则 与file的name相同 # y# Z& X( s2 N; Q# S3 M8 u
136.private File uploadImage;//得到上传文件 - x8 [8 k4 O& i
137.private String uploadImageContentType;//得到上传文件的类型
w- t: R$ C+ Y138.private String uploadImageFileName;//得到上传文件的名称
6 |: D0 f; p3 k139.
# x) Z N: K& f5 @140.String realPath=ServletActionContext.getServletContext().getRealPath $ n5 o) t; R& @$ Y3 n* D
141. 6 j1 t# L* t# L0 h0 v0 `6 \& L2 }
142.("/images");
. m/ U8 v/ ^4 T3 z! r143.File saveFile=new File(new File(realPath),uploadImageFileName); " V/ A {8 ^' I( Y4 p
144.if(!savefile.getParentFile().exists())
0 Y3 [$ s! r9 x145. saveFile.getParentFile().mkdirs();
2 r- w& q$ F: L7 @# e0 }146.FileUtils.copyFile(uploadImage,saveFile); " v" ~5 u+ J: ?! J
147. / u" \# N# a8 Y% E/ m W
148.12.多文件上传 / Z2 R3 q& J9 C; h
149.
; n E0 q1 o% m. ?- a7 a150.将属性类型定义成数组活list类型
! z0 U3 w! y1 a/ T1 g151.private File[] uploadImage;
$ s9 b8 a5 u& @4 D152.private String[] uploadImageContentType; & E/ g7 V3 D, Y( Y$ G
153.private String[] uploadImageFileName;
9 {- V+ `& W9 i8 v154. ) z, @! Y2 |! Q. ]0 P
155.String realPath=ServletActionContext.getServletContext().getRealPath 5 j) j$ H1 \$ i5 G
156. - W; L# H3 D5 T. P+ w) s
157.("/images"); , `, f* _7 W! I
158.if(uploadImage!=null){
) [" P. k) W! K- n* B159.File savedir=new File(new File(realPath)); & h: P3 h: Z& e1 p6 u3 P9 F; p
160.if(!savedir.exists()) 0 p$ h2 b j+ m y' h, J
161. savedir.mkdirs(); . n1 ~* f8 d, E1 I, M
162.for(int index=0;index<uploadImage.length,index++){
8 ^& O/ J$ Z8 Q, m6 b# f! J' _- ^' |163. File saveFile=new File(savedir,uploadImageFileName[index]); 0 t D# z& s; f8 F0 H+ a5 i# p
164. if(!saveFile.getParentFile().exists()) ' m5 M1 R( W: f! v* X8 x7 a/ |; D
165. saveFile.getParentFile().mkdirs(); : I% G8 \6 u( e6 i5 S
166. FileUtils.copyFile(uploadImage[index],saveFile); ; V3 }8 a" F: |7 r
167.} % e. u. Y+ J# x# @
168. 2 w1 F& l8 `5 w! i- Y! D
169.}
, l' A. m1 p5 [, m, K2 u ?170.13.自定义拦截器
) z- j" f) Z+ K/ V4 t* _8 i& }171.一:类实现Interceptor接口 0 y& s! {6 {: b: j; D: R# R
172.在intercept方法中进行操作,invocation.invoke();继续执行该方法 $ F+ L$ A' M3 u, ^
173.二:注册拦截器
+ M1 T7 R. \, I174.在sturts配置中定义:
5 ]: x; `" F m0 W5 w6 h, M175.<interceptors> 1 h, x) R5 U# K5 g6 r- U( E& h
176. <interceptor name="自定义拦截器名" class="类"/> , T" r5 g7 a2 ~8 R1 o4 k* R
177. <interceptor-stack name="栈名">
/ G! M/ v ?5 c+ U: H. j! B178. <interceptor-ref name="defaultStack"/>引用原来的拦截器 9 X. w# ~& c R% h
179. <interceptor-ref name="自定义拦截器名"/>
- _6 Q O' y$ o. p180. </interceptor-stack> 6 u) o, r* V% s/ H( V8 a9 |$ S7 P
181.</interceptors> - g2 s/ s7 w6 T6 K: x/ _
182.<action name="xxx" class="xxxx"> ; [$ X3 N2 s1 F4 E' Q
183. <interceptor-ref name="栈名">
2 Q3 {4 [! r F7 r# o( J184.</action>
K. L5 H' F( U$ _1 w8 s5 J+ D185. ( t. B% [- N C8 B
186.定义一个包下的所有action都使用此拦截器
4 l* W1 w. j/ F187.<default-interceptor-ref name="栈名"/>
1 {, z6 k6 ?4 V6 ^188. 2 P+ l5 _5 p9 y# ? A& n& t5 [
189.14.struts2的校验 / D5 `/ H5 q: l2 f# B
190.第一:手工代码校验 & n( P3 N6 e: u, k( |4 z, Z9 m
191.一:所有方法校验
/ |# @, d& O- @, S192. 1、类继承ActionSupport 再从写validate方法 # C+ a( t N& D- a" e% S
193. 2、在validate中写入自己的验证需要 5 @# s$ O( j" z7 \& y& v: {
194. 3、此方法this.addFieldError("username","用户名不能为空!");用于放置错误信息
J+ S, ?. Q; [# ]3 A! p) v195. ; u: H9 f5 w, n# F, u
196.,正则表达式的验证: - _1 L4 h0 G! c+ ?& T/ Y9 F
197.Pattern.compile(regex).matcher(this.mobile).matches();regex以^开始以$结尾 $ |8 S; N0 B% I+ @
198. , H/ h- i& c+ o! _
199.fieldError包含错误信息则返回input <s:fielderror/>显示错误信息 " }1 _% s* B9 l- w
200.二:指定方法校验 : c! z5 O9 W+ F2 L2 I3 H" O w
交时返回视图
2 D; o4 t+ t' h
7 s5 E4 z% H! K; v |
|