TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
大家都知道springMvc 都有注解配置 实现请求方式比如:1 {' p+ |6 `) B5 V" K
@RequestMapping(value = "/index", method = RequestMethod.GET)
; s: n* d* f8 i0 q: | A这样的话 用户只能发送get请求。0 a$ N3 H( Y9 G3 b8 M
但是struts2 大多是基于配置实现的、注解的方式用的很少、下面通过struts2实现Http只允许POST请求。
9 }: {+ b r) D' }) L( h
5 F2 B, }/ y5 m- Y注解 RequestTypeAnnotation:- @Target({ElementType.PARAMETER, ElementType.METHOD})
% [1 _! @" d. d) y1 e - @Retention(RetentionPolicy.RUNTIME)
" E& W4 X o0 @4 ~7 g - @Documented
7 n$ C. `, e% Z0 b! W2 {8 \ - public @interface RequestTypeAnnotation {
7 v" r6 o5 U: G4 D. F - RequestType value();
5 ~* u9 f6 q3 f1 i2 D6 N - public enum RequestType {
. `. g1 Y) r" U, F* U - /**
6 i1 p6 g. n+ b ^ - * post方式的http请求
; F- a. R$ C! `. r% V1 y) ` - */
, x9 `* }1 N$ w - POST,
2 A% I& h. L: g" T9 r& x8 H7 i# m0 P - /**6 y7 i; Z* I4 w' c1 U% F
- * get方式的http请求
4 E- \; a7 j" P7 S! J: H; g. ` - */
" L4 N) v% |$ J( m4 {7 u- q - GET3 X, r* X2 ]/ ~1 d$ `
- }
6 s$ F, Z7 I& a! I! R! C - }
复制代码 拦截器 RequestTypeInterceptor:- /**( \5 e* U+ c! H" X1 G/ W+ t* @2 `
- * 拦截器7 ^) E6 I& ~' ]% C
- * 创建者 张志朋% p& U6 R1 W/ P' k: c
- * 创建时间 2016年1月5日
$ g1 H0 W* E' _ - *3 c4 D. d* `, ?; G
- */+ T5 R4 V: V) `) I5 [
- public class RequestTypeInterceptor extends AbstractInterceptor {2 a! N* [; s4 i/ u/ M* W
- private static final long serialVersionUID = -4204585527913002611L;
* H& n o% E: s# w: D, H' S- n, L - 0 h- ?1 G6 k+ r
- public String intercept(ActionInvocation invocation) throws Exception {
8 [8 w, O% x! W+ w
" q) v0 z% u$ m/ `- Action action = (Action) invocation.getAction();
% ~6 o" z/ Z. A, F O* L c0 v7 _. m - try {6 e9 A$ z. U6 M! i+ ^* ]( j
- Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {});. O: b9 |; u; x, I6 D) V* T3 P# G
- Annotation[] annotations = method.getAnnotations();' G5 c( d5 b1 n o
- String methodName = ServletActionContext.getRequest().getMethod();
$ h& T# Q9 A- D* n8 K - for (Annotation annotation : annotations) {0 m, @% L/ a% R/ M8 X! V$ }
- if (annotation instanceof RequestTypeAnnotation) {- o: l: P* d# v" x, V) a% T
- RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation;6 i' U( g- \! u/ z- f- x
- if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) {
( ^" D, D* w6 @( f s# |2 \" `- E/ ? - return "input";
, @: ~; G+ ^7 ~" a5 D. a4 M# } - }
6 i0 L( G6 Z7 i; B% f& x% q2 G - }
, j% h# y& B: B2 B/ l - }/ n) P; p2 ]4 g. t$ \. Y8 o
- } catch (Exception e) {2 E' w/ |, j9 y9 J" C- Q# N6 l0 d
- return "error";
/ `1 T) l# ], V5 R1 q$ H - }
1 ]; h/ e! k- m' A0 E - return invocation.invoke();$ `% B! [, y, I' W9 w/ o7 G
- }
0 ?; `6 K$ Y. q4 L8 s" J% H - }
复制代码 请求 WelcomeAction:9 b* }9 C* L$ T( }8 b+ A
- /**4 U# W' [" g" k+ l" `, w
- * 测试action
; H* A" W' c4 b. Q$ U; s - * 创建者 张志朋
5 P: ]/ y* f' R0 Q - * 创建时间 2016年1月5日; m, i& }) }. s: D9 V+ A
- *2 k; O: A/ Y" p0 L! P
- */
, |6 M+ z$ m4 @+ ?5 ]" V - public class WelcomeAction extends ActionSupport {9 q+ s5 g5 v) c
- private static final long serialVersionUID = 752609025281512627L;, A5 G3 ]7 @& Y, ]& m6 P. n/ t
- private String message;
$ ~+ {) Y8 y7 R- i, O; c7 K - @RequestTypeAnnotation(RequestType.POST)9 M; i3 m, p1 {) W5 r
- public String execute(){3 b* m; G! y! W& O+ z" N1 O* f
- message = "只接受post请求";# X' M) P( b6 T- l. \# g7 X
- return SUCCESS;
( Y' a# E4 X& q5 [! k& b - }4 W. B3 @! t/ ^: @6 i
- /**
+ Z( ^( D; K4 G3 c - * @return the message
6 a) ?, K( Q; @( N2 A# i( G - */
2 n4 ^9 L3 p9 a/ d$ u) K - public String getMessage() {
' k# F$ e' h- T+ J' s3 j$ c - return message;
+ x) o6 Q5 g# W$ N - }
% `7 h2 n) Z; P. H1 q' o - /**
6 k; N: Y$ L3 M; w) C - * @param message the message to set- a. P' P6 N7 w m7 r
- */
0 D" l1 f3 z8 K8 k& G - public void setMessage(String message) {4 g+ V3 u* L, {( t+ b; ~
- this.message = message;' i; d+ Q% c! a; Y1 n. P
- }9 O+ ~' G! n: G3 M0 V
- }
复制代码
3 p' u3 h+ v9 \( s {struts.xml配置:- <struts>3 p( t3 ^% z; L( z( p5 @
- <constant name="struts.devMode" value="false" />
& B' A5 \ V6 Y; s8 q - <constant name="struts.i18n.encoding" value="UTF-8" />
/ { d& z( p" r' ~5 j: B$ F8 n - <constant name="struts.action.extension" value="action"></constant>0 z, v+ E* Q/ p' I( V7 Z
- <package name="web01" extends="json-default">/ z0 C' a' S+ {2 A$ i7 \+ N
- <interceptors>
1 B2 }/ u$ r, L2 \7 [ - <interceptor name="requestType" class="itstyle.interceptor.RequestTypeInterceptor"></interceptor>
' n' _' k) A7 O+ I+ P: D+ Q$ B- V - </interceptors>
9 g; k, z. h/ t4 G: |7 o4 n+ J8 k - <action name="welcome" class="itstyle.action.WelcomeAction">
4 A9 n/ y, F& a7 f - <result name="success" type="json">
( y+ |( @- Y3 L; O - <param name="root">message</param> m4 `% E X0 ] V- g
- </result>
& N( j z# V. y8 N8 p - <result name="input">/error.jsp</result>
" s& s, x6 V2 }- T$ o) a - <interceptor-ref name="defaultStack"></interceptor-ref>
) j6 X( j% g& i0 G' n5 Q4 t/ c* _ - <interceptor-ref name="requestType"></interceptor-ref> 5 |2 }' L8 P8 k# n# k( a
- </action>, k6 \! U+ ?3 O- V" w& ]3 q3 Z
- </package>
$ x( j9 w" H" g- b) L- W - </struts>
复制代码
# J, G5 ~: X, b v$ i项目源码下载地址: 注解实现struts2 action 只接受post请求
5 O4 i- n; L Q6 r6 G+ G3 d9 `% e3 U
提取码:. Z0 c+ f! [5 R' q
# e/ b8 ]9 U4 V' s* `2 b- Z' U8 }7 o4 }0 U* K0 q! Q; j
|
|