convention-plugin 可以用来实现 struts2 的零配置。
零配置的意思并不是说没有配置,而是通过约定大于配置的方式,大量通过约定来调度页面的跳转而使得配置大大减少。
考虑到某种因素,这里采用 myeclipse 作为示例 IDE,环境 :

JDK 1.6 myeclipse 8.6.1 struts 2.1.8

web.xml

  <filter>       <filter-name>struts2</filter-name>       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>   </filter>   <filter-mapping>       <filter-name>struts2</filter-name>       <url-pattern>/*</url-pattern>   </filter-mapping>

struts.xml

<struts>
  <constant name="struts.devMode" value="true"/>                                                                     <!-- 开发模式 -->   <constant name="struts.i18n.encoding" value="UTF-8"/>                                                       <!-- Web运用编码 -->   <constant name="struts.convention.result.path" value="/view/" />                                  <!-- 搜索视图资源的路径 -->   <constant name="struts.convention.action.name.separator" value="_" />                     <!-- Action类名分隔符 -->   <constant name="struts.convention.classes.reload" value="true" />                                 <!-- convention类重加载 -->   <constant name="struts.convention.action.suffix" value="Action" />                               <!-- Action后缀名 -->   <constant name="struts.action.extension" value="action,do,html,htm,php,aspx" /> <!-- Action扩展名 -->   <constant name="struts.convention.package.locators" value="action,actions" />       <!-- 搜索Action资源的包路径 -->    </struts>

convention 几项重要配置 :

Action 类后缀名 : <constant name="struts.convention.action.suffix" value="Action" />
继承了 struts2 的 ActionSupport 类的类不是一个普通的类,它具有能够处理请求和对请求作出响应的能力,
通常,开发者常常为这种特殊的类起一个后缀名,以区分一般普通的类。例如,xxAction,xxController,
这里的类名的后缀 Action,Controller 就是对应上面配置中的 value 的值,这个值会在发起 URL 请求的时候被截去。例如 : TestAction ---> test

Action类扩展名 [多项以逗号隔开]  :   <constant name="struts.action.extension" value="action,do,html,htm,php,aspx" /> 与文件的扩展名相似的,例如 : TestAction ---> test.action 或 test.do 或 test.html 或 test.php 或 test.aspx 或 ...
注意 : 若该项被配置,则,访问所有的 Action 都需带上 Action 的扩展名,否则客户端将抛出 404 ERROR

Action类名分隔符 : <constant name="struts.convention.action.name.separator" value="_" />
若 Action 类名中出现驼峰标识的串,则用分隔符来切割串,如 HelloWorldAction ---> hello_world

搜索 Action 资源的包路径 [多项以逗号隔开] : <constant name="struts.convention.package.locators" value="action,actions" /> 只有当包名含有以上配置中 value 值中至少一项时,convention plugin 才能搜索找得到该 Action,
否则就算访问的 Action 是存在的,convention plugin 也无法找得到该 Action
注意 : 若包名不是以上列出现过的串结束,则后面的串相当于该包下所有 Action 访问的命名空间 ( 以下面的 LoginAction 作为示例 )。

搜索视图资源(JSP,freemarker,等)的路径 : <constant name="struts.convention.result.path" value="/view/" />
所有的视图资源都需放在配置指定的文件夹路径下,否则,就算结果视图是真实存在的,convention plugin 也无法找得到该视图。默认值是 /WEB-INF/content/

demo 结构图 :

convention 约定 :
1. [ Action 不存在的情况 ] 若 convention plugin 在包搜索路径中搜索不到与请求的 URL 相匹配的 Action,则会到视图的搜索路径下搜索
与请求的 URL 相匹配的视图资源,若还搜索不到,则抛出 no Action mapped Exception 
示例 : 
view/hello_world.jsp [ 没有与之匹配的 Action 类 ]

<html>  <body>   <h2>Hello World!!</h2>  </body> </html>


2. [ Action 的 execute 方法或动态方法调用的情况 ] 如请求 name!method.action,若 NameAction 存在,且 NameAction 中存在 method 这样一个方法,
则根据 method 方法返回的字符串,结果的视图资源名称规则 ( 视图以 JSP 文件为例 ) :
① success  ----->  name.jsp 或 name_success.jsp ; 若这两个视图同时存在,则 convention plugin 会选择 name_success.jsp 作为视图来展示
② 非 success 的任意串 string  ----->  name_string.jsp
示例 :

package net.yeah.fancydeepin.action;
import com.opensymphony.xwork2.ActionSupport; /**  * -----------------------------------------  * @描述  TODO  * @作者  fancy  * @邮箱  fancydeepin@yeah.net  * @日期  2012-10-26 <BR>  * -----------------------------------------  */ public class SayHelloAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    private String message;          public String execute(){                  message = "Hello struts2 convention plugin!";         return SUCCESS;     }          public String say(){                  message = "SayHelloAction, say";         return "world";     }          public String sayHello(){                  message = "SayHelloAction, sayHello";         return "conventionPlugin";     }
    public String getMessage() {         return message;     }      }

view/say_hello.jsp

<html>  <body>   <h2>say_hello.jsp &rarr; Message : ${message}</h2>  </body> </html>

view/say_hello_world.jsp

<html>  <body>   <h2>say_hello_world.jsp &rarr; Message : ${message}</h2>  </body> </html>

view/say_hello_conventionPlugin.jsp

<html>  <body>   <h2>say_hello_conventionPlugin.jsp &rarr; Message : ${message}</h2>  </body> </html>




convention 注解 :
@ParentPackage : 对应于 struts.xml 常量配置项的 <constant name="struts.convention.default.parent.package" value="xx"/> 默认值是 convention-default
@ResultPath : 对应于 struts.xml 常量配置项的 <constant name="struts.convention.result.path" value="xx" /> 默认值是 /WEB-INF/content/
@Namespace : Action 访问的命名空间,该注解一旦声明,结果的视图资源需放在 : 视图搜索目录/命名空间 ( 如 : view/simple/demo.jsp )

package net.yeah.fancydeepin.action;
import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.ResultPath; import com.opensymphony.xwork2.ActionSupport; /**  * -----------------------------------------  * @描述  TODO  * @作者  fancy  * @邮箱  fancydeepin@yeah.net  * @日期  2012-10-26 <BR>  * -----------------------------------------  */ @ParentPackage("convention-default") @Namespace("/simple") @ResultPath("/view/") public class DemoAction extends ActionSupport{
    private static final long serialVersionUID = 1L;     private String message;          public String execute(){                  message = "DemoAction";         return SUCCESS;     }
    public String getMessage() {         return message;     } }

view/simple/demo.jsp

<html>  <body>   <h2>demo.jsp &rarr; Hello World ${message}!</h2>  </body> </html>


@Action

package net.yeah.fancydeepin.action;
import org.apache.struts2.convention.annotation.Action; import com.opensymphony.xwork2.ActionSupport; /**  * -----------------------------------------  * @描述  TODO  * @作者  fancy  * @邮箱  fancydeepin@yeah.net  * @日期  2012-10-26 <BR>  * -----------------------------------------  */ public class ActionannotationAction extends ActionSupport{
    private static final long serialVersionUID = 1L;     private String message;          @Action("invoke")     public String invokeMethod(){                  message = "ActionannotationAction, invokeMethod";         return SUCCESS;     }
    public String getMessage() {         return message;     } }

view/invoke.jsp

<html>  <body>   <h2>invoke.jsp &rarr; Message : ${message}</h2>  </body> </html>


@Result,@Results

package net.yeah.fancydeepin.action;
import java.util.Random; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; /**  * -----------------------------------------  * @描述  TODO  * @作者  fancy  * @邮箱  fancydeepin@yeah.net  * @日期  2012-10-26 <BR>  * -----------------------------------------  */ @Results({@Result(name = "success", location = "result.jsp")}) public class ResultAction extends ActionSupport{
    private static final long serialVersionUID = 1L;     private String message;          public String execute(){                  message = "ResultAction, execute";         return SUCCESS;     }          @Action(value = "doIt",          results = {             @Result(name = "isTrue", location = "result_true.jsp"),             @Result(name = "isFalse", location = "result_false.jsp")         }     )     public String doExecute(){
        message = "doExecute isFalse.";         if(new Random().nextBoolean()){             message = "doExecute isTrue.";             return "isTrue";         }         return "isFalse";     }
    public String getMessage() {         return message;     } }

view/result.jsp

<html>  <body>   <h2>result.jsp &rarr; Message : ${message}</h2>  </body> </html>

view/result_true.jsp

<html>  <body>   <h2>result_true.jsp &rarr; Message : ${message}</h2>  </body> </html>

view/result_false.jsp

<html>  <body>   <h2>result_false.jsp &rarr; Message : ${message}</h2>  </body> </html>



The last example

package net.yeah.fancydeepin.action.admin;
import com.opensymphony.xwork2.ActionSupport; /**  * -----------------------------------------  * @描述  TODO  * @作者  fancy  * @邮箱  fancydeepin@yeah.net  * @日期  2012-10-26 <BR>  * -----------------------------------------  */ public class LoginAction extends ActionSupport{
    private static final long serialVersionUID = 1L;     private String username;     private String password;
    public String log(){                  username = username.intern();         password = password.intern();         if(username == "admin" && password == "fancy"){             return SUCCESS;         }         return ERROR;     }
    public void setUsername(String username) {         this.username = username;     }
    public void setPassword(String password) {         this.password = password;     } }

view/admin/login_success.jsp

<html>  <body>   <h2>Welcome!!</h2>  </body> </html>

view/admin/login_error.jsp

<html>  <body>   <h2>Login Failed!!</h2>  </body> </html>


文章转自:http://www.blogjava.net/fancydeepin/archive/2012/10/26/struts2_convention_plugin.html

Struts2 Convention Plugin ( struts2 零配置 )的更多相关文章

  1. struts2采用convention-plugin实现零配置

    最近开始关注struts2的新特性,从这个版本开始,Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置. 配置文件精简了,的确是简便 ...

  2. Convention插件 struts零配置

    http://blog.csdn.net/spyjava/article/details/13631961系列课程使用 注解:http://www.yiibai.com/struts_2/struts ...

  3. 菜鸟学Struts2——零配置(Convention )

    又是周末,继续Struts2的学习,之前学习了,Struts的原理,Actions以及Results,今天对对Struts的Convention Plugin进行学习,如下图: Struts Conv ...

  4. 从struts2.1开始Convention零配置

    从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该 ...

  5. struts2 Convention插件零配置,使用注解开发

    从struts21开始,struts2不再推荐使用codebehind作为零配置插件,而是改用Convention插件来支持零配置.与以前相比较,Convention插件更彻底. 使用Conventi ...

  6. Struts2 注解零配置方法(convention插件使用)

    最近接触到一个新的项目,是做一个使用S2SH的电子商务商城的二次开发.之前使用过S2SH,在此之前的项目中,Struts2 使用的是XML配置而这个项目是使用注解.在这个项目中,注解还不需要使用Act ...

  7. spring+hibernate+struts2零配置整合

    说句实话,很久都没使用SSH开发项目了,但是出于各种原因,再次记录一下整合方式,纯注解零配置. 一.前期准备工作 gradle配置文件: group 'com.bdqn.lyrk.ssh.study' ...

  8. 13、零配置Struts2开发

    Convention 插件 从 Struts 2.1 开始, Struts 可以使用 Convention 插件来支持零配置: Convention 插件完全抛弃配置信息, 不仅不需要使用 strut ...

  9. Struts2零配置介绍(约定访问)

    从struts2.1开始,struts2 引入了Convention插件来支持零配置,使用约定无需struts.xml或者Annotation配置 需要 如下四个JAR包 插件会自动搜索如下类 act ...

随机推荐

  1. 计算机系统结构总结_Memory Hierarchy and Memory Performance

    Textbook: <计算机组成与设计——硬件/软件接口>  HI <计算机体系结构——量化研究方法>       QR 这是youtube上一个非常好的memory syst ...

  2. oracle PL/SQL编程基础知识

    在oracle中使用pl/sql来扩展SQL的功能,使得SQL能够更加的灵活,功能更加强大,效率更高.pl/sql让sql也能执行判断,循环等操作.主要记录一下pl/sql的基本语法和基本条件判断语句 ...

  3. [七月挑选]写个定时任务,从github下载代码到阿里ECS服务器上

    title: 写个定时任务,从github下载代码到阿里ECS服务器上 根据前几篇博客中能自己创建一个博客,并在github.io上访问到自己的博客,但是如果自己有服务器,那怎么能定时获取github ...

  4. egon消失的一天,空虚寂寞冷,苑模块的时间

    一.时间模块time python有三种表达时间的形式:时间戳.格式化字符串输出和元组. 时间戳:从1970年1月1日00:00:00开始按秒计算的偏移量,返回值是一个float型. 格式化字符串输出 ...

  5. NLP 中任务及相关概念

    命名实体识别 命名实体识别(Named Entity Recognition,简称NER),又称作"专名识别",是指识别文本中具有特定意义的实体,主要包括人名.地名.机构名.专有名 ...

  6. [ZJOI2006]物流运输(动态规划,最短路)

    [ZJOI2006]物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个 ...

  7. 1121. Damn Single (25)

    "Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are suppo ...

  8. python类和self解析

    在介绍Python的self用法之前,先来介绍下Python中的类和实例……我们知道,面向对象最重要的概念就是类(class)和实例(instance),类是抽象的模板,比如学生这个抽象的事物,可以用 ...

  9. 缓存promise技术不错哦

    1. pageManager.js const promiseList = {}; module.exports = { putData: function(pageName,data){ promi ...

  10. 关于本地使用antd的upload组件上传文件,ngnix报错405的问题

    使用阿里的ui框架antd的upload,会自动请求ngnix上面的一个路径,也就是action所在的位置,一直报错405 not allowed,后来经讨论,统一将action写成一个路径,后端对这 ...