TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
大家都知道springMvc 都有注解配置 实现请求方式比如:
% i" t" n: @1 Z& c# j/ d( e$ S@RequestMapping(value = "/index", method = RequestMethod.GET)
: y% g5 r! A3 D" r9 T5 I7 |0 U% t这样的话 用户只能发送get请求。
, w% T. L* P7 L8 M4 i' C但是struts2 大多是基于配置实现的、注解的方式用的很少、下面通过struts2实现Http只允许POST请求。
* L( B+ x4 o! _9 {. K% p6 i4 a# b: I0 x) o& l( m; F/ }
注解 RequestTypeAnnotation:- @Target({ElementType.PARAMETER, ElementType.METHOD}) $ l0 v4 |7 P! ?4 T2 V5 l
- @Retention(RetentionPolicy.RUNTIME)
% y% k' W# C( X8 A8 @ - @Documented + ]( ~/ X/ |0 r2 j3 A
- public @interface RequestTypeAnnotation {& b% ~5 K" e7 R+ a
- RequestType value();$ ]; i" `1 d. C$ ^# I. Z
- public enum RequestType {2 H. v0 }: O# Z4 \& h3 W; _
- /**
Q& g M: }! F) K, Q - * post方式的http请求
& ?/ j. u' v0 _ - */
7 E4 R+ w U { z6 U* W: @- _ - POST,
) T1 i7 x [& A2 {7 M8 V* v% q! x - /**6 w. @+ R& Z, Q8 p" `8 M
- * get方式的http请求
9 `! w1 a$ J8 `8 i9 z - */
2 y& M% l m3 M2 k - GET, ?/ V& W$ Z' G4 f. g4 P
- }
" ~+ k2 b/ l' K/ P: f# B - }
复制代码 拦截器 RequestTypeInterceptor:- /** Q' n* `# U& ]6 ?' A
- * 拦截器4 w4 |2 V. y1 K. ~
- * 创建者 张志朋! T# X Q! B- u! t. o& |! x
- * 创建时间 2016年1月5日
! [; x; N& e7 t: c5 X - *8 H# c5 K! u* `
- */ u; M) V/ S4 g" m; N0 O" n. ^
- public class RequestTypeInterceptor extends AbstractInterceptor {
; L& w8 w( Z& N. V- E9 K - private static final long serialVersionUID = -4204585527913002611L;! D; o6 ~; J6 E6 n- N- s
% h* m8 |# \/ O' l8 ? e$ p$ R- public String intercept(ActionInvocation invocation) throws Exception {8 `8 O# ^' l- k- r& n
- % M3 d& o5 t. f& I) G
- Action action = (Action) invocation.getAction();1 X1 a; M" b% B- {' J
- try {
1 ]) T; j( u; ~& e( H+ c2 ^ - Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {});1 J, {) w2 j) l
- Annotation[] annotations = method.getAnnotations();
2 [" \# }. j5 S, \. G* z - String methodName = ServletActionContext.getRequest().getMethod();, G4 ~& A, M9 l" d; K
- for (Annotation annotation : annotations) {+ W1 S8 Z. o/ \" U
- if (annotation instanceof RequestTypeAnnotation) {
7 ]" @8 r! |& q6 g( E8 z - RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation;
; q# {6 @1 T* }" |: C8 `" [8 R q$ T - if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) {0 ]/ E5 R. C" [: f
- return "input";3 a2 A1 H0 }0 D
- } ]+ Y, u x k8 _& v2 l, Q
- }0 ?3 K6 n" f5 u: v: W
- }1 e& `, t! d9 ^6 ^4 Z
- } catch (Exception e) {
$ R' v8 G4 e% R+ ` - return "error";
" b; b/ V: c8 P8 X! G9 | - }/ m$ O0 W# c# n W& ]% c
- return invocation.invoke();8 B: o* f6 v# C2 V3 \4 l" s
- }
- b' A, n+ f2 b/ Y( C, } - }
复制代码 请求 WelcomeAction:
9 H# Y, R5 R8 t- /**
G: u0 b9 N: I8 E4 Q! x - * 测试action
j( q+ I- K/ u* L - * 创建者 张志朋
( i' N# `* @7 ~5 \, k: \ - * 创建时间 2016年1月5日
' T* N" I4 F: A* K( a6 b; ?3 \ - *2 }8 Y. P2 N3 s6 `1 d* Y: L
- */9 C5 @# B9 P; h, C) E
- public class WelcomeAction extends ActionSupport {- Q% x! ^* T# D" x3 E0 c' z
- private static final long serialVersionUID = 752609025281512627L;
. T9 g8 `$ Z; M2 _( o' [% Y - private String message;
* k8 [" K ~: I2 x - @RequestTypeAnnotation(RequestType.POST)2 l- j; M# _9 L& b5 L; W* o
- public String execute(){
7 }: s6 p1 n$ d- D- Q1 s( S% k - message = "只接受post请求";
% g) Q2 P% r) T - return SUCCESS;
6 I8 o9 c" r2 x) D$ N; |% p - }
1 E: R7 G" K1 ]! D - /**' i) Z6 z; {( b y: [$ J: D
- * @return the message% G! e4 ^- C- O: `* G: }
- */
5 Y. |. P/ {2 W: j- k! y - public String getMessage() {7 O+ y, w% F: o ~: T. u
- return message;& X: ?3 w8 E7 C% Z; P/ t/ R, z
- }
& S# A9 @. _( d$ m/ ]$ P - /**! c( V: {: B6 Q! O R' o
- * @param message the message to set
( y2 l- I* n! q1 B - */
( S5 z [* q, ^ j; A - public void setMessage(String message) {2 Y# `0 f2 m' Z1 Q
- this.message = message;0 M# i& G6 K& R
- }1 o2 u1 J" Y9 O8 S
- }
复制代码
6 f& ^4 H P$ L1 A$ Wstruts.xml配置:- <struts>
" }$ h) ~6 c- o# u6 r5 @ - <constant name="struts.devMode" value="false" />
' E9 A- h' x$ D- V3 p; T - <constant name="struts.i18n.encoding" value="UTF-8" />
( s* n4 i; X: s. V5 Z/ W0 c5 S - <constant name="struts.action.extension" value="action"></constant>& @! U& g3 t) f
- <package name="web01" extends="json-default">2 x/ S+ P; l6 b- a, Z8 Z
- <interceptors>
7 ^0 I8 _1 \! v! s - <interceptor name="requestType" class="itstyle.interceptor.RequestTypeInterceptor"></interceptor>
! S) A! b5 q& ]% W: `: ~/ e - </interceptors>
6 W& V( t) g0 a! x: w - <action name="welcome" class="itstyle.action.WelcomeAction">, ?) {, D2 @, r5 ?% V: U- C
- <result name="success" type="json">4 R+ |5 M' m) u9 d! M( W
- <param name="root">message</param>
, i7 w7 O. v7 @$ Z1 F8 }# { - </result>
, H7 Q* V- Y% g, | - <result name="input">/error.jsp</result>0 y/ [$ s/ ]# C# `
- <interceptor-ref name="defaultStack"></interceptor-ref> - I# Y2 X ]" J* n' _: z
- <interceptor-ref name="requestType"></interceptor-ref>
- |9 V7 p* V# }/ \! m - </action>
" H! d* X6 Q4 m) U - </package>
7 {8 c8 x% t! _ - </struts>
复制代码
: ?7 Q2 b N3 D) C. n项目源码下载地址: 注解实现struts2 action 只接受post请求) R9 [: [: z. S: }# p% f D" g' I6 c
2 f9 }$ ?( ?" H& b
提取码:
, @0 Q* B! }2 a* E. T; ~+ B
2 K& K* o0 \- Y4 A8 q/ ~
/ c. a6 a5 d; b; R& X: v; a0 ^7 C2 \ |
|