Struts Convention Plugin 流程 (2.1.6+)

首先添加lib:

<dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-config-browser-plugin</artifactId>
          <version>2.3.20</version>
      </dependency>
      <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-convention-plugin</artifactId>
          <version>2.3.20</version>
      </dependency>

访问 http://localhost:8080/conv/config-browser/a.action 可以查看目前映射的所有路径

 

插件会按以下顺序执行:

1.寻找以下包:struts, struts2, action ,actions

任何深度找到的以上包都自动作为根目录

2.然后在包内寻找以下类:实现了com.opensymphony.xwork2.Action或者继承了com.opensymphony.xwork2.ActionSupport,或类名以Action结尾

3.映射为url:

com.example.actions.MainAction -> /main
com.example.actions.products.Display -> /products/display
com.example.struts.company.details.ShowCompanyDetailsAction -> /company/details/show-company-details(全部为小写)

 

比如,image 

会映射成,image image

 

可以跳过某些包:struts.convention.exclude.packages=org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.* (被跳过的包是无法再加入的,即使手动添加)

自动搜索的包名:struts.convention.package.locators=action,actions,struts,struts2

自动搜索的包名开头:struts.convention.package.locators.basePackage=

自动搜索的类名结尾:struts.convention.action.suffix=Action

手动指定具体包:struts.convention.action.packages=

 

4.默认,所有的Result都到这里去找:WEB-INF/content

如,image ,则可以通过 http://localhost:8080/conv/my-jsp.action 访问到

可通过常量控制:struts.convention.result.path=/WEB-INF/content/

 

 

 

 

 

 

 

根据返回类型和返回字符串,要起不同的名字:

image

 

5.如果找不到这个页面文件,就认为这是个action,其他action可以在方法上添加@Action(“xx”),指明具体路径,这也就是Result Type为chain

image 中,

public class MyJsp extends ActionSupport {
    public String execute() {
return "here";
      }
}
public class HimJsp extends ActionSupport {
      @Action("my-jsp-here") //通过指定为具体路径,实现chain,前提是没有那个页面文件
      public String execute() {
return "yes";
      }
}

 

 

 

 

 

最后会变成,

image

 

可以设置自动重载:

<constant name="struts.devMode" value="true"/>

<constant name="struts.convention.classes.reload" value="true" />

 

struts.convention.action.alwaysMapExecute =false  禁止自动调用execute()

struts.convention.action.disableScanning=true  禁用扫描

struts.convention.action.mapAllMatches =true  没有@Action也自动映射

struts.convention.action.name.lowercase=false  不要变成小写

struts.convention.action.name.separator=_  名称分隔符

struts.convention.action.eagerLoading=true  不使用Spring的时候可以提高性能

posted @ 2014-12-13 20:45  gcg0036  阅读(629)  评论(0编辑  收藏  举报