TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
大家都知道springMvc 都有注解配置 实现请求方式比如: L. [8 W7 g7 w) t* A
@RequestMapping(value = "/index", method = RequestMethod.GET)
, @; G; `- w4 k7 h9 f% ^* j" E& I这样的话 用户只能发送get请求。
3 z: e, B* D. O' c6 p& h x+ ^6 M$ V但是struts2 大多是基于配置实现的、注解的方式用的很少、下面通过struts2实现Http只允许POST请求。
+ B; ?) V/ H3 p* W6 Q/ f& f0 T7 n8 k
0 ~* M& ^/ J3 `2 o; |注解 RequestTypeAnnotation:- @Target({ElementType.PARAMETER, ElementType.METHOD})
O: `% T* I) R' X9 A d - @Retention(RetentionPolicy.RUNTIME) 4 F/ g% P/ e# F+ f6 d& X3 C
- @Documented ) a, x- g5 [$ w4 u0 S/ [9 y
- public @interface RequestTypeAnnotation {
e3 A' u) R4 i) i( r/ c9 ~; ? - RequestType value();7 V6 Q. W$ W) V" t, a
- public enum RequestType {0 A0 b* p) T% X5 f: x$ Q7 Q
- /**
7 H. y' K+ i4 n& N0 o - * post方式的http请求4 h) c8 I/ |5 h/ L; V3 z
- */2 G8 v7 G) p3 b+ `& F
- POST,
" ?$ |1 w# E1 X8 Z - /**
/ U* l) N0 M7 L7 T2 _: F- ^ - * get方式的http请求
! Q' U6 L" F6 U! B ~* B$ \7 v - */
4 z; K5 d, M* `: R9 o% y$ C - GET
- G8 Z- O( R( K# h. A8 w3 I. _ - }
( x- C9 t' r' v/ R& G! r - }
复制代码 拦截器 RequestTypeInterceptor:- /**
. r* z* O& ~1 [$ P; U) z& D: ^ - * 拦截器: O! S7 @( v2 f
- * 创建者 张志朋
7 E3 u- h8 Y6 `8 w - * 创建时间 2016年1月5日
+ ~" _( @% M0 e$ [) _' I x& M - *+ A; e4 O4 s+ n3 r3 Q: e2 v$ j0 @
- */
, ?, V, {: w# {9 F( }2 w! y - public class RequestTypeInterceptor extends AbstractInterceptor {
, G. k, V1 J: S* X/ W3 k4 z& T: } - private static final long serialVersionUID = -4204585527913002611L;
- C8 M; X! y- O; r, `( J, }# T - + o. P* V b& {* R. M' U+ `
- public String intercept(ActionInvocation invocation) throws Exception {
( D; ~/ I& x, l, L* G7 R% v) h* h - ! {: p& q5 X1 [$ h
- Action action = (Action) invocation.getAction();" N3 s# s0 h5 y7 }8 A# G
- try {
+ v$ E% k& w# |, ?- K& f - Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {});+ c% ~/ z9 V$ j: M9 A
- Annotation[] annotations = method.getAnnotations(); u1 l) p( ~' @, k' s
- String methodName = ServletActionContext.getRequest().getMethod();* ^9 K0 o6 ~7 q; r$ d; o
- for (Annotation annotation : annotations) {
( C) I' N a7 ~2 I7 M - if (annotation instanceof RequestTypeAnnotation) {
1 Y0 r5 d+ H$ W* E' d - RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation;, H& u4 J9 J, y- z0 T
- if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) {+ p7 p8 N, F$ e y
- return "input";
" A8 A& C ~* m6 Q - }+ }9 G. U/ \* `1 t
- }8 \0 g B( e) j: ^% Z
- }
, R# ]. u f8 Q5 L( Y, f. K0 x - } catch (Exception e) {/ r+ o# B' Q3 W8 [! ?' a
- return "error"; x* ?6 k# T" c, I
- }- J8 e3 \3 \ G4 H
- return invocation.invoke();
+ g& \; ?& J; U) ~6 b" H - }
+ w- A/ ?( _" O - }
复制代码 请求 WelcomeAction:0 X5 p# R& _: j: [
- /**
" W& D% N, Y# ~4 s# F - * 测试action$ d+ {% p, w* W
- * 创建者 张志朋
. Q, N6 L. y2 b2 _& X% J* e% n - * 创建时间 2016年1月5日" W; |" [2 t2 b) |; L4 t
- *
/ m `) y/ [) W$ W - */
0 s% q0 b* g6 X V7 R - public class WelcomeAction extends ActionSupport {+ B, m/ V/ I3 H+ x
- private static final long serialVersionUID = 752609025281512627L;9 V& v) |, Z4 F$ j+ r( d
- private String message;" }7 l" u3 J* E8 M3 M
- @RequestTypeAnnotation(RequestType.POST)' z4 o9 p2 [1 b& O! _
- public String execute(){. w0 ^( z) K; t1 k1 z9 s7 s
- message = "只接受post请求";2 z5 X+ T0 d6 |# x5 X- ~6 G1 @
- return SUCCESS;
' d1 J/ P! g7 B5 w. s - }
: c3 o6 Z3 K' y- s1 O+ B - /**
; P, ? `# I; M, G - * @return the message% Y+ m* e, [8 V( y/ i
- */+ y C- X) q9 ~3 n* e
- public String getMessage() {
7 x# E% B% \* j0 c9 ?- | - return message;" o# v+ I8 A8 r; M6 c- H
- }
7 r$ N' O, S. j - /**
5 i7 e& l% A# E/ o8 K2 t9 j - * @param message the message to set
* }4 W$ y' S+ ?/ V" m1 v) G - */ m- c5 K9 P l1 m
- public void setMessage(String message) {
; j) H, H) U$ X. A7 T9 B0 [9 S# @ - this.message = message;
0 a. r8 z8 \/ e* Z( e - }2 w k7 B: e. x2 g7 u2 t
- }
复制代码 7 T( W) C5 Z2 [' d1 d4 W+ K' c
struts.xml配置:- <struts>. a/ e9 n6 M1 V( X% p
- <constant name="struts.devMode" value="false" />
" Y! W0 G4 x/ N+ I - <constant name="struts.i18n.encoding" value="UTF-8" />
( }% u% p; j" V& ^# W$ E# f - <constant name="struts.action.extension" value="action"></constant>1 }$ f, a+ F; Q. w" g* G( L' f
- <package name="web01" extends="json-default">
* N; b5 G0 {! n2 N5 [$ W - <interceptors>4 r0 ?: A: a- R
- <interceptor name="requestType" class="itstyle.interceptor.RequestTypeInterceptor"></interceptor> " R' }0 v, p9 }$ q( y
- </interceptors>
- f2 l. e5 X8 I- x - <action name="welcome" class="itstyle.action.WelcomeAction">
- e7 x, ]9 F# J1 m - <result name="success" type="json">
) j2 Q4 ]. _; V8 _9 H% `$ T" n1 }. R9 ` - <param name="root">message</param> ~( j8 \& a# O' B: k; C
- </result># O5 n% s& ~6 @5 E$ r. @1 _. {/ ?/ u
- <result name="input">/error.jsp</result>1 ?- e; F8 O3 Y' E5 |' n: d4 @
- <interceptor-ref name="defaultStack"></interceptor-ref> ) s! E& z; e# d
- <interceptor-ref name="requestType"></interceptor-ref>
6 C2 e7 ~- A I7 X: E0 Y - </action>
% {$ n9 Y) n" U( y/ ?8 V! H( F - </package>9 n p' e4 ~! {3 |8 W
- </struts>
复制代码 5 r+ M0 ^- h, V) C- b: g" U( _
项目源码下载地址: 注解实现struts2 action 只接受post请求* Q/ e6 n- }! N4 A Z' g& W
3 H- |# e I: ^$ I) m# L提取码:
5 j1 w1 N* a$ ~/ ?7 M) Y
! L$ ]: n" c6 D. Z
# U; W! ^8 ^/ f' y0 ^; L C; [1 C |
|