zoukankan      html  css  js  c++  java
  • Struts2的零配置和rest插件

    1. 零配置使用struts2-convention-plugin-2.3.16.jar,rest使用struts2-rest-plugin-2.3.16.jar

    1.1 Struts2的convention插件的主要特点是“约定优于配置”,对于Struts2而言,它会自动在你创建的action、actions、struts、struts2这四个包下自动搜索,只要实现了com.opensymphony.xwork2.Action接口的类或者是类名以“Action”结尾的类,Struts2就会认为包里的类是Action类。

    当Struts2按约定找到了这些符合条件的类以后,就会自动部署这些Action,但在不同的包结构下,访问这些Action的URL也是不同的,请看下面的表格举例:

    访问URL
    org.crazyit.actions.LoginAction 映射到/
    com.test.action.abc.UserAction 映射到/abc
    org.crazyit.struts2.wage.hr.AddEmployeeAction 映射到/wage/hr
    org.crazyit.struts.auction.bid.BidAction 映射到/auction/bid

    而访问Action的名字,也应遵循两个规则,第一:如果类名包含Action后缀,那么把Action后缀去掉;第二:将以骆驼命名法的类名转成中画线写法,所有的字母都小写,单词之间用中画线分割。比如:

    类名 映射
    LoginAction /login.action
    GetBooks /get-books.action
    AddEmployeeAction /add-employee.action

    Action处理用户请求之后都会返回一个字符串作为逻辑视图,该逻辑视图必须映射到实际的物理视图。Convention插件默认也为作为逻辑视图和物理视图之间的映射提供了约定。默认情况下,Convention插件总会到WEB-INF/content路径下定位物理资源,定位资源的约定是:actionName+result+suffix.当某个逻辑后视图找不到对应的物理视图时,Convention插件会自动试图使用actionName+suffix作为物理资源。 例如:zj.qiao.actions.user.LoginAction类,返回success字符串时,Convention将优先考虑使用WEB-INF/content/user/login-success.jsp作为视图资源,如果找不到,WEB-INF/content/user/login.jsp也可作为对应的视图资源。

    1.2 REST插件可以让Struts2实现RESTful风格的URL访问资源方式,其实Struts2本质上是一个MVC框架,而REST插件是将原本的URL转换成RESTful风格的URL而已, REST插件中RestActionMapper负责接收参数,把HTTP的请求方式分别用7个方法来做出处理:

    HTTP 方法

    URI

    调用的action方法

    参数

    GET

    /movie

    index

    POST

    /movie

    create

    PUT

    /movie/Thrillers

    update

    id="Thrillers"

    DELETE

    /movie/Thrillers

    destroy

    id="Thrillers"

    GET

    /movie/Thrillers

    show

    id="Thrillers"

    GET

    /movie/Thrillers/edit

    edit

    id="Thrillers"

    GET

    /movie/new

    editNew

    使用了REST插件之后,Action类就不使用execute()方法来处理用户请求了,而是上面的7个方法来实现

    注意:如果浏览器不支持DELETE和PUT操作,需要在表单中多传一个值来模拟这种方式,在你的<form>标记中加入一个隐藏域:<input type="hidden" name="_method" value="DELETE"/>,这样,在提交表单的时候,Struts2就会知道你当前的请求方式是DELETE,而执行destory()方法。更新也是一样的,加入PUT隐藏域就可以了

    2. 默认的约定总是不爽,所以可以在struts.xml中配置convert

     1    <!-- 类名后缀,默认是Action -->
     2     <constant name="struts.convention.action.suffix" value="Controller"/>
    3 <!-- 设置即使没有@Action注释,依然创建Action映射。默认值是false --> 4 <constant name="struts.convention.action.mapAllMatches" value="true"/>
        <!--设置Action的默认继承包--> 5 <constant name="struts.convention.default.parent.package" value="rest-default"/>
    6 <!-- 包名后缀,默认为action、actions、struts、struts2--> 7 <constant name="struts.convention.package.locators" value="controller" />
    8 <!--设置Convention插件是否从其它jar包中搜索Action类,默认值为true--> 9 <constant name="struts.convention.action.disableJarScanning" value="true" />
    10 <!--设置Convention插件文件协议类型--> 11 <constant name="struts.convention.action.fileProtocols" value="jar,wsjar" />
    12 <!--设置Convention插件需要搜索的其它jar包,Convention插件除了扫描默认的action,actions,struts,struts2,还会扫描该常量指定的一个或多个包,Convention会试图从指定包中发现Action类--> 13 <constant name="struts.convention.action.includeJars" value=".*?/struts2-action*.*?jar(!/)?" />
    14 <!--指定其它jar的包作为根包来搜索Action类--> 15 <constant name="struts.convention.action.packages" value="*.*.myaction" />
    16 <!-- 指定视图文件所在的目录地址 --> 17 <constant name="struts.convention.result.path" value="/WEB-INF/content/" />
    18   <!--使用rest映射/rest,struts映射其它的-->    
    19   <constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
    20   <constant name="struts.mapper.prefixMapping" value="/rest:rest,:struts" />
    21   <constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />

    在网上看到许多使用这两个插件发现的问题,在此也记下

    1. 以war部署在weblogic(10.3.3)时找不到action,在war包classes下添加META-INF目录随便放个文件,在struts.xml中添加

    <constant name="struts.convention.action.includeJars" value=".*?/_wl_cls_gen.*?jar(!/)?" />     
    <constant name="struts.convention.exclude.parentClassLoader" value="true" />
    <constant name="struts.convention.action.fileProtocols" value="jar,zip,vfsfile,vfszip" />

    2. c#以post方式调用时各种报错

    c#中如果以post方法请求url时,不论是HttpWebRequest还是WebClient,默认都会添加expect = 100-continue的头信息,org.apache.struts2.rest.RestActionMapper对此特殊处理,去调用createContinue方法,但是如果Controller没有提供createContinue方法,c#调用时就会报错了。解决办法要么客户端

    webReq.ServicePoint.Expect100Continue = false;//禁止自动添加Except:100-continue到http头信息

    要么服务器端Controller总添加createContinue方法

     
  • 相关阅读:
    zlog 使用手册
    Contribution and Coding Style Guide Bullet Physics Library
    Windows系统读写ext2/3/4文件系统的工具「ext2fsd」
    blender svn
    c# 各个版本
    c# Looping with the foreach statement
    C# 9.0 and .NET 5 – Modern Cross-Platform Development
    我的常用博客
    qml通过stacklayout实现页面切换
    JSON解析-android平台(转)
  • 原文地址:https://www.cnblogs.com/yhzh/p/5085428.html
Copyright © 2011-2022 走看看