TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
大家都知道springMvc 都有注解配置 实现请求方式比如:" h# I. v; l; @1 a$ L# ]
@RequestMapping(value = "/index", method = RequestMethod.GET)
$ p" S1 V. b1 Q$ D) ]) Y# @这样的话 用户只能发送get请求。
) T6 W1 i# P+ W$ l# C5 y但是struts2 大多是基于配置实现的、注解的方式用的很少、下面通过struts2实现Http只允许POST请求。' |; {. n9 \2 Z0 n
; h6 W: p; x+ x! l6 h1 M* Y注解 RequestTypeAnnotation:- @Target({ElementType.PARAMETER, ElementType.METHOD})
* [% V% f/ }) T+ I - @Retention(RetentionPolicy.RUNTIME) & o7 y! ^- e/ Q" ?6 {: s& a f
- @Documented
8 D; M y9 Q0 o4 |8 \ - public @interface RequestTypeAnnotation {* x Z& Q: A( }4 \ M A" U2 a
- RequestType value();0 G1 x& [7 i G, k
- public enum RequestType {
6 @" u+ V& y; R% I: y& B; } - /**) ~# \0 `/ {2 I0 }& A0 I
- * post方式的http请求5 t4 h& ^5 J u" O" d2 b0 r+ b
- */
9 p$ A+ T# l/ f* {+ R, e9 x8 S - POST," Z% A( A p; r1 [" v' u1 m
- /**
7 E6 D8 s/ r+ L$ }( d8 v9 w. ~ - * get方式的http请求
4 E8 @' r% A$ g. A- I6 y - */
* z/ @- }: C7 S0 X" k/ P - GET% a2 o! g+ ~( r3 w$ R; r
- }
1 U; }- ?8 V9 l a! S9 P2 d - }
复制代码 拦截器 RequestTypeInterceptor:- /**' _8 ~* l$ E6 c& N+ B3 h7 ~
- * 拦截器
# L+ g' r% T7 W/ g - * 创建者 张志朋; ^, ^6 m2 e5 v" H) b2 e
- * 创建时间 2016年1月5日
2 X; O4 Z1 \& K; {; A( z3 y - *
@: e9 \2 C/ T0 ^ - */* Q# t1 T' a, F% D- I. c
- public class RequestTypeInterceptor extends AbstractInterceptor {! t! H+ q* T, s1 G; V1 w# ]
- private static final long serialVersionUID = -4204585527913002611L;
7 G4 @7 A, H0 O; R3 |' I' S$ | - * a {, ]! [7 b( L
- public String intercept(ActionInvocation invocation) throws Exception {- l# O+ W7 k2 V" N& t* m. Q& s
, V2 I3 s! w: O! W3 Z/ _) a1 C, O- Action action = (Action) invocation.getAction();+ b, R& c: @8 o5 a: g8 Q. [
- try {
: D6 Q) T3 ], h; a1 @& \0 ~+ F - Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {});6 q8 H w$ `) B/ G) p5 y
- Annotation[] annotations = method.getAnnotations();; j+ O, V* j+ s# B- o% V! W& M0 u
- String methodName = ServletActionContext.getRequest().getMethod();* m b+ m1 E4 ~! }7 ]' V
- for (Annotation annotation : annotations) {! [' p. M5 K4 s4 \+ L6 v* q
- if (annotation instanceof RequestTypeAnnotation) {, Y+ W7 @ P; P5 R/ }/ e3 L2 Y
- RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation;' {- }( r- `1 K0 `7 f
- if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) {( V# E' K4 c- r3 m6 o& p Y! A
- return "input";
# e' I7 {# |4 Y+ `; e" Z# B - }
, B0 c. B. Q1 Z' f+ z0 ~ - }( O/ c( y/ R1 Z+ ~ j7 f
- }/ b! A6 F M9 R! L) h( n+ P
- } catch (Exception e) {& x r3 N% s9 q# Y% E# l
- return "error";
5 O, D1 x( ~( E7 ~- } - }- C/ B2 L) C8 k, k6 ^- V
- return invocation.invoke();4 a: [9 Y: X; b, M" i' t
- }
# @# T/ y9 `" X3 m7 L* E - }
复制代码 请求 WelcomeAction:2 W( i# h M* m( H" H. l! H8 F
- /**
4 I* [# s; \& y - * 测试action
9 C8 {5 N4 {- z: m* C' N - * 创建者 张志朋3 [. Q( \( v: e3 g0 p" C, [& }. i. v
- * 创建时间 2016年1月5日. v, Z% f0 h0 ^& y9 W# b4 {
- *6 K; _' I8 N+ `8 h
- */
; k9 Y- j; @- {5 i4 r+ h0 q i1 M - public class WelcomeAction extends ActionSupport {: r5 u: {$ x# ?) k7 x9 {
- private static final long serialVersionUID = 752609025281512627L;$ f$ j: B6 A" f8 t" J- o# ^8 Z
- private String message;
$ S3 c2 X5 S, E( w3 s0 ` - @RequestTypeAnnotation(RequestType.POST)% G: b8 g( F N% j6 p+ Q) F
- public String execute(){
/ _, L7 }* g" q( |; Z! S - message = "只接受post请求";
7 ~- m/ i, s' T2 I - return SUCCESS;! Y" s) [% v- b$ N% ]$ }5 G8 t
- }- M+ n# C- T: `, e" H' L: h4 a
- /**
1 |& l2 i I% W8 D - * @return the message8 a6 L7 v- C! t' q# I, B a
- */
7 j0 X4 P u: x H, n - public String getMessage() {6 Z% W# J# M; Z" B9 F
- return message;8 V) H4 k8 y' F) `* \; p* z
- }( S# \: A0 C* @$ Z3 f0 T; ]4 M
- /**2 q3 B* A+ {% L3 d. Q( F. ^* i+ D
- * @param message the message to set
* W! Z& p5 m* Y; U- C3 _ - */8 ]. j, `4 h; m/ ]
- public void setMessage(String message) {3 t% { u% _7 z. Z7 w; e1 ?
- this.message = message;' C& s- d+ R2 V2 r& i9 b
- } d% u* I+ I j
- }
复制代码
, z1 s+ b% u7 G: y$ f6 kstruts.xml配置:- <struts>/ m+ u2 y; r+ r2 k" ^; P
- <constant name="struts.devMode" value="false" />- D- i# i5 v& I* D! f
- <constant name="struts.i18n.encoding" value="UTF-8" />6 A& `. L2 E0 F) j
- <constant name="struts.action.extension" value="action"></constant>0 Y- A [ o3 c+ \! t
- <package name="web01" extends="json-default">1 V% B1 d& U8 @3 x4 @8 t. d) D W
- <interceptors>
& q& i; x" _3 j7 [/ W1 h - <interceptor name="requestType" class="itstyle.interceptor.RequestTypeInterceptor"></interceptor>
7 ]1 J6 m( h& ~; p1 y, m - </interceptors>
& q. i! O+ |& Z; I1 B; x- x; E. P - <action name="welcome" class="itstyle.action.WelcomeAction">% G, Q. r+ o G3 b) P
- <result name="success" type="json">1 |# h C/ Q. d# q( l" ~
- <param name="root">message</param>
4 g) @) H y; @/ Q( `* A - </result>
5 J$ o) H; j8 e% D- V I q5 w - <result name="input">/error.jsp</result>7 ]7 R) G8 c! k
- <interceptor-ref name="defaultStack"></interceptor-ref> ( |; c* l$ d) r0 H/ P
- <interceptor-ref name="requestType"></interceptor-ref>
+ z6 k; e* }% m' G' |$ M3 ^# ` - </action>
" L% ~& V: m# G0 O7 X+ h% E l$ { - </package>/ n8 O6 e% _# @* M- ]
- </struts>
复制代码
u- ^9 ]( Q9 E2 V3 t! Y项目源码下载地址: 注解实现struts2 action 只接受post请求
. Q I* ?& ^" G0 j) ]# p; i- [: n; l: I: b/ l& s$ C$ ?
提取码:
9 i# N5 s' c1 n7 z; {& C' V) T- g- N z7 x8 }
- g7 T, j7 [; F, `! v |
|