TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
大家都知道springMvc 都有注解配置 实现请求方式比如:
Q B# I+ g5 G@RequestMapping(value = "/index", method = RequestMethod.GET)
/ Q4 r$ r+ Z3 i+ w2 R+ v, m, T2 R- f这样的话 用户只能发送get请求。
7 \. o) p. C. w3 Q% K9 r9 Z4 O但是struts2 大多是基于配置实现的、注解的方式用的很少、下面通过struts2实现Http只允许POST请求。
2 x3 x; V$ ^$ k8 b; X8 s+ x, _9 h1 p' s" Y6 T# g0 {4 p
注解 RequestTypeAnnotation:- @Target({ElementType.PARAMETER, ElementType.METHOD}) . y& X) e4 n5 k3 J
- @Retention(RetentionPolicy.RUNTIME) 3 Z; G' H5 N5 z2 a
- @Documented . \0 j$ `) \' G& s4 N5 a
- public @interface RequestTypeAnnotation {$ z1 l4 |4 d2 `
- RequestType value();& j3 w/ G, n3 `2 q4 ]
- public enum RequestType {
2 e( T) X" n) H9 h( z - /**) T4 V; C- t, h; p7 d
- * post方式的http请求: \0 L" q7 I$ }3 ]
- */
^! I( z7 f" q" h! Y( y - POST,
9 X( I* W2 b6 l$ e) V5 a0 f' {3 o - /**$ j$ M3 U5 ^6 j
- * get方式的http请求
+ Z3 {- ]" z7 l( r; O P9 F - */
: O$ T2 ?( e4 ^9 o0 d0 L( f C - GET
1 m1 t, Q3 {% E+ h% B5 Z1 p6 I - }
0 B* c9 @; y0 E# s - }
复制代码 拦截器 RequestTypeInterceptor:- /**, R/ L ? m! L5 g" C
- * 拦截器
' V& c y( y/ v: `* P/ q0 _2 r$ T - * 创建者 张志朋 ^9 F$ E) X) S0 w, B
- * 创建时间 2016年1月5日: |& M* z6 c0 ?5 n5 ~$ C
- *# _" F$ w& ~9 L) p
- */* h4 t8 U: c# i* J
- public class RequestTypeInterceptor extends AbstractInterceptor {
! L# _" v% @% Q$ `* c4 P3 e9 G - private static final long serialVersionUID = -4204585527913002611L;: [8 y% c/ s/ F* k
- - K: g! Z! j% v, H5 W5 H. x
- public String intercept(ActionInvocation invocation) throws Exception {
0 z H$ o0 E0 R8 H$ k. X
# q& J |' l6 W7 ^# m8 y0 ?- Action action = (Action) invocation.getAction();
- N1 L1 L* T2 r. V- L - try {: G3 l3 ]" X& \2 j# ^3 t
- Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {});
) V& F) O, Z. {, _8 a - Annotation[] annotations = method.getAnnotations();& n; C" a" I* X+ ]; j7 e
- String methodName = ServletActionContext.getRequest().getMethod();6 x5 A! O" P4 I6 u. v" @
- for (Annotation annotation : annotations) {
/ o& y5 o+ l& l2 W8 v, X. F8 p9 [+ z - if (annotation instanceof RequestTypeAnnotation) {) ^0 B5 t( i- M7 [/ l3 ^5 q
- RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation;& _% w! z# m" ~; T; f
- if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) {' s# B, s9 G* {. m9 |, p' E
- return "input";6 k$ t1 C5 r8 j0 c
- }' U. a1 ?( }$ k( }1 `4 R
- }0 ^$ @+ D+ a; f2 }
- }
& Y) f7 a @( o7 w( D- J- B - } catch (Exception e) {
8 g6 g [2 B: W; M, Z" h6 X - return "error";
+ q* L* Z$ d- p - }0 z3 V. S' Y, Y7 t
- return invocation.invoke();
! M. U* `2 \. D# f# B* u/ j - }
/ _5 E& r0 n+ E$ a6 c% x; z9 E7 j6 I - }
复制代码 请求 WelcomeAction:
" i# {5 U8 G& X# c# U" i- /**9 q/ x5 H& ^ V, S' S/ m3 O" k
- * 测试action
9 H9 u6 Z" K/ |, \0 `" [" \2 f3 ~ - * 创建者 张志朋
% S( S8 n6 q8 x, r- L# \ - * 创建时间 2016年1月5日; r. m" F3 r$ q8 |! g" z0 `
- *
{) L, N6 e5 S+ D: E8 F0 U - */
' g! u+ H% E. |5 n1 t5 k5 ? - public class WelcomeAction extends ActionSupport {; R' {" b8 ~2 t: Z2 X
- private static final long serialVersionUID = 752609025281512627L;# u" u7 y" G# \- K
- private String message;
: ]( k6 ?. i* V' z; \: B - @RequestTypeAnnotation(RequestType.POST)
- ~# v& I" g. W: ~ - public String execute(){3 k* k& ]1 `- D0 }# P, }# J
- message = "只接受post请求";0 ~) O+ z; g) }8 q" B: M& N1 C; C
- return SUCCESS;1 ?4 n0 i5 J# F% H) K
- }7 q/ u- R0 r6 J
- /**
+ f+ C; f- O6 l9 @# x( t - * @return the message
. K5 }6 f& m7 t5 h - */% v) I3 D! \0 l4 X; D$ U0 J2 H
- public String getMessage() {5 ]* K8 s! b, ?- o6 Z4 l
- return message;
) N [' N. u5 X W - }& F5 A( E {+ i
- /**& b, ~! r; I% o j p1 u$ w' n
- * @param message the message to set
/ o1 l( }0 G' E - */
G% i3 j1 p, \/ K - public void setMessage(String message) {4 Z( M. E0 O! }* ]' n4 }& c4 o1 i
- this.message = message;' C1 P* X9 O7 J% `
- }1 M6 ^7 q/ _) y7 J
- }
复制代码 ( T( x( Z" _" \8 C- g' @. h
struts.xml配置:- <struts>+ O0 c4 |( @& H3 ^
- <constant name="struts.devMode" value="false" />0 i( Z2 G, s( {1 e
- <constant name="struts.i18n.encoding" value="UTF-8" />
; K- W8 N2 t0 m% Y - <constant name="struts.action.extension" value="action"></constant>
0 |. e2 X, M. L: ] ? - <package name="web01" extends="json-default">
* w4 K4 Y" r! I8 s+ j7 X8 s - <interceptors>
; s s2 t: z" _. |$ A, N; ^ - <interceptor name="requestType" class="itstyle.interceptor.RequestTypeInterceptor"></interceptor> 3 L5 Q8 o8 i8 Q# Y3 Q4 a- O
- </interceptors>
6 j% q# y5 q Y! m& s - <action name="welcome" class="itstyle.action.WelcomeAction">
& k" Z+ D4 T; ~0 H( H% I! q - <result name="success" type="json">
+ k# F; u# v" J4 K7 d2 A8 |9 G - <param name="root">message</param>7 I- P4 O* W6 {) I0 f
- </result>* Q+ l( L% W% p' W$ I( x2 a; X
- <result name="input">/error.jsp</result>8 [) ~* i7 Y5 O, j$ S% S: g
- <interceptor-ref name="defaultStack"></interceptor-ref>
$ W+ {8 q3 ]+ l - <interceptor-ref name="requestType"></interceptor-ref>
& r' p) O: r& V - </action>
9 i a+ A f k$ W+ R- W! W0 ]. M. | - </package>; Q# Q* r9 ~5 U, Q
- </struts>
复制代码
# ^# I! k6 y' I2 [$ x' f8 B项目源码下载地址: 注解实现struts2 action 只接受post请求' m3 J& U4 M5 K* v7 l7 ^
4 |; l1 w0 q0 Z3 n. s. w0 X
提取码:1 L/ F$ c w$ a$ x
$ K, V f1 q/ ^2 y4 Y4 O* {" i% l- E6 t' w/ E3 L+ [
|
|