TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
大家都知道springMvc 都有注解配置 实现请求方式比如:; i0 \7 q' }9 K: K: N5 J
@RequestMapping(value = "/index", method = RequestMethod.GET)) v# c2 ], X8 j/ H! i, f6 ~6 e- p
这样的话 用户只能发送get请求。1 {" W) T0 d! ^! O. a6 h
但是struts2 大多是基于配置实现的、注解的方式用的很少、下面通过struts2实现Http只允许POST请求。
! C. q. x0 f5 d# U- t
3 @$ g U! w; Z+ m注解 RequestTypeAnnotation:- @Target({ElementType.PARAMETER, ElementType.METHOD})
, j- [. u' _+ T - @Retention(RetentionPolicy.RUNTIME) # w1 L% i) d3 q- Q
- @Documented
8 |: i% _5 m% e7 m- ?* ~% l - public @interface RequestTypeAnnotation {1 J! S) L5 X( p# d
- RequestType value();% m6 j1 w: G% p1 v( N
- public enum RequestType {
& K7 B) i1 E/ N( b - /**
' F: Y+ v2 e* f+ x% ? - * post方式的http请求: ^0 \9 Z. w/ E3 M5 z5 E5 `5 l
- */( s- f# y, W! _& t1 F
- POST,3 J, j6 t& P* o1 t: u; M
- /**% p9 V; T5 U: a% q
- * get方式的http请求0 [0 r' a Z5 p* ]
- */
+ s& I2 X6 ~+ V+ f8 B - GET
3 [% z1 A/ c: C" @% U - }. |4 `/ E" A! R4 x
- }
复制代码 拦截器 RequestTypeInterceptor:- /**
- L7 N" u- r4 o# u6 M4 H - * 拦截器0 e; M- ~. G$ ^: k% g2 _) X l
- * 创建者 张志朋
4 `% G+ T9 N \ - * 创建时间 2016年1月5日
: l, [0 W5 u! t; m# a: t5 T* E - *
6 b+ ^: j4 p9 o* o1 B: C( h2 p - */$ A) Q+ k% r; E" a
- public class RequestTypeInterceptor extends AbstractInterceptor {) W" m% |, V" ?
- private static final long serialVersionUID = -4204585527913002611L;
. W% V" h8 h% e h3 u - : O7 j; E: o& I/ }1 G8 x2 U4 W4 a; {
- public String intercept(ActionInvocation invocation) throws Exception {
$ y, h6 T! k8 S/ O
; `6 V1 T8 M6 J9 ?9 |- Action action = (Action) invocation.getAction();
' u+ c4 u6 D2 J - try {1 @, Z/ ^% W3 r; a
- Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {});6 f. d/ |4 m* J& f* Y; p
- Annotation[] annotations = method.getAnnotations();3 v7 Q1 P1 p5 ~' q
- String methodName = ServletActionContext.getRequest().getMethod();; `* D0 I' @% z& U/ ?9 B
- for (Annotation annotation : annotations) {
! T( ]: m6 ?( p3 {0 z - if (annotation instanceof RequestTypeAnnotation) {
* c6 V* S, G- r8 W* P* f( y- F8 N% J7 a - RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation;( E/ _0 B) b* i; Z
- if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) {
' D' e0 m8 @& n4 E; i- x - return "input";
- S* I; C4 h# j( G5 ]7 N1 q - }; C% {. E( p) p* F
- }
( a6 o% W# b# l; q& V j% |* s8 p( ? - }
! w( j+ c( Q- U$ ^ - } catch (Exception e) {9 [' K/ H0 A& ~' i" n
- return "error";& @( R7 o) w% ]
- }- e M, b0 x9 t6 f: t$ a: X0 E
- return invocation.invoke();
) {! c0 C4 A5 h. y' ~/ e0 c - }: [ O) {* S1 q% i. S& H1 a
- }
复制代码 请求 WelcomeAction:$ c' u: d! _6 V: d
- /**7 i* s: S2 W0 I/ F% @
- * 测试action8 N. r& f* S9 |4 q+ f
- * 创建者 张志朋' K6 \3 }1 g8 A
- * 创建时间 2016年1月5日
$ a4 s/ c5 W: O8 ^. Y - */ p: T8 _: ]: h
- */; R* N6 M6 j5 B" v
- public class WelcomeAction extends ActionSupport {
' z1 g3 _4 D( F' O$ |4 L! m - private static final long serialVersionUID = 752609025281512627L;. c' A# U, B1 N- x1 z: w* ]
- private String message;' ] F; t) ~ F* V/ D. X1 s
- @RequestTypeAnnotation(RequestType.POST)
5 u6 p. G; K; \: }0 }# n/ w" L - public String execute(){( V1 Y. \- w4 C# P& ]
- message = "只接受post请求";# f* e2 |2 Q) ?/ ^8 {( c- x% C
- return SUCCESS;9 Q' R0 E+ }/ J$ l- r, l, N0 m
- }) B; |3 |8 Y0 X6 g. W0 r) }, ]
- /**4 P* W% d7 r% P) W* K9 `: x
- * @return the message7 O6 f5 D$ o3 p- W6 D9 _$ C
- */1 v) w5 U. W# Q* b
- public String getMessage() {1 ]2 ]& i8 k6 L5 i5 k
- return message;
2 `- y5 q+ a1 B, i8 P, t O( T - }
* \0 s) @9 A& k( U - /**5 n! q- B: D9 M. A* I
- * @param message the message to set* b0 O8 `! @" ]; W# d
- */
4 b7 m% Z& w9 _ - public void setMessage(String message) {
) [6 d& Y$ N/ R6 X- T, V - this.message = message;
" x% |5 h* O/ o+ G - }, ^& x. S' F1 I' R+ F, r
- }
复制代码 + [, v0 ^2 t a: b ]7 V. d& H
struts.xml配置:- <struts>, o5 F% r9 _! J2 s
- <constant name="struts.devMode" value="false" />8 T- Q# h/ U# B3 p# }( L
- <constant name="struts.i18n.encoding" value="UTF-8" />% S7 B. K N! D6 n
- <constant name="struts.action.extension" value="action"></constant>4 y8 i$ ~" b4 z+ m- ?/ I. J
- <package name="web01" extends="json-default">- `6 e' |) d, n# s3 R
- <interceptors>
' z; N9 P/ j* S8 _" Q - <interceptor name="requestType" class="itstyle.interceptor.RequestTypeInterceptor"></interceptor>
$ h3 I% ~+ V i8 {) P3 s$ d+ j - </interceptors>
, n9 f* }7 r. J: V$ O r - <action name="welcome" class="itstyle.action.WelcomeAction">3 h! N" L% o% e. D8 j1 p
- <result name="success" type="json">
8 h% c5 f; s! u* v( c' o - <param name="root">message</param>
$ ]. B5 L, U/ k6 w/ T* X& i - </result>
6 k( Z& Q& y3 v4 z - <result name="input">/error.jsp</result>8 [5 E/ d! R9 a( C6 Q5 N0 F2 |! b+ V3 e5 D4 j
- <interceptor-ref name="defaultStack"></interceptor-ref>
" K, d( K! N5 I3 U* [# H9 _( l - <interceptor-ref name="requestType"></interceptor-ref>
' z8 n! ^, p: |$ {% t# q - </action>& @: E" i* J, L6 B
- </package>
0 R% D `' L7 ~: x" W6 G% r, z; ` Z7 \ - </struts>
复制代码 + F, [8 C" [* B' q1 O8 a
项目源码下载地址: 注解实现struts2 action 只接受post请求
, t% L% `0 {9 U; z
3 h/ X7 w/ ]# X* [, |! G7 S提取码:5 Y/ ?' r) P, o3 r, _7 e
- [" v: V: O+ a. l9 [& U' Z8 j
|
|