TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
大家都知道springMvc 都有注解配置 实现请求方式比如:6 M. a- i2 D# l0 f4 e
@RequestMapping(value = "/index", method = RequestMethod.GET)& ]' U( D$ n! B$ w4 N
这样的话 用户只能发送get请求。
9 H; y3 n, p9 d* c% K但是struts2 大多是基于配置实现的、注解的方式用的很少、下面通过struts2实现Http只允许POST请求。
: f& O/ w' R; A1 {% d+ k. ]9 S/ B; A. f- B
注解 RequestTypeAnnotation:- @Target({ElementType.PARAMETER, ElementType.METHOD})
+ R" u3 F$ R7 w# ?$ u( _ - @Retention(RetentionPolicy.RUNTIME) 6 {$ X) Q- r9 z3 E$ c: W
- @Documented $ ^1 x6 w, }& q" h+ d, ]
- public @interface RequestTypeAnnotation {
( c1 [% L- J) `+ m - RequestType value();& ?3 a% D0 ~+ G3 P& h
- public enum RequestType {
/ s4 C2 L7 B3 c - /*** H5 r* d! L$ ^
- * post方式的http请求
7 J6 P" ^: o j - */
* @1 I. B3 c n7 W5 S9 t' Z - POST,
' u) o6 W) x* F7 h - /**/ {3 w' q& D+ }6 _% t6 R
- * get方式的http请求
! n. p* u( y, i( e - */
$ z4 U5 l) i! T* A) P - GET2 N& ?( I/ Z! E- v/ Z
- }& |' V+ J8 n- Z8 w! h" T X
- }
复制代码 拦截器 RequestTypeInterceptor:- /**
9 \$ R& u8 i* W6 C% w3 M. f - * 拦截器& |- T) x" ?; K% r
- * 创建者 张志朋1 V- V" V4 s, \3 W3 U: a
- * 创建时间 2016年1月5日3 O* |3 u7 e9 e5 m7 ?
- *- V6 a$ I6 H+ A! p. L% A$ D
- */' @ z4 C9 z, n( T
- public class RequestTypeInterceptor extends AbstractInterceptor {( R( k! E7 y( i3 ?6 c
- private static final long serialVersionUID = -4204585527913002611L;6 x% ^3 w! ^( P. q
' Z. \; \' Z G2 B/ f, A9 p0 b- public String intercept(ActionInvocation invocation) throws Exception {
7 c/ E9 b4 N9 F( d - 5 }. j6 E1 y4 K5 x0 ]* K* N% C$ M
- Action action = (Action) invocation.getAction();' M/ F: s) O6 @0 W4 D
- try {
* G7 U/ R5 X m5 a1 I - Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {});/ w" | j0 E6 d8 x
- Annotation[] annotations = method.getAnnotations();
$ H( S: j! D& ^+ R( d) P - String methodName = ServletActionContext.getRequest().getMethod();0 _2 y$ v5 d- t3 H$ S9 X/ A7 Z7 X" t
- for (Annotation annotation : annotations) {8 U- S* e' R* e
- if (annotation instanceof RequestTypeAnnotation) {
7 H: I2 P# _& r; F9 d9 u6 O - RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation;4 x+ M* L/ X: h2 Q3 d$ w
- if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) {
1 }2 L d+ u- o! H7 F - return "input";9 k1 ^& z3 Y. P) g2 ~
- }1 V# i+ E/ u b! ^3 u4 G
- }
7 Q. X. o" ]$ u+ F! r - }
2 {: G P7 [# p: t) H3 V$ y6 I - } catch (Exception e) {
9 S" c7 m+ n6 O S/ K - return "error";# v. H0 Y- R0 ~0 s9 A: t
- }7 t' \8 P9 O& P7 ~1 j. w2 ^6 n
- return invocation.invoke();% b/ r% g8 H8 n0 i/ A+ Q
- }$ ]& T$ w: C8 J U
- }
复制代码 请求 WelcomeAction:6 g* c6 d; L2 ]/ X; m8 F
- /**
. G0 H) k! c& D2 D1 X - * 测试action
* s$ a8 I2 l& ~5 S - * 创建者 张志朋5 R. L! T+ e9 k0 Q
- * 创建时间 2016年1月5日
8 M0 b4 T# ~( J- Z( @ - *
3 Z5 ~; k0 X9 Z1 n8 ^ - */
" ~, S$ `9 ]# b+ Q) V* s - public class WelcomeAction extends ActionSupport {
1 m2 f& d! L5 U- J( F7 A - private static final long serialVersionUID = 752609025281512627L;& z+ v3 k0 ^' o+ Y! j
- private String message;- a. `' ~' Q5 G
- @RequestTypeAnnotation(RequestType.POST)9 n5 ?4 n& p, E5 W8 t& o
- public String execute(){. l* i; Z4 `- x8 c- m; ]
- message = "只接受post请求";
5 W# J' }. m! k2 ^3 f3 B1 N - return SUCCESS; r# F7 [0 {7 h, T+ ^2 G3 i* C" p
- }) j! o) r w# K4 O0 k/ i/ l
- /**
, s" F* C2 g9 `& ] - * @return the message
) R9 l& }& b" E - */
0 T7 Z) A% _1 c/ j. x - public String getMessage() {
2 u9 R0 m5 p) {; |6 W( \ - return message;4 ^7 I8 M: S. c# p5 g' M
- }
( H# c& i' M1 U6 s9 y. W* F( @ - /**
6 k) h, Q, [2 [9 H( \7 [- ^) Z% w - * @param message the message to set9 A$ ^5 j: R& F+ }
- */* A1 M* d# I+ i/ n2 v1 L
- public void setMessage(String message) {$ _2 K0 F9 Q4 P' x. N: {
- this.message = message;
$ s2 y2 j/ z2 b7 f! j" t. O - }! `5 X6 C6 K+ t+ ?
- }
复制代码 2 K2 D7 ~9 T \% N7 I" U
struts.xml配置:- <struts># Q( @- p! k/ }+ d5 k( S
- <constant name="struts.devMode" value="false" />
9 l3 |' r4 b+ b( k5 B" S* U2 F9 y - <constant name="struts.i18n.encoding" value="UTF-8" />
N G. K$ }! d' M$ _" |3 p8 D - <constant name="struts.action.extension" value="action"></constant>
1 J. ~, d7 a* G* o1 X8 X - <package name="web01" extends="json-default">
* X+ X1 n2 h: ?# S/ Q; z' p - <interceptors>
1 _ M* F) y" O9 }- T - <interceptor name="requestType" class="itstyle.interceptor.RequestTypeInterceptor"></interceptor> 3 h; J' }$ S$ u
- </interceptors>5 S9 Y" a6 L! j {2 E
- <action name="welcome" class="itstyle.action.WelcomeAction">1 C9 N* l: n( D8 H' Z! k' R
- <result name="success" type="json">) i. g( X h+ b: M* t/ M- u
- <param name="root">message</param>, V$ @. F. ~% } F3 F6 f2 |
- </result>
# h. _ T) J: \( I# }* l3 g - <result name="input">/error.jsp</result>
% a7 s4 k1 O8 V - <interceptor-ref name="defaultStack"></interceptor-ref>
! a. y/ K3 V/ Q1 Z - <interceptor-ref name="requestType"></interceptor-ref> . e* h6 B$ ]6 a
- </action>% g: P5 S9 G! {4 f3 \
- </package>
! z% g- u$ A4 L4 }- Z$ M: Y - </struts>
复制代码 ; I% q3 D/ R- x V. i! F( [, j1 L
项目源码下载地址: 注解实现struts2 action 只接受post请求4 C0 T; v) L8 j& u0 {- J
/ i$ Z) p7 u! H8 w$ j6 T* |提取码:' l6 e7 Z: t7 n" z% g" M8 S
6 h9 [( |! [7 r! |
& L& t% r+ b9 f* { |
|