Struts2 Convention Plugin ( struts2 零配置 )

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

转载于:https://www.cnblogs.com/Neil223/p/5587535.html

Struts2 Convention Plugin ( struts2 零配置 )相关推荐

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

    http://struts.apache.org/release/2.1.x/docs/convention-plugin.html http://javeye.iteye.com/blog/3587 ...

  2. 简述Struts2 Convention零配置

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

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

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

  4. 【struts2】struts2的零配置

    零配置(zero configuration)的意思是不使用任何配置文件部署struts2应用,如struts.xml.struts.properties等.零配置并不是真的"零配置&quo ...

  5. struts2 零配置

    一.新建一个web项目,命名为:struts2 二.导入strut2所需的jar包 所需jar下载:http://pan.baidu.com/s/1dDxP4Z3 三.配置struts2的启动文件,在 ...

  6. Struts2零配置 Zero Config+CodeBehind

    Zero Config能根据web.xml中配置的actionPackages自动扫描所有Action类,并猜测其NameSpace. 再利用CodeBehind猜测Result指向的jsp,实现了s ...

  7. Struts2零配置属性详解(2)

    2019独角兽企业重金招聘Python工程师标准>>> Struts2 零配置属性详解 一.插件包 直接引入myEclipse里面的struts code 包即可. struts2- ...

  8. struts2+hibernate3.3+spring3.0 实现零配置

    目前对于这三大框架整合开发有很多人有这不同的看法,我在这提供一个自己比较喜欢的设计方式: 下面以搭建一个用户管理实验做为说明: 第一步:创建数据库(只需要建立数据库名字就可以了) 第二步:在myecl ...

  9. Struts2+spring+jdbc 以xml配置形式整合

    今天做作业,练习一下Struts2+spring+jdbc 以xml配置形式整合 整合步骤: 工程结构图: 重要配置文件 web.xml <?xml version="1.0" ...

最新文章

  1. Discuz!常用函数解析(续)
  2. 去掉星空极速,开通ADSL路由
  3. 模拟电视频率可用于超级Wi-Fi
  4. PyQt5 技术篇-设置QTableWidget表格组件默认值实例演示,如何获取QTableWidget表格组件里的值,获取表格的行数和列数
  5. 3d数学基础学习总结
  6. Amazon:大数据分析技能,你满足几条?
  7. [SAP成都] SAP UI5应用的sap-ui-core.js被加载之前,还有哪些js文件被加载了
  8. [听尉迟方侃侃]平台
  9. 【卷积码系列3】(n,k,m)卷积码的维特比译码实现(不使用MATLAB库函数)及性能对比(vitdec函数-代码见CSDN同名资源)
  10. 宁波Uber优步司机奖励政策(12月14日到12月20日)
  11. 日期天数转换c语言程序,C语言 ---计算连个日期之间的天数转换
  12. centos 时区正确,时间不对
  13. iPhone 12s Pro Max外观配置细节曝光:支持120Hz刷新率
  14. linux xargs命令_如何在Linux中使用xargs命令?
  15. Rails + Bootstrap个人博客搭建的完整过程(4)
  16. DataFrame数据转为list,再逐行写入Excel
  17. 概率论与数据统计在分类预测中的原理介绍(信息增益、交叉熵等)
  18. 基于python的数据分析-基于Python的南京二手房数据可视化分析
  19. 2019 10月 月末总结
  20. python 实现串口通信USB转232自闭环、USB转485测试

热门文章

  1. Java socket中关闭IO流后,发生什么事?(以关闭输出流为例)
  2. web前后台数据交互
  3. 如何用Python写一个Package
  4. 日常生活小技巧 -- 网络调试助手
  5. S5PV210开发 -- 通信
  6. PC 远程控制 android手机的方法之一VNC
  7. 【区块链基础知识系列】 第8课 区块链之零知识证明
  8. 锁相环环路滤波器计算公式_锁相环计算方法
  9. irobot扫地机器人 电压_【专利技术分析报告】“iRobot扫地机器人”核心专利
  10. JZOJ 3899. 【NOIP2014模拟】逻辑的连通性