我的日常

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 项目源码 > 注解实现struts2 action 只接受post请求
总共48087条微博

动态微博

查看: 7845|回复: 4

注解实现struts2 action 只接受post请求

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

    2021-2-2 11:21
  • 签到天数: 36 天

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2016-01-05 16:22:17 |只看该作者 |倒序浏览
    大家都知道springMvc 都有注解配置 实现请求方式比如:
    % i" t" n: @1 Z& c# j/ d( e$ S@RequestMapping(value = "/index", method = RequestMethod.GET)
    : y% g5 r! A3 D" r9 T5 I7 |0 U% t这样的话 用户只能发送get请求。
    , w% T. L* P7 L8 M4 i' C但是struts2 大多是基于配置实现的、注解的方式用的很少、下面通过struts2实现Http只允许POST请求。
    * L( B+ x4 o! _9 {. K% p6 i4 a# b: I0 x) o& l( m; F/ }
    注解 RequestTypeAnnotation:
    1. @Target({ElementType.PARAMETER, ElementType.METHOD})    $ l0 v4 |7 P! ?4 T2 V5 l
    2. @Retention(RetentionPolicy.RUNTIME)   
      % y% k' W# C( X8 A8 @
    3. @Documented   + ]( ~/ X/ |0 r2 j3 A
    4. public @interface  RequestTypeAnnotation {& b% ~5 K" e7 R+ a
    5.     RequestType value();$ ]; i" `1 d. C$ ^# I. Z
    6.     public enum RequestType {2 H. v0 }: O# Z4 \& h3 W; _
    7.         /**
        Q& g  M: }! F) K, Q
    8.          * post方式的http请求
      & ?/ j. u' v0 _
    9.          */
      7 E4 R+ w  U  {  z6 U* W: @- _
    10.         POST,
      ) T1 i7 x  [& A2 {7 M8 V* v% q! x
    11.         /**6 w. @+ R& Z, Q8 p" `8 M
    12.          * get方式的http请求
      9 `! w1 a$ J8 `8 i9 z
    13.          */
      2 y& M% l  m3 M2 k
    14.         GET, ?/ V& W$ Z' G4 f. g4 P
    15.     }
      " ~+ k2 b/ l' K/ P: f# B
    16. }
    复制代码
    拦截器 RequestTypeInterceptor:
    1. /**  Q' n* `# U& ]6 ?' A
    2. * 拦截器4 w4 |2 V. y1 K. ~
    3. * 创建者    张志朋! T# X  Q! B- u! t. o& |! x
    4. * 创建时间    2016年1月5日
      ! [; x; N& e7 t: c5 X
    5. *8 H# c5 K! u* `
    6. */  u; M) V/ S4 g" m; N0 O" n. ^
    7. public class RequestTypeInterceptor extends AbstractInterceptor  {
      ; L& w8 w( Z& N. V- E9 K
    8.     private static final long serialVersionUID = -4204585527913002611L;! D; o6 ~; J6 E6 n- N- s

    9. % h* m8 |# \/ O' l8 ?  e$ p$ R
    10.     public String intercept(ActionInvocation invocation) throws Exception {8 `8 O# ^' l- k- r& n
    11. % M3 d& o5 t. f& I) G
    12.         Action action = (Action) invocation.getAction();1 X1 a; M" b% B- {' J
    13.         try {
      1 ]) T; j( u; ~& e( H+ c2 ^
    14.             Method method = action.getClass().getMethod(invocation.getProxy().getMethod(), new Class[] {});1 J, {) w2 j) l
    15.             Annotation[] annotations = method.getAnnotations();
      2 [" \# }. j5 S, \. G* z
    16.             String methodName = ServletActionContext.getRequest().getMethod();, G4 ~& A, M9 l" d; K
    17.             for (Annotation annotation : annotations) {+ W1 S8 Z. o/ \" U
    18.                 if (annotation instanceof RequestTypeAnnotation) {
      7 ]" @8 r! |& q6 g( E8 z
    19.                     RequestTypeAnnotation reqTypeAnnotation = (RequestTypeAnnotation) annotation;
      ; q# {6 @1 T* }" |: C8 `" [8 R  q$ T
    20.                     if (!reqTypeAnnotation.value().name().equalsIgnoreCase(methodName)) {0 ]/ E5 R. C" [: f
    21.                         return "input";3 a2 A1 H0 }0 D
    22.                     }  ]+ Y, u  x  k8 _& v2 l, Q
    23.                 }0 ?3 K6 n" f5 u: v: W
    24.             }1 e& `, t! d9 ^6 ^4 Z
    25.         } catch (Exception e) {
      $ R' v8 G4 e% R+ `
    26.             return "error";
      " b; b/ V: c8 P8 X! G9 |
    27.         }/ m$ O0 W# c# n  W& ]% c
    28.         return invocation.invoke();8 B: o* f6 v# C2 V3 \4 l" s
    29.     }
      - b' A, n+ f2 b/ Y( C, }
    30. }
    复制代码
    请求 WelcomeAction:
    9 H# Y, R5 R8 t
    1. /**
        G: u0 b9 N: I8 E4 Q! x
    2. * 测试action
        j( q+ I- K/ u* L
    3. * 创建者    张志朋
      ( i' N# `* @7 ~5 \, k: \
    4. * 创建时间    2016年1月5日
      ' T* N" I4 F: A* K( a6 b; ?3 \
    5. *2 }8 Y. P2 N3 s6 `1 d* Y: L
    6. */9 C5 @# B9 P; h, C) E
    7. public class WelcomeAction extends ActionSupport {- Q% x! ^* T# D" x3 E0 c' z
    8.     private static final long serialVersionUID = 752609025281512627L;
      . T9 g8 `$ Z; M2 _( o' [% Y
    9.     private String message;
      * k8 [" K  ~: I2 x
    10.     @RequestTypeAnnotation(RequestType.POST)2 l- j; M# _9 L& b5 L; W* o
    11.     public String execute(){
      7 }: s6 p1 n$ d- D- Q1 s( S% k
    12.         message = "只接受post请求";
      % g) Q2 P% r) T
    13.         return SUCCESS;
      6 I8 o9 c" r2 x) D$ N; |% p
    14.     }
      1 E: R7 G" K1 ]! D
    15.     /**' i) Z6 z; {( b  y: [$ J: D
    16.      * @return the message% G! e4 ^- C- O: `* G: }
    17.      */
      5 Y. |. P/ {2 W: j- k! y
    18.     public String getMessage() {7 O+ y, w% F: o  ~: T. u
    19.         return message;& X: ?3 w8 E7 C% Z; P/ t/ R, z
    20.     }
      & S# A9 @. _( d$ m/ ]$ P
    21.     /**! c( V: {: B6 Q! O  R' o
    22.      * @param message the message to set
      ( y2 l- I* n! q1 B
    23.      */
      ( S5 z  [* q, ^  j; A
    24.     public void setMessage(String message) {2 Y# `0 f2 m' Z1 Q
    25.         this.message = message;0 M# i& G6 K& R
    26.     }1 o2 u1 J" Y9 O8 S
    27. }
    复制代码

    6 f& ^4 H  P$ L1 A$ W
    struts.xml配置:
    1. <struts>
      " }$ h) ~6 c- o# u6 r5 @
    2.    <constant name="struts.devMode" value="false" />
      ' E9 A- h' x$ D- V3 p; T
    3.    <constant name="struts.i18n.encoding" value="UTF-8" />
      ( s* n4 i; X: s. V5 Z/ W0 c5 S
    4.    <constant name="struts.action.extension" value="action"></constant>& @! U& g3 t) f
    5.    <package name="web01" extends="json-default">2 x/ S+ P; l6 b- a, Z8 Z
    6.        <interceptors>
      7 ^0 I8 _1 \! v! s
    7.             <interceptor name="requestType" class="itstyle.interceptor.RequestTypeInterceptor"></interceptor>
      ! S) A! b5 q& ]% W: `: ~/ e
    8.         </interceptors>
      6 W& V( t) g0 a! x: w
    9.      <action name="welcome" class="itstyle.action.WelcomeAction">, ?) {, D2 @, r5 ?% V: U- C
    10.         <result name="success" type="json">4 R+ |5 M' m) u9 d! M( W
    11.            <param name="root">message</param>
      , i7 w7 O. v7 @$ Z1 F8 }# {
    12.         </result>
      , H7 Q* V- Y% g, |
    13.         <result name="input">/error.jsp</result>0 y/ [$ s/ ]# C# `
    14.         <interceptor-ref name="defaultStack"></interceptor-ref> - I# Y2 X  ]" J* n' _: z
    15.         <interceptor-ref name="requestType"></interceptor-ref>
      - |9 V7 p* V# }/ \! m
    16.      </action>
      " H! d* X6 Q4 m) U
    17.    </package>
      7 {8 c8 x% t! _
    18. </struts>
    复制代码

    : ?7 Q2 b  N3 D) C. n
    项目源码下载地址:  注解实现struts2 action 只接受post请求) R9 [: [: z. S: }# p% f  D" g' I6 c
    2 f9 }$ ?( ?" H& b
    提取码:
    , @0 Q* B! }2 a* E. T; ~+ B
    游客,如果您要查看本帖隐藏内容请回复

    2 K& K* o0 \- Y4 A8 q/ ~
    / c. a6 a5 d; b; R& X: v; a0 ^7 C2 \

    科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与科帮网享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、科帮网管理员和版主有权不事先通知发贴者而删除本文


    JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

    35

    主题

    0

    听众

    526

    金钱

    三袋弟子

    该用户从未签到

    沙发
    发表于 2016-02-27 00:51:54 |只看该作者
    STRUTS 注解 逐渐流行了?
    回复

    使用道具 举报

    admin    

    1244

    主题

    544

    听众

    1万

    金钱

    管理员

  • TA的每日心情

    2021-2-2 11:21
  • 签到天数: 36 天

    [LV.5]常住居民I

    管理员

    板凳
    发表于 2016-02-27 11:29:08 |只看该作者
    相逢在巅峰 发表于 2016-2-26 16:51
    2 s1 g  s- _* k! S, hSTRUTS 注解 逐渐流行了?
    ) \' t* O' ^& W
    配置 着玩的~
    回复

    使用道具 举报

    woniu 实名认证   

    2

    主题

    0

    听众

    330

    金钱

    四袋长老

    该用户从未签到

    地板
    发表于 2016-04-12 12:11:01 |只看该作者
    这个项目太棒勒!下下来学习下!
    回复

    使用道具 举报

    5

    主题

    0

    听众

    316

    金钱

    四袋长老

    该用户从未签到

    5#
    发表于 2017-08-14 08:43:35 |只看该作者
    不错不错不错,学习一下
    回复

    使用道具 举报

    快速回复
    您需要登录后才可以回帖 登录 | 立即注册

       

    关闭

    站长推荐上一条 /1 下一条

    发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
    快速回复 返回顶部 返回列表