TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
大家都知道springMvc 都有注解配置 实现请求方式比如:
! a* a( s2 ?+ ^$ C T. E1 s@RequestMapping(value = "/index", method = RequestMethod.GET). r9 E7 V; j: f+ r
这样的话 用户只能发送get请求。
9 @; w3 Y: j2 i0 ]' a+ J但是struts2 大多是基于配置实现的、注解的方式用的很少、下面通过struts2实现Http只允许POST请求。
3 @- w5 _) \2 A5 R' b+ v/ i7 k3 m- @, ^" S* R
注解 RequestTypeAnnotation:- @Target({ElementType.PARAMETER, ElementType.METHOD}) / p& H! F w" ^0 o
- @Retention(RetentionPolicy.RUNTIME) 2 G- w: w1 ^; w# e# {. O
- @Documented , c7 c) Q$ r7 d) H0 P. ]% R. c
- public @interface RequestTypeAnnotation {1 O9 f2 Y+ x8 Z; n, {, |& Z% H
- RequestType value();
. O& b/ ~8 P3 g% e - public enum RequestType {5 y% d" ^, L/ ~ a, ~) T
- /**- r6 Z3 ?% ?! i. E* k0 @1 K2 G! `/ }
- * post方式的http请求
/ ? @" c0 [& m5 c - */
! g9 t0 }: v3 Y5 n" w - POST,
1 i: Z) e/ y l+ {0 R; D# K - /**
9 [& u7 P/ y1 P1 ]" K - * get方式的http请求& k& h0 @4 ]! ?& d! F
- */) S* d' h: Y3 @
- GET
" \7 S( d# i0 I4 `, N - }4 D* }6 w5 R6 o" E
- }
复制代码 拦截器 RequestTypeInterceptor:- /**
! ^/ i9 M( \: z, y4 O( B, p9 W3 d - * 拦截器+ e2 ]4 c4 O: F4 p2 O {' s
- * 创建者 张志朋
, a, y0 `7 w8 c0 X( _. Q - * 创建时间 2016年1月5日) c4 @" Q3 }; p
- *
. {- l9 c. {! P; B0 M" U - */
8 u' D6 b( u/ n3 Y0 s9 [ - public class RequestTypeInterceptor extends AbstractInterceptor {
( G- Y! g/ s0 F - private static final long serialVersionUID = -4204585527913002611L;
) @. A- y# p0 L. ]1 b - # x2 X2 o, V) `, f$ B
- public String intercept(ActionInvocation invocation) throws Exception {" y, ]5 [; A- i
- ) Y" ^. z( {1 ^( ]+ m) P
- Action action = (Action) invocation.getAction();! i2 ^- q9 m7 Z! Q
- try {
% _* \. z/ ] d4 l - Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {});- K; Q, m: z/ V7 H& f/ W: z) A
- Annotation[] annotations = method.getAnnotations();
- J q8 W3 D& W$ _' s - String methodName = ServletActionContext.getRequest().getMethod();
% J8 X$ D/ U% a) g/ \ - for (Annotation annotation : annotations) {
7 u! x: v6 J6 u - if (annotation instanceof RequestTypeAnnotation) {7 ~, o/ O% D/ t; A2 r+ K. \, r
- RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation;7 ~- z; T$ ^9 `& b0 Y5 o. a- }, Z; d
- if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) {
) z3 u9 f2 }( S9 [" \! ] - return "input";
s( P$ Y* x7 s: J! Q3 O" ^7 ]# h4 B - }
' ?" \- f* B; m - }
8 C* o7 v5 Y2 Y8 s7 B - }
4 Q% \6 `" `1 P7 ?7 E8 l$ D, e - } catch (Exception e) {1 Z) O% L! g+ S
- return "error"; N, a0 q5 ?8 r6 B
- }. G7 }+ w0 f5 U) N0 P
- return invocation.invoke();4 |! V+ H+ l7 Z
- }' M+ Q5 W# o( n: N3 X7 b0 P0 g
- }
复制代码 请求 WelcomeAction:
+ D# `# f p% |3 e) k- /**/ Z9 I+ O q% T. s A
- * 测试action' y J3 |- x1 d4 A) t+ j# ^: F, E6 ?
- * 创建者 张志朋
, F0 ~; [3 g# ~# d - * 创建时间 2016年1月5日
) B/ d% K8 J1 N' M& G% y - *
$ P/ e$ _% h0 o - */$ w5 R: I3 g7 b+ F# m8 s( Y
- public class WelcomeAction extends ActionSupport {8 h. h9 H C# Y5 @
- private static final long serialVersionUID = 752609025281512627L;5 N) j- Y9 Z& E2 n
- private String message;
; j0 e* ]7 Y; ^( P9 J - @RequestTypeAnnotation(RequestType.POST)3 ~4 e: y. R6 \% X E2 o
- public String execute(){
$ m' `- K5 z. }9 N0 O5 C# T - message = "只接受post请求";
' [& S8 p9 ]3 i1 X - return SUCCESS;
0 U5 X) e S( b5 i - }; a- R4 R3 i0 j1 x% Q0 D# l
- /**
+ n" h {/ G- n - * @return the message- U& s7 a* ]2 ^0 H5 j6 p( ~& T. R
- */
& T7 }& b+ ?+ C( p2 Q& ?9 p1 U - public String getMessage() {
$ E; V7 G* A" j6 o - return message;
. w) h* X7 y5 {: j" ^ - }
! K; M# f8 t/ t: C - /**
s; y* ~# _$ `- |3 F - * @param message the message to set
$ o9 i I( [4 |; z$ Z - */9 p' c% _0 S2 B' o) a
- public void setMessage(String message) {/ O* E2 i3 Y+ G' Z2 _3 U( E
- this.message = message;
4 T, C7 @. r6 l) [4 ?, _8 h2 H6 V - }2 ^4 W; E5 A5 B5 F
- }
复制代码
% F$ Q9 ~ ~. N0 h3 kstruts.xml配置:- <struts>8 J4 L: k0 ~' x
- <constant name="struts.devMode" value="false" />% X5 Y" w* A+ O9 O& r, I: ^0 U
- <constant name="struts.i18n.encoding" value="UTF-8" />
4 [3 r7 V1 @3 D7 a/ c( K - <constant name="struts.action.extension" value="action"></constant>& s" i( X0 \0 `0 E/ f0 ]' g4 B
- <package name="web01" extends="json-default">6 K3 ?0 U* I$ k, A- e# Q
- <interceptors>. w, P& T+ ^& d
- <interceptor name="requestType" class="itstyle.interceptor.RequestTypeInterceptor"></interceptor>
. |% C: o" c! N$ `9 }: K - </interceptors>
9 y9 S S0 @( D1 f' `5 U! {/ s - <action name="welcome" class="itstyle.action.WelcomeAction">+ o0 g: A* |+ `7 [- O. Q
- <result name="success" type="json">
- S3 a1 q1 G9 l' t6 _' n - <param name="root">message</param>
/ Y* Y& T7 M4 z! u - </result>* _1 }! p9 P3 S& d- O
- <result name="input">/error.jsp</result>( B: V1 U4 @1 z
- <interceptor-ref name="defaultStack"></interceptor-ref> & _1 A0 a. C6 D- T& \
- <interceptor-ref name="requestType"></interceptor-ref> 6 O: ~5 t# t% K) j* f
- </action>
. M# R% B' ~+ @1 `4 ` - </package>: O$ V, \- [' W/ G% V" C
- </struts>
复制代码 5 u- G5 D+ W& L! T" n+ \
项目源码下载地址: 注解实现struts2 action 只接受post请求
- C2 o. `. c5 T
- L- m8 V3 U% }7 [提取码:% p" |. I# a8 g2 N7 Y. R9 _
1 X8 B" l1 I0 m+ n
! M1 ~4 _- R) D. o' E4 ?6 E0 b
|
|