struts2: config-browser-plugin 与 convention-plugin 学习

struts2被很多新手诟病的一个地方在于“配置过于复杂”,相信不少初学者因为这个直接改投Spring-MVC了。convention-plugin、 config-browser-plugin这二个插件的出现,很大程度改善了这个囧境。

简言之:convention-plugin采用"约定大于配置”的思想,只要我们遵守约定,完全可以少写配置甚至不写配置;而config-browser-plugin则用于方便的浏览项目中的所有action及其与jsp view的映射。这二个插件结合起来学习,能很方便的搞定struts2中各种复杂的action-view映射需求。

一、config-browser-plugin使用

1 <dependency>
2     <groupId>org.apache.struts</groupId>
3     <artifactId>struts2-config-browser-plugin</artifactId>
4     <version>2.3.16</version>
5 </dependency>

View Code

maven项目的pom.xml中加上这个即可,运行后,浏览 http://localhost:8080/{你的项目名称}/config-browser/ 即可看到当前项目中的所有action 

注:以下内容中,凡有url的地方,项目名称假设为struts2-helloworld

如果跑不起来,检查服务器应用WEB-INF/lib/下是否有struts2-config-browser-plugin-2.3.16.jar 这个文件

 

二、convention-plugin 使用

1 <dependency>
2     <groupId>org.apache.struts</groupId>
3     <artifactId>struts2-convention-plugin</artifactId>
4     <version>2.3.16</version>
5 </dependency>

View Code

pom.xml中加上这个后,可以把struts.xml配置文件给干掉了(或者改个名),部署App,如果启动正常,则表示环境ok。如果提示缺少asm 啥类,检查下面这几个依赖项,是否也加进来了

 1 <dependency>
 2     <groupId>asm</groupId>
 3     <artifactId>asm</artifactId>
 4     <version>3.3.1</version>
 5 </dependency>
 6
 7 <dependency>
 8     <groupId>asm</groupId>
 9     <artifactId>asm</artifactId>
10     <version>3.3.1</version>
11 </dependency>
12
13 <dependency>
14     <groupId>asm</groupId>
15     <artifactId>asm-commons</artifactId>
16     <version>3.3.1</version>
17 </dependency>
18
19 <dependency>
20     <groupId>asm</groupId>
21     <artifactId>asm-tree</artifactId>
22     <version>3.3.1</version>
23 </dependency>

View Code

 

2.1 零action的view

convention-plugin约定所有的jsp view都放在WEB-INF/content目录下,在这个目录下先随便放一个名为"no-action.jsp"的jsp文件,里面随便写点啥

浏览 http://localhost:8080/struts2-helloworld/no-action

即:即使没有对应的Action类,struts2也能按约定正常展现页面。(当然,这只是开胃小菜,真正应用中,除了做一些纯静态的页面原型之外,大部分场景,背后还是要有Action类来支撑的)

 

2.2 常规映射

建一个HelloWorld.action类

 1 package com.cnblogs.yjmyzz.action;
 2
 3 import org.apache.struts2.convention.annotation.Action;
 4 import org.apache.struts2.convention.annotation.Namespace;
 5 import org.apache.struts2.convention.annotation.Result;
 6
 7 @Namespace("/home")
 8 public class HelloWorldAction extends BaseAction {
 9
10     private static final long serialVersionUID = -8827776224243873974L;
11
12     private String message;
13
14     @Action("hello-world")
15     public String execute() throws Exception {
16         return SUCCESS;
17     }
18
19     @Action(value = "say-hi", results = { @Result(name = "success", location = "hello-world.jsp") })
20     public String sayHi() throws Exception {
21         message = "welcome to SSH!";
22         return SUCCESS;
23     }
24
25     public String getMessage() {
26         return message;
27     }
28
29     public void setMessage(String message) {
30         this.message = message;
31     }
32
33 }

View Code

解释一下:

第7行,在整个Action类上使用了@Namespace("/home"),表示整个Action最终浏览的url,是以 http://localhost:8080/{你的项目名称}/home/ 打头

第14行,通过注解@Action("hello-world"),把默认的/home/index.action路径,改成了 /home/hello-world

至于execute方法,返回success后,对应的是哪个jsp文件,这个不用死记,通过config-browser-plugin看下便知

即:execute方法返回input/error/success中的任何一个,都会映射到/WEB-INF/content/home/hello-world.jsp 这个文件上

20行sayHI()方法上的注解有点意思,@Action(value = "say-hi", results = { @Result(name = "success", location = "hello-world.jsp") }),默认情况下,如果不指定location,返回success时,它应该对应 /WEB-INF/content/home/say-hi.jsp这个文件,但是通过location值,变成了hello-world.jsp,即与/home/hello-world共用同一个view.

 

3、拦截器问题

上一篇学习了通过拦截器来处理异常,采用convention插件后,会发现拦截器不起作用(struts.xml中配置了也一样)

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5
 6 <struts>
 7
 8     <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 9     <constant name="struts.devMode" value="false" />
10
11     <package name="default" namespace="/" extends="struts-default">
12
13         <interceptors>
14             <interceptor name="myinterceptor"
15                 class="com.cnblogs.yjmyzz.Interceptor.ExceptionInterceptor">
16             </interceptor>
17
18             <interceptor-stack name="myStack">
19                 <interceptor-ref name="myinterceptor" />
20             </interceptor-stack>
21         </interceptors>
22
23         <default-interceptor-ref name="myStack" />
24         <default-action-ref name="index" />
25
26         <global-results>
27             <result name="error">/WEB-INF/common/error.jsp</result>
28         </global-results>
29
30         <global-exception-mappings>
31             <exception-mapping exception="java.lang.Exception"
32                 result="error" />
33         </global-exception-mappings>
34
35         <action name="index">
36             <result type="redirectAction">
37                 <param name="actionName">hello-world</param>
38                 <param name="namespace">/home</param>
39             </result>
40         </action>
41
42     </package>
43
44 <!-- 因为有Convention-plugin,就不再需要手动写action-view的映射规则了 -->
45 <!--     <include file="struts-home.xml" />
46     <include file="struts-mytatis.xml" />  -->
47
48 </struts>

View Code

原因在于convention-plugin使用后,所有Action不再继承自默认defaultStack对应的package,为了解决这个问题,建议所有Action都继承自一个自定义的BaseAction类,然后在BaseAction上加上@ParentPackage("default"),让所有Action强制继承自struts.xml中定义的default package

 1 package com.cnblogs.yjmyzz.action;
 2
 3 import org.apache.struts2.convention.annotation.ParentPackage;
 4 import org.slf4j.Logger;
 5 import org.slf4j.LoggerFactory;
 6
 7 import com.opensymphony.xwork2.ActionSupport;
 8
 9 @ParentPackage("default")
10 public class BaseAction extends ActionSupport {
11
12     private static final long serialVersionUID = -4320398837758540242L;
13     protected Logger logger = LoggerFactory.getLogger(this.getClass());
14
15 }

View Code

 

基本用法就是这些,如果不清楚Action最终出来的url是啥,或者不清楚某个url最终会对应到哪个jsp文件,无需死记规则,只要善用config-browser-plugin,大多数下不用查阅文档即可快速解决问题。

时间: 2024-08-04 03:06:45

struts2: config-browser-plugin 与 convention-plugin 学习的相关文章

struts2使用Convention Plugin在weblogic上以war包部署时,找不到Action的解决办法

环境: struts 2.3.16.3 + Convention Plugin 2.3.16.3 实现零配置 现象:以文件夹方式部署在weblogic(10.3.3)上时一切正常,换成war包部署,运行时提示找不到Action   解决办法: 1. 检查生成的war包中\WEB-INF\classes\下有无META-INF目录,如果没有,在eclipse里resource\META-INF下随便放一个文件,比如test.xml,这样maven打包生成war包时,才会在classes下创建MET

struts2.1 使用 convention plugin 时配置拦截器问题

问题描述 使用conventionplugin可以不用写action而根据地址栏中写的action名去找相应的页面.我在struts.xml中配置了默认拦截器,我将自己写的拦截器加入到默认拦截器中了,现在的问题是,如果我不写action类而直接利用conventionplugin根据地址栏中的action名查找相应的页面,则拦截器不起作用,如何才能让拦截器起作用而不用再写action类. 解决方案 解决方案二:该回复于2010-05-18 16:24:09被版主删除解决方案三:呵呵,写个过滤器解

JQuery Plugin 1 - Simple Plugin

1. How do you write a plugin in jQuery? You can extend the existing jQuery object by writing either methods or functions. 1) Writing a custom jQuery method jQuery methods are defined by extending the jQuery.fn object with your method name. $.fn.exten

给IBuySpy构建一个PlugIn系统

话说公元2003年12月17日,MSDN Library网站上悄无声息的多了一篇文章,介绍了关于构建一个PlugIn Framework的一些基础知识,于是,有了这篇随笔-      PlugIn,很COOL的特性,下面将演示如何给我们的IBuySpy定制一个Page Start PlugIn,这个PlugIn可以让用户自己来创建PlugIn,嵌入到IBuySpy的Page Start PlugIn里面,在网站页面载入的时候,会执行用户嵌入的PlugIn.      可我们为什么要给IBuySp

2.2.5 使用 plugin

除了 loader 外,plugin 是另一个扩展 webpack 能力的方式.于 loader 专注于处理资源内容的转换不同,plugin 的功能范围更广,更为灵活强大. plugin 的存在是为了实现那些 loader 实现不了或者不适合在 loader 中实现的功能.如: 自动生成项目的 html 页面 (HtmlWebpackPlugin). 向构建过程中注入环境变量(EnvironmentPlugin---内置). 向块(Chunk)的结果文件中添加注释信息(BannerPlugin-

HTML5:Subway Map Visualization jQuery Plugin(示例畫深圳地鐵線路圖)

深圳地鐵羅寶線和蛇口線示例:見上圖. 要求瀏覽器版本:browser does support HTML5 canvas element:Google Chrome V 8+;Mozilla Firefox V 3.6+;Opera V 11+;Apple Safari V 5+;Microsoft Internet Explorer V 9+. javascript:   /* Copyright (c) 2010 Nik Kalyani nik@kalyani.com http://www.

详解 ML2 Core Plugin(I) - 每天5分钟玩转 OpenStack(71)

我们在 Neutron Server 小节学习到 Core Plugin,其功能是维护数据库中 network, subnet 和 port 的状态,并负责调用相应的 agent 在 network provider 上执行相关操作,比如创建 network.上一节也介绍了两个 Core Plugin:linux bridge plugin 和 open vswitch plugin.本节将详细讨论更重要的 ML2 Core Plugin. Moduler Layer 2(ML2)是 Neutr

jsp:plugin

js 执行一个applet或Bean,有可能的话还要下载一个Java插件用于执行它. JSP 语法 <jsp:plugin type="bean | applet" code="classFileName" codebase="classFileDirectoryName" [ name="instanceName" ] [ archive="URIToArchive, ..." ] [ align=

MS CRM 2011 Plugin Unsecure Configuration

在注册CRM插件(plugin)的时候,可以配置unsecure configuration和secure configuration.本文中着重讲解一下Unsecure Configuration的使用. 既然是unsecure的,那么在这里配置的信息,可以被任何人看见并使用.那么如何在plugin中获得unsecure configuration中的字符串呢?我们需要为plugin定义一个构造函数public Plugin(string unsecureConfig) 或者如果你也想使用se

Notepad++ 文本比较插件:Compare plugin

Notepad++ 是一款小巧, 功能强大的编辑器.用起来很方便.   Notepad++ 默认的安装没有 Compare plugin, 需要定制安装.不过也很方便.   在Notepad++的"plugin"菜单中选择"Plugin Manager"->"show Plugin Manager",然后再"Available"选项卡内找到"Compare" plugin,打勾,按Install.