读书人

Struts2 之convention-plugin兑现零配

发布时间: 2012-09-23 10:28:10 作者: rapoo

Struts2 之convention-plugin实现零配置 规则全解释


则将路径配置到了WEB-INF/page 下。
2. 默认包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。你可以通过设置struts.convention.package.locators属性来修改这个配置。如:


    则定义了在项目中,包路径包含web和action的将被视为Action存在的路径来进行搜索。
    Com.ustb.web.*/com.ustb.action.*都将被视为含有Action的包路径而被搜索。
    3. 接着,Convention从前一步找到的package以及其子package中寻找 com.opensymphony.xwork2.Action 的实现以及以Action结尾的类:


        还是举个例子:
        UserAction->user UserDetailAction ->user-detail。结合上面的。对于com.ustb.web.user.detail.UserDetailAction,映射的url就是/WEB-INF/content/user/detail/user-detail.jsp
        6. struts支持.jsp .html .htm .vm格式的文件。
        下面是actiong和结果模版的映射关系:

        URLResult
        File that could matchResult Type/hellosuccess/WEB-INF/content/hello.jspDispatcher/hellosuccess/WEB-INF/content/hello-success.htmDispatcher/hellosuccess/WEB-INF/content/hello.ftlFreeMarker/hello-worldinput/WEB-INF/content/hello-world-input.vmVelocity/test1/test2/helloerror/WEB-INF/content/test/test2/hello-error.htmlDispatcher

        ?

        ?

        ?

        ?

        ?

        ?

        ?

        ?

        ?

        ?

        以上的内容来自struts2的文档http://struts.apache.org/2.1.6/docs/convention-plugin.html

        ?


        当然,简单的通过默认的方式来进行配置不能完全满足实际项目的需要。所幸,convention的零配置是非常灵活的。
        通过@Action注释
        对如下例子:

          方法名默认调用路径默认映射路径method1/hello!method1.action ./WEB-INF/content/hello.jspmethod2/hello!method2.action./WEB-INF/content/hello.jsp

          通过@Action注释后

          方法名@Action注释后调用路径@Action注释 后映射路径method1/action1!method1.action./WEB-INF/content/action1.jspmethod1/user/action2!method2.action/WEB-INF/content/user/action2.jsp


          通过@Actions注释

                    package com.example.actions;import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Actions;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;public class HelloWorld extends ActionSupport { @Action(value="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")}) public String method1() { return “error”; }}


                    当我们调用 /hello -world !method1.action 时,返回 /WEB-INF/content/hello-error.jsp
                    当我们调用 /other/bar!method1.action 时,返回 www.baidu.com

读书人网 >编程

热点推荐