Struts2conventionplugin中文文档.docx

上传人:b****2 文档编号:12897399 上传时间:2023-04-22 格式:DOCX 页数:27 大小:26.02KB
下载 相关 举报
Struts2conventionplugin中文文档.docx_第1页
第1页 / 共27页
Struts2conventionplugin中文文档.docx_第2页
第2页 / 共27页
Struts2conventionplugin中文文档.docx_第3页
第3页 / 共27页
Struts2conventionplugin中文文档.docx_第4页
第4页 / 共27页
Struts2conventionplugin中文文档.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

Struts2conventionplugin中文文档.docx

《Struts2conventionplugin中文文档.docx》由会员分享,可在线阅读,更多相关《Struts2conventionplugin中文文档.docx(27页珍藏版)》请在冰豆网上搜索。

Struts2conventionplugin中文文档.docx

Struts2conventionplugin中文文档

Struts2ConventionPlugin中文文档

Introduction

从struts2.1版本开始,ConventionPlugin作为替换替换CodebehindPlugin来实现Struts2的零配置。

•   包命名习惯来指定Action位置

•   命名习惯制定结果(支持JSP,FreeMarker等)路径

•   类名到URL的约定转换

•   包名到命名空间(namespace)的约定转换

•   遵循SEO规范的链接地址(即:

使用my-action来替代MyAction)

•   基于注解的Action名

•   基于注解的拦截机(Interceptor)

•   基于注解的命名空间(Nameespace)

•   基于注解的XWork包

•   默认action以及默认的结果(比如:

/products将会尝试寻找com.example.actions.Products或com.example.actions.products.Index进行处理)

无需配置Convention即可使用Convention,Convention的某些约束习惯可以通过配置属性来控制,您也可以在类中覆写其中的方法来达到扩展目地。

安装

使用Convention插件,你需要将其JAR文件放到你应用的WEB-INF/lib目录中,你也可以在你Macen项目的POM文件中添加下面包依赖

 

1. 

2.    org.apache.struts 

3.    struts2-convention-plugin 

4.    2.1.6 

5. 

转换基于Codebehind项目到Convention

跳转到此页面,查看需要修改的变化和小提示

如果你想在你系统中结合Convention插件使用REST。

需要在你项目的struts.xml中添加如下配置

 

1. 

2. 

3. 

Helloworld

到目前为止,你已经在你项目中添加了Convention插件支持,首先我们从一个非常简单的例子开始入手。

本例中,我们将演示根据访问URL来访问固定的Action,默认情况下,Convention会默认所有的结果页面都存储在WEB-INF/content下,你也可以在struts的properties文件中设定struts.convention.result.path的值到一个新的路径。

路径最后“/”是不必要的,Convention会自动进行处理。

以下是本例的JSP文件

WEB-INF/content/hello-world.jsp

 

1. 

2. 

3.    Hello world!

 

4. 

5.

启动Tomcat或其他你所使用的JEE容器,在浏览器访问http:

//localhost:

8080/hello-world,你可看到以下信息:

Helloworld!

这表明,Convention已经能正常运行,并找到了结果。

即使在没有action存在情况下,convention也会根据URL规则来找到结果页面。

Codebehindhelloworld

我们继续扩展本例并添加代码实现类。

为了实现本功能,首先需要Convention能正确找到我们的Action类,默认情况下,Convention会找到com.opensymphony.xwork2.Action的实现类,或制定包中以Action结尾的类action

Convention使用以下方法来搜索类路径,首先,Convention会从根package中寻找包名含有struts,struts2,actionoractions的任意packages。

下一部,Convention从前一步找到的package以及其子package中寻找com.opensymphony.xwork2.Action的实现以及以Action结尾的类,下面为Convention寻找的类

 

1.com.example.actions.MainAction 

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

3.pany.details.ShowCompanyDetailsAction 

4.com.example.actions.MainAction

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

6.pany.details.ShowCompanyDetailsAction

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

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

 

1.com.example.actions.MainAction -> / 

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

3.pany.details.ShowCompanyDetailsAction -> /company/details

接下来Convention需要确定URL的具体资源部分。

第一步取消类名中的Action,并以”-”来分割类名的其他部分,且将每个分部的首字母转为小写。

如下所示

 

1.com.example.actions.MainAction -> /main 

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

3.pany.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

以下就是action类的实现代码:

 

1.package com.example.actions; 

2.  

3.import com.opensymphony.xwork2.ActionSupport;  

4.  

5.public class HelloWorld extends ActionSupport { 

6.    private String message; 

7.  

8.    public String getMessage() { 

9.        return message; 

10.    } 

11.  

12.    public String execute() { 

13.        message = "Hello World!

"; 

14.        return SUCCESS; 

15.    } 

16.}

编译以上代码,并将其class放到WEB-INF/classes中,Convention将会将/hello-world映射到这个Action.部署上面的类以后,我们在JSP文件中添加打印message的语句,具体代码如下:

 

1. 

2. 

3.    The message is ${message} 

4. 

5. 

启动应用服务器,在浏览器访问http:

//localhost:

8080/hello-world地址,我们看到如下结果界面:

ThemessageisHelloWorld!

原文:

http:

//cwiki.apache.org/WW/convention-plugin.html

翻译:

石太祥

Struts2ConventionPlugin中文文档(二

Resultsandresultcodes

Struts启动后,Convention将预设好应用中的所有的action,默认情况下,配置将包含在你应用中能找到的所有JSP文件。

同时您也可在Action代码中设置与习惯不同的结果页面。

通常Action方法返回一个字符串,通过返回的字符串找到结果页面,而使用Convention允许你在action代码中指定和返回字符串不同的结果页面。

编译下面的例子。

我们希望在action中返回zero而不是success,第一步,我们更新action类,返回zero。

 

1.package com.example.actions; 

2.  

3.import com.opensymphony.xwork2.ActionSupport;  

4.  

5.public class HelloWorld extends ActionSupport { 

6.    private String message; 

7.  

8.    public String getMessage() { 

9.            return message; 

10.    } 

11.  

12.    public String execute() { 

13.        if (System.currentTimeMillis() % 2 == 0) { 

14.            message = "It's 0"; 

15.            return "zero"; 

16.        } 

17.  

18.        message = "It's 1"; 

19.        return SUCCESS; 

20.    } 

21.} 

接下来,我们添加一个新的JSP页面WEB-INF/content/hello-world-zero.jsp。

需要注意的是,文件名的第一部分和action名是对应的,后面的部分和action返回的字符串对应。

这就是convention确定具体使用那个页面来渲染结果。

下面是修改后的JSP代码:

 

1. 

2. 

3.    The error message is ${message} 

4. 

5. 

现在,你可以编辑你的程序,重启应用,刷新页面,根据当前时间不通,会看到不通的渲染结果页面

结果页面的类型会自动匹配文件,支持的渲染页面的格式为:

jsp.ftl,vm,html,htm.下面是actiong和结果模版的映射关系:

URL

Result

Filethatcouldmatch

ResultType

/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

 

Action链

如果在一个action结果中调用另外一个action,他们俩将被链接到一起,如果在第一个action代码中未定义result,如下代码:

 

1.package com.example.actions; 

2.  

3.import com.opensymphony.xwork2.Action; 

4.import com.opensymphony.xwork2.ActionSupport;  

5.  

6.public class HelloAction extends ActionSupport { 

7.    @Action("foo") 

8.    public String foo() { 

9.        return "bar"; 

10.    } 

11.  

12.    @Action("foo-bar") 

13.    public String bar() { 

14.        return SUCCESS; 

15.    } 

16.} 

“foo”action执行时候,由于找不到结果,convention尝试在同一个包下寻找action名为“foo-bar”的action。

如果找到这样的action,convention将会调用并返回相关联的result。

XWorkpackages

为了避免冲突,可将action放在一个自定义XWORK的package下。

package命名由action所在的Java包,action对应的URL中namespace部分以及action的parentXWorkpackage三个部分组成。

parentXWorkpackage值在属性struts.convention.default.parent.package中指定(默认为conventionDefault),package的属性值须继承于strutsDefault

因此,Convention插件中XWORKpackages采用如下命名规则:

 

1.## 

Usingourexamplefromabove,theXWorkpackageforouractionwouldbe:

上例中,action对应的XWORKpackage如下:

 

1.com.example.actions#/#conventionDefault

Struts2ConventionPlugin中文文档(三)

Annotation参考

Convention使用某些注解语句来覆写插件默认的action到url的映射和自动搜索渲染到的页面。

此外,你还可以修改action配置文件中定义的父XWORK的包信息

Actionannotation

Convention插件可以使用Action注解语句来修改action返回的URL地址。

本注解同时也允许包含在Actions语句中,用来使一个action对应于多个URL。

在action方法中使用本注解语句,可以参考以下代码:

 

1.package com.example.actions; 

2.  

3.import com.opensymphony.xwork2.ActionSupport;  

4.import org.apache.struts2.convention.annotation.Action; 

5.  

6.public class HelloWorld extends ActionSupport { 

7.    @Action("/different/url") 

8.    public String execute() { 

9.        return SUCCESS; 

10.    } 

11.} 

现在我们action类中将使用/different/url来替代默认的/hello-world,如果未指定@Result(参考下节),result的路径将会使用action的namespace,上面的例子中将会返回一下路径"/WEB-INF/content/different/url.jsp"。

Action类中的单个方法可以使用Actions注解来映射多个地址。

 

1.package com.example.actions; 

2.  

3.import com.opensymphony.xwork2.ActionSupport;  

4.import org.apache.struts2.convention.annotation.Action; 

5.import org.apache.struts2.convention.annotation.Actions; 

6.  

7.public class HelloWorld extends ActionSupport { 

8.    @Actions({ 

9.        @Action("/different/url"), 

10.        @Action("/another/url") 

11.    }) 

12.    public String execute() { 

13.        return SUCCESS; 

14.    } 

15.} 

另外的Action或Actions的使用方法是,在单个action类中定义多个action方法,每个方法对应一个不同的地址。

下面是多个action方法的范例:

 

1.package com.example.actions; 

2.  

3.import com.opensymphony.xwork2.ActionSupport;  

4.import org.apache.struts2.convention.annotation.Action; 

5.import org.apache.struts2.convention.annotation.Actions; 

6.  

7.public class HelloWorld extends ActionSupport { 

8.    @Action("/different/url") 

9.    public String execute() { 

10.        return SUCCESS; 

11.    } 

12.  

13.    @Action("url") 

14.    public String doSomething() { 

15.        return SUCCESS; 

16.    } 

17.} 

前面的例子中,第二个URL地址是不推荐的,上面url将使用java包名作为namespace,而不会直接使用Action注解的地址。

Interceptor和interceptorstacks同样可以使用interceptorRefs注解来指定。

下例演示了在action中同时添加"validation"和"defaultStack"拦截器。

 

1.package com.example.actions; 

2.  

3.import com.opensymphony.xwork2.ActionSupport;  

4.import org.apache.struts2.convention.annotation.Action; 

5.import org.apache.struts2.convention.annotation.Actions; 

6.  

7.public class HelloWorld extends ActionSupport { 

8.    @Action(interceptorRefs={@InterceptorRef("validation"), @InterceptorRef("defaultStack")}) 

9.    public String execute() { 

10.        return SUCCESS; 

11.    } 

12.  

13.    @Action("url") 

14.    public String doSomething() { 

15.        return SUCCESS; 

16.    } 

17.} 

可以通过params属性来将参数传递给结果。

属性的值是一个偶数个元素的String的数组,由形如{"key0","value0,"key1","value1"..."keyN","valueN"}所组成,举个例子:

 

1.package com.example.actions; 

2.  

3.import com.opensymphony.xwork2.ActionSupport;  

4.import org.apache.struts2.convention.annotation.Action; 

5.import org.apache.struts2.convention.annotation.Actions; 

6.  

7.public class HelloWorld extends ActionSupport { 

8.    @Action(interceptorRefs=@InterceptorRef(value="validation",params={"programmatic", "false", "declarative", "true})) 

9.    public String execute() { 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 材料科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1