Struts2 Convention Plugin 学习

来源:互联网 发布:3d游戏编程入门教程 编辑:程序博客网 时间:2024/05/24 05:27

struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置

 

 

 

 

 Convention使用以下方法来搜索类路径,首先,Convention会从根package中寻找包名含有strutsstruts2action or actions 的任意packages。下一部,Convention从前一步找到的package以及其子package中寻找

下面为Convention寻找的类

Classes

com.example.actions.MainAction

com.example.actions.products.Display (implements com.opensymphony.xwork2.Action)

com.example.struts.company.details.ShowCompanyDetailsAction

每一个被Convention找到action都会对应一个明确的URL地址,URLpackage的名字以及Action类名为基础。

首先Convention从根package以及类所在的package名来确定对应的URL中的路径(namespace),以下就是根据package确定的URL namespace

Namespaces

com.example.actions.MainAction -> /

com.example.actions.products.Display -> /products

com.example.struts.company.details.ShowCompanyDetailsAction -> /company/details

接下来Convention需要确定URL的具体资源部分。第一步取消类名中的Action,并以”-”来分割类名的其他部分,且将每个分部的首字母转为小写。如下所示

Full URLs

com.example.actions.MainAction -> /main

com.example.actions.products.Display -> /products/display

com.example.struts.company.details.ShowCompanyDetailsAction -> /company/details/show-company-details

 

 

你也可以通过配置struts.convention.exclude.packages 来告诉Convention忽略某些包,也可以设置struts.convention.package.locators 用来更改Convention默认的根packages,最后你还可以设置  struts.convention.action.packages. 来让Convention只搜索特定package下的Action

结果页面的类型会自动匹配文件,支持的渲染页面的格式为:jsp.ftl,vm,html,htm.下面是actiong和结果模版的映射关系:

URL

Result

File that could match

Result Type

/hello

success

/WEB-INF/content/hello.jsp

Dispatcher

/hello

success

/WEB-INF/content/hello-success.htm

Dispatcher

/hello

success

/WEB-INF/content/hello.ftl

FreeMarker

/hello-world

input

/WEB-INF/content/hello-world-input.vm

Velocity

/test1/test2/hello

error

/WEB-INF/content/test/test2/hello-error.html

Dispatcher