zoukankan      html  css  js  c++  java
  • Struts2 Plugin 试用笔记

    背景:由于项目需要,需要在原有的框架中加入restful风格的元素并且不影响原有的系统使用。原有的框架Controller层使用的是Struts2,因此考虑使用Struts2的REST-Plugin插件。

    准备:

    相关的jar:

    struts2-core-2.3.15.jar

    struts2-rest-plugin-2.3.15.jar

    struts2-convention-plugin-2.3.15.jar

    xstream-1.4.2.jar

    xwork-core-2.3.15.jar

    xmlpull-1.1.3.1.jar

    开始:

    根据官方的文档说明,首先是配置文档struts.xml:

    <constant name="struts.convention.action.suffix" value="Controller"/>

    <constant name="struts.convention.action.mapAllMatches" value="true"/>

    <constant name="struts.convention.default.parent.package" value="rest-default"/>

    <constant name="struts.convention.package.locators" value="controller"/>

    <constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />

    <constant name="struts.mapper.prefixMapping" value="/rest:rest,:struts" />

    <constant name="struts.action.extension" value="action,,json,xml" />

    说明:

    struts.convention.action.suffix  用于指定控制层action的类名结尾的后缀例如OrdersController,ProductsController,当然可以指定其他任何名字。

    struts.convention.action.mapAllMatches  设置即使没有@Action注释,依然创建Action映射。默认值是false。因为Convention-Plugin是约定优于配置的风格,可以不通过注解根据预先的定义就能访问相应Action中的方法

    struts.convention.default.parent.package  固定配置,指定所有action的父包

    struts.convention.package.locators  用于指定扫描action所在的包路径包含的包名,上例中包名可以如下com.controller以及com.controller.corp.actions。根据convention的约定,controller之后的包名为namespace,访问时url需加上相应的包名http://localhost:8080/corp/actions/normal4.action

    ps一旦指定这个属性后后则以前相类似的配置将不再起作用。例如以前注解配置action时在web.xml中配置的

    <init-param>
    <param-name>actionPackages</param-name>
    <param-value>com.action</param-value>
    </init-param>

    将失效。

    struts.mapper.class  可选值:struts,composite,restful,restful2,设置URL解析且映射到ACTION的实现(默认struts)。如果只需要restful的action那么指定rest就好,但是如果需要兼容原有的action那么这里就必须固定org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper

    struts.mapper.prefixMapping  与struts.mapper.class对应指定不同风格action的namespace,这里有点搞,如果前缀为空即这里的:struts(意思是普通风格的action不需要加前缀)那么action不需要指定namespace,但是如果非空例如——/s:struts(所有普通风格的action需要加上/s前缀),那么需要分以下两种情况:

    1,action的包路径正好就在struts.convention.package.locators指定的包下,那么action的namespace可以省略,访问时直接加上prefixMapping相应的前缀即可例如:http://localhost:8080/s/normal.action,http://localhost:8080/rest/orders。

    2,上例中有个包路径是com.controller.corp.actions则这个路径下的action类必须加上namespace,例如/s/corp/actions,不然会显示找不到这个action,访问url:http://localhost:8080/s/corp/actions/normal4.action

    struts.action.extension  显示访问的后缀,这里可以是.actin也可以为空或者xml,json都行

    其次,编写restful风格的action:

    参考struts2的sample包就行:struts2-rest-showcase.war

    还有其他一些需要注意的,下次补充。

    参考:

     

    http://struts.apache.org/release/2.3.x/docs/rest-plugin.html#RESTPlugin-RESTandnonRESTfulURL%2527sTogetherConfiguration

    http://struts.apache.org/release/2.3.x/docs/convention-plugin.html#ConventionPlugin-ConvertingaCodebehindbasedapplicationtoConvention

  • 相关阅读:
    爬取校花网的视频
    实现爬取图片
    MongoDB简介和安装
    WebSocket实现简易聊天室
    WEB service基础
    Flask之CBV,flash,Flask-Session,WTForms,DBUtils
    Flask之session,路由,配置,蓝图, before/after_request
    Flask之request,response,Jinja2
    Linux下的docker
    Linux下docker配置镜像加速后重启docker服务失败
  • 原文地址:https://www.cnblogs.com/youneverdie/p/3173241.html
Copyright © 2011-2022 走看看