Struts2.2.1 零配置实现

    技术2022-05-19  20

     

     

     

     

     

    废话不多讲,直接说步骤

     

    Struts2.2.1为例

    1)  加入Struts2需要的jar文件,如下图

     

     

    红色部分是实现零配置所需要的插件,spring-plugin是与spring集成的插件,其他的是Struts2工作的基本包,别的包用到了再导

     

    2)  Struts.xml配置

     

    红色部分是零配置的实现,需要解释最后两组注释

    <bean        type=”org.apache.struts2.conventionActionNameBuilder” name=”actionNameBuilder” class=”org.apache.struts2.convention.DefaultActionNameBuilder />

    <constant name=”struts.convention.actionNameBuilder” value=”actionNameBuilder” />

    Struts2.2.1里,对于Action命名方式, 举例如下

    Action类名:org.nicebuild.web.MyFirstStrutsAction

    Namespace/

    URL/my-first-struts

    Action类名:org.nicebuild.web.Product.MyProductAction

    Namespace/product

    url/product/my-product

    说明:Struts2.2.1Action类名去掉Action后缀,然后把每个单词全部小写,并默认用  -  号将其连接,但我们习惯于用驼峰方式,所以

     

    这两个配置就是为了改变Action命名方式,做这个配置后,以上二个Action所对应的NamespaceURL如下

    Action类名:org.nicebuild.web.MyFirstStrutsAction

    Namespace/

    URL/myFirstStruts

    Action类名:org.nicebuild.web.Product.MyProductAction

    Namespace/product

    url/product/myProduct

    早在Struts2.0 2.1的版本中,默认是这样命名的,大家也都习惯,就改一下这个配置

     

    strtus.convention.result.path 这个配置是指定ActionResult对应的页面位置,就是全部result的根目录,在零配置以后,Action的跳转规则如下

    Action类名:MyStrutsAction

    successresult.path根目录寻找与action同名的jsp页面,为myStruts.jsp

    input:对应的jsp页面为:myStruts-input.jsp

    如果是其他的result,就在页面主名后加上-resultName就成

     

    这是用了result.path配置后的,不过也不是非得这样写,还可以在Action类里通过@Results @result @Actionannotation去标,这些Annotation的用法很简单,不一一说明,去百度一下就成,配置就这样了,很简单,具体的可以去看Struts2.2.1的文档,里边有详细说明步骤

    文档主页上的Getting Started中,找到Guides

    再接下来的Guides页面上找到 Plugin Developers Guide,下边有Bundled Plugins

    Convention Plugin,这个就是零配置的详细说明,里边的英文也很简单,看得明白。


    最新回复(0)