TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
大家都知道springMvc 都有注解配置 实现请求方式比如:4 g# ~' r6 b/ N/ N
@RequestMapping(value = "/index", method = RequestMethod.GET)
7 x* t& H5 u8 G! f! O这样的话 用户只能发送get请求。, V F1 N+ o" m9 d9 k Y& e
但是struts2 大多是基于配置实现的、注解的方式用的很少、下面通过struts2实现Http只允许POST请求。( y i8 \4 y4 T6 t) n: n
" q3 \" n: C8 Y9 q$ G
注解 RequestTypeAnnotation:- @Target({ElementType.PARAMETER, ElementType.METHOD})
; X' v. v" u& E/ p R - @Retention(RetentionPolicy.RUNTIME) , z3 I' I( J, i# B. T5 c
- @Documented ! m2 }' P2 d6 k9 e, S. N' l8 k
- public @interface RequestTypeAnnotation {
7 z& ~6 @$ s! G: o1 D - RequestType value();
6 g. f- X! ~! r - public enum RequestType {" ]+ R8 K! V! L n+ p. j
- /**
/ [% ]- q3 t; w$ [ - * post方式的http请求: t) `# O2 |& z- ?6 \* g/ S
- */
: C7 J! X0 K5 o/ B5 g; K5 _: E - POST,6 l/ V" T$ N) m( b
- /**$ [5 c" C* I& v/ T
- * get方式的http请求
. @) p' H+ W& T( F* Z/ S3 Y - */' ^3 I! c3 x3 L- ^
- GET
' B% V$ W6 h( W& w$ x6 g; m - }
5 C( H& M: g& m( e* |+ M - }
复制代码 拦截器 RequestTypeInterceptor:- /**2 i5 j _5 J) q0 e) z5 ?
- * 拦截器) L& g9 h. @# M7 W6 d
- * 创建者 张志朋
3 o9 U S" D" Q" A2 ] - * 创建时间 2016年1月5日$ i' h( b2 d7 O3 G$ Q
- *
, Q+ p# z; _* u8 r' w$ C - */
9 D: X9 i0 T- w1 G8 h+ d9 E7 ^9 g - public class RequestTypeInterceptor extends AbstractInterceptor {
6 j- l( H* L4 t - private static final long serialVersionUID = -4204585527913002611L;
. t3 ~/ T1 Z7 a3 |! _
! A o. l1 g, k$ G2 x" j- public String intercept(ActionInvocation invocation) throws Exception {
6 J u+ V- n1 o5 }- `& e. B - 7 ?9 l) G7 P# R M; u0 T/ ]. i
- Action action = (Action) invocation.getAction();; o% w$ K4 V- j" z) _* @. k4 W
- try {
# {. m+ v2 ]' v - Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {});' C l. _; [1 L% g( |4 Z
- Annotation[] annotations = method.getAnnotations();% Z$ l8 B& {3 C
- String methodName = ServletActionContext.getRequest().getMethod();
0 L9 M! g# o% z, C' m1 E: r - for (Annotation annotation : annotations) {
6 {/ z1 ^( y- X: n - if (annotation instanceof RequestTypeAnnotation) {
1 s% o m* _& s0 r1 {5 P: g - RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation;! y1 w# ^/ i$ V/ Y
- if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) {
) `' F: ~5 B7 v) |3 ]+ `4 _- R - return "input";3 i8 e- S$ g/ P1 {; w/ X9 ~: T
- }' d1 ]0 }; V4 T& e
- }/ S, R/ S& D9 n a7 P* S
- }* H0 L4 X c5 g# O/ F2 Q: J
- } catch (Exception e) {
Q" ]5 R% F6 X. @3 P - return "error";8 ^; o8 i4 p! S. |
- }
9 U* a) ?% O3 G9 }+ P - return invocation.invoke();
8 c/ M5 j r9 T' ~2 W - }
% S, A9 ~5 U; a) A- Z1 Z3 \9 \9 r - }
复制代码 请求 WelcomeAction:
0 V; w' T1 b5 `2 t) R- /**6 e! y% P# y0 W6 M6 f
- * 测试action3 R( r' `* y' ^6 C! G
- * 创建者 张志朋
3 v# e: [; @/ c N$ M/ q( M2 P- l m- M - * 创建时间 2016年1月5日, h; E. \1 s$ Z; }- r- O
- *8 F7 |5 S9 P& n- R* U% x! S
- */
+ T$ |7 b" P" {% c& o - public class WelcomeAction extends ActionSupport {4 c2 i0 q% T+ O1 v/ v, g8 x
- private static final long serialVersionUID = 752609025281512627L;
6 j4 R1 o/ ~/ B - private String message;
) s, E- _$ h% Y+ ]& V! Q. j; ` - @RequestTypeAnnotation(RequestType.POST)$ V, ?* f9 w( O
- public String execute(){
/ w% W$ `' V" M2 ]- z - message = "只接受post请求";% a& {6 ]* j# v5 g
- return SUCCESS;
0 `. \- r- L/ p+ M* F! { - }
- Y6 W; K" Q2 h* M, W" o, N1 \ - /**
: a% Q9 }. B* _; z9 D - * @return the message, @% ?/ T+ c' H% I6 A: R
- */
( [- G5 a4 I$ w3 n* f - public String getMessage() {1 y: j& V# E! }2 @* W% [
- return message;
/ Q2 r3 \; Q+ G8 r% k - }" T8 S4 g/ |9 ^! y d" X* D
- /**
% D/ ~( Z1 i) b4 u2 `/ @& r& ` - * @param message the message to set q2 R, ]4 \6 x3 |+ y( v
- */' `8 u; d3 v, z3 i
- public void setMessage(String message) {
5 o' g& T7 m! i! M. |& z - this.message = message;
4 e+ M' B* e _' @ - }
7 d4 _, V- e* }# ?9 M3 ~ - }
复制代码
( A4 I# }- w4 H0 F" wstruts.xml配置:- <struts>1 B+ N# j$ ?" f5 S" u) P
- <constant name="struts.devMode" value="false" />0 X4 ?1 Z! q# e" C) U2 v0 T5 G# Q
- <constant name="struts.i18n.encoding" value="UTF-8" />7 F+ c& Z' i- Q. Q3 [# c/ o
- <constant name="struts.action.extension" value="action"></constant>' ]$ j" G+ P' w& D
- <package name="web01" extends="json-default">
8 }! r3 ]$ O; v5 d- v6 U4 r2 p - <interceptors>& _& ~5 x! F C
- <interceptor name="requestType" class="itstyle.interceptor.RequestTypeInterceptor"></interceptor> 5 e/ ~/ K0 S2 g7 h: m5 @. W5 d2 _9 S
- </interceptors>6 |! U- x$ U) p# w8 |
- <action name="welcome" class="itstyle.action.WelcomeAction">, I6 ~/ X7 j7 s$ a0 M: o
- <result name="success" type="json">
5 C2 O, L* J M& ~6 M5 ] - <param name="root">message</param>
& z5 a5 S$ q# X4 {, w - </result>$ J* |* h# h; O v9 p1 g+ W7 g
- <result name="input">/error.jsp</result>1 f8 Z6 Q" O- ^! t; u
- <interceptor-ref name="defaultStack"></interceptor-ref> 3 p8 s! L: L1 x' f+ P+ i% r( J1 {
- <interceptor-ref name="requestType"></interceptor-ref> # C' `, Y; d9 S X
- </action>
; w3 b: ?: p; i2 n' U - </package>
1 f0 z# m* t" @/ H" a% s - </struts>
复制代码
+ Z9 V' N/ o" L4 |4 N项目源码下载地址: 注解实现struts2 action 只接受post请求2 c; x0 {; L" K* ~0 E" F
7 _( |' f3 ?9 [$ G9 r
提取码:; d1 m' X# n. g; T
* c, W S5 m/ a" e" C0 D6 ~2 M1 Y+ d7 @
|
|