TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
大家都知道springMvc 都有注解配置 实现请求方式比如:
0 w w. V4 P7 W6 L6 a@RequestMapping(value = "/index", method = RequestMethod.GET)- y& Q8 I' j& Q. b. l8 {
这样的话 用户只能发送get请求。, K" G* T/ r9 V! D# p. h3 U2 q/ J
但是struts2 大多是基于配置实现的、注解的方式用的很少、下面通过struts2实现Http只允许POST请求。
. z0 k" ~9 N" o2 k
5 w% g& C0 z( K/ d" M i- R注解 RequestTypeAnnotation:- @Target({ElementType.PARAMETER, ElementType.METHOD})
; }+ T0 v) a/ U, V0 m - @Retention(RetentionPolicy.RUNTIME)
& Y) f$ N0 S4 O- E: S - @Documented + p4 Q1 `0 W- ~* [8 S3 ~
- public @interface RequestTypeAnnotation {1 D4 A' h5 T- j {4 ?
- RequestType value();
6 g6 F/ _' I; o3 W- R I - public enum RequestType {
0 H8 G5 Q- d3 O7 V9 o6 l - /**# l4 p9 f& E8 S4 w3 Z* K+ P5 Z
- * post方式的http请求+ k: S7 H7 p/ i1 j( y3 V! n
- */1 Y% W+ ^7 }1 z K; F
- POST,
F. Y* [% ~# l& z - /**
! F& v; c2 n0 G0 Q0 @4 \* S - * get方式的http请求
7 G! f% d; i8 h# D2 o; m - */9 S: h: Y# C) ], ~8 M; \
- GET
% N u6 e5 ~: p - }7 A \, L% \; f9 x f& `
- }
复制代码 拦截器 RequestTypeInterceptor:- /**
4 q9 j/ L P4 L4 u! } - * 拦截器
1 O! P+ y+ D/ z. U# T - * 创建者 张志朋
5 e* b- e1 ~* n+ b& |; X) I - * 创建时间 2016年1月5日
: G& n3 o3 r3 L - *8 C, s9 Y' e4 [; _
- */
" Q" b: B6 N9 o1 \ - public class RequestTypeInterceptor extends AbstractInterceptor {
& A m& ?7 g) B, l; C. D - private static final long serialVersionUID = -4204585527913002611L;9 T! f J% N! G8 J2 y8 m, U9 \3 _
- ! R" B' v p1 y. ]0 Q3 o- K9 G; v. p
- public String intercept(ActionInvocation invocation) throws Exception {
, L5 E0 E4 W- [4 ~- ~
1 S [" l9 O8 m( C$ U- d5 W; J- Action action = (Action) invocation.getAction();+ C) x* S/ S) A8 ?; E4 ~
- try {
# `8 C5 p! h3 ?1 p) a - Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {});
) J% m* U6 s- _7 I) U/ S) X - Annotation[] annotations = method.getAnnotations();% e1 p8 S j. o! K% I/ o" j, \0 F
- String methodName = ServletActionContext.getRequest().getMethod();
; F [- U; z+ H ~; B' W! X) t! d - for (Annotation annotation : annotations) {
3 i* |! r- X* T1 ]$ {' B" ]+ ^% ]( U - if (annotation instanceof RequestTypeAnnotation) {
" i7 {* z! F% @& p: a- f - RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation;# d% b2 h0 m- _, L: D; B) y
- if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) {
3 ?( R7 L: s0 M1 z" q - return "input";' a, Q! b4 _, F' x2 W
- }/ N" R9 @- u! u7 D$ t
- }* ?8 e* c0 T& R1 h; C
- }
) d6 E2 m( O( ]! k - } catch (Exception e) {
* N( l# U. _$ w( G9 e- X - return "error";
; h% e V: Q P - }) R2 L' i2 c( R% ^. }! N! t/ @
- return invocation.invoke();
3 `- { k1 @! b* w - }2 ]: B K" e$ c( K% d
- }
复制代码 请求 WelcomeAction:- }4 K9 ?$ {+ T+ ^
- /**6 E( X4 a) \8 B( P
- * 测试action! y' I7 y `5 I: T- |- V
- * 创建者 张志朋
8 z& |. e& U1 T - * 创建时间 2016年1月5日# w9 i; o0 s! W5 p
- *
: J$ S4 U, T& J3 a0 b# a - */& N, b9 |+ X4 p* {( t* y. Y
- public class WelcomeAction extends ActionSupport {
; x6 {4 D9 n$ ~7 `& U: l - private static final long serialVersionUID = 752609025281512627L;6 b2 f! o3 O' l k
- private String message;
! r' g F7 C f% T1 u6 v: z - @RequestTypeAnnotation(RequestType.POST)6 a! s5 f, A5 g+ {1 w
- public String execute(){/ a+ k5 h3 |6 z, D7 L" w- C
- message = "只接受post请求";- d5 Y- K$ C( \0 l I" f9 H" @4 e
- return SUCCESS;$ C( V2 ?, J1 M- ~1 k2 F5 @
- }
: k& o: W/ V8 V: h5 k4 a* f - /**: i q' \' U5 x v5 D1 Q
- * @return the message
* I+ }9 M! `: P( B1 J& V' k - */9 a8 F! [, P" p9 K' o* W
- public String getMessage() {4 v# z" X" e) u" B8 V
- return message; I, e% d0 r2 @# C3 g5 D5 q% f
- }
( _- ]$ G; k0 b7 J C5 B - /**
( a/ f& M. X7 k - * @param message the message to set
1 I/ V! {5 Y6 t/ `6 g - */
+ |1 x. ] B, ^) V - public void setMessage(String message) {; R5 V, _( D. d$ @; P7 F) e
- this.message = message;
* A9 X+ o6 C" ]" q& f% \( p/ @ - }
1 l& S: e E4 j$ D* x- D: P% L - }
复制代码 0 k8 o5 U8 t5 {5 m
struts.xml配置:- <struts>
6 B+ n( `# k) i% d - <constant name="struts.devMode" value="false" /> [! n9 [4 E. Z; F% T
- <constant name="struts.i18n.encoding" value="UTF-8" />
; N7 h% s6 ]' ~, h, K* H - <constant name="struts.action.extension" value="action"></constant>: L9 U% Q* d* L2 y3 w$ A4 n A
- <package name="web01" extends="json-default">
+ b7 z$ I& Z- G/ C - <interceptors> z" h2 o* l* ~
- <interceptor name="requestType" class="itstyle.interceptor.RequestTypeInterceptor"></interceptor>
0 V; A' `: d" g5 t - </interceptors>- _4 g5 i3 I; H! q( d% p
- <action name="welcome" class="itstyle.action.WelcomeAction">
7 R: k8 }1 W' H2 |/ [# ^. p - <result name="success" type="json">
, h x4 R! E5 Q8 x% `6 N( k - <param name="root">message</param>" V% P) N0 p2 R: n7 i- r \
- </result>
& R* u' K, T9 r s& P2 D1 D2 [ - <result name="input">/error.jsp</result>1 ]8 c! [7 N9 f* V
- <interceptor-ref name="defaultStack"></interceptor-ref>
0 F( T% e( C7 e - <interceptor-ref name="requestType"></interceptor-ref>
' x! ^$ C3 {& Y$ b- W: _ - </action>
9 g8 z& s5 Q. {4 C* A) s, }! r1 G - </package>2 k0 h& l* M4 b; h4 Z6 |
- </struts>
复制代码
2 p0 r6 e3 ^% ~/ {3 I% C5 Z4 |3 n项目源码下载地址: 注解实现struts2 action 只接受post请求
' Z1 c6 C& p( D
s% C8 A: f9 Z6 J) F b提取码:
. D% E7 i8 W5 I% u/ z8 H8 k4 m ]# L0 }' ~- L2 a
$ E( t, q6 B. d
|
|