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使用
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-config-browser-plugin</artifactId>
<version>2.3.16</version>
</dependency>
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 使用
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.16</version>
</dependency>
pom.xml中加上这个后,可以把struts.xml配置文件给干掉了(或者改个名),部署App,如果启动正常,则表示环境ok。如果提示缺少asm 啥类,检查下面这几个依赖项,是否也加进来了
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency> <dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency> <dependency>
<groupId>asm</groupId>
<artifactId>asm-commons</artifactId>
<version>3.3.1</version>
</dependency> <dependency>
<groupId>asm</groupId>
<artifactId>asm-tree</artifactId>
<version>3.3.1</version>
</dependency>
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类
package com.cnblogs.yjmyzz.action; import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result; @Namespace("/home")
public class HelloWorldAction extends BaseAction { private static final long serialVersionUID = -8827776224243873974L; private String message; @Action("hello-world")
public String execute() throws Exception {
return SUCCESS;
} @Action(value = "say-hi", results = { @Result(name = "success", location = "hello-world.jsp") })
public String sayHi() throws Exception {
message = "welcome to SSH!";
return SUCCESS;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} }
解释一下:
第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中配置了也一样)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <interceptors>
<interceptor name="myinterceptor"
class="com.cnblogs.yjmyzz.Interceptor.ExceptionInterceptor">
</interceptor> <interceptor-stack name="myStack">
<interceptor-ref name="myinterceptor" />
</interceptor-stack>
</interceptors> <default-interceptor-ref name="myStack" />
<default-action-ref name="index" /> <global-results>
<result name="error">/WEB-INF/common/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings> <action name="index">
<result type="redirectAction">
<param name="actionName">hello-world</param>
<param name="namespace">/home</param>
</result>
</action> </package> <!-- 因为有Convention-plugin,就不再需要手动写action-view的映射规则了 -->
<!-- <include file="struts-home.xml" />
<include file="struts-mytatis.xml" /> --> </struts>
原因在于convention-plugin使用后,所有Action不再继承自默认defaultStack对应的package,为了解决这个问题,建议所有Action都继承自一个自定义的BaseAction类,然后在BaseAction上加上@ParentPackage("default"),让所有Action强制继承自struts.xml中定义的default package
package com.cnblogs.yjmyzz.action; import org.apache.struts2.convention.annotation.ParentPackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.opensymphony.xwork2.ActionSupport; @ParentPackage("default")
public class BaseAction extends ActionSupport { private static final long serialVersionUID = -4320398837758540242L;
protected Logger logger = LoggerFactory.getLogger(this.getClass()); }
基本用法就是这些,如果不清楚Action最终出来的url是啥,或者不清楚某个url最终会对应到哪个jsp文件,无需死记规则,只要善用config-browser-plugin,大多数下不用查阅文档即可快速解决问题。
struts2: config-browser-plugin 与 convention-plugin 学习的更多相关文章
- Struts2 Convention Plugin ( struts2 零配置 )
Struts2 Convention Plugin ( struts2 零配置 ) convention-plugin 可以用来实现 struts2 的零配置.零配置的意思并不是说没有配置,而是通过约 ...
- struts2使用Convention Plugin在weblogic上以war包部署时,找不到Action的解决办法
环境: struts 2.3.16.3 + Convention Plugin 2.3.16.3 实现零配置 现象:以文件夹方式部署在weblogic(10.3.3)上时一切正常,换成war包部署,运 ...
- 创建一个dynamics 365 CRM online plugin (六) - Delete plugin from CRM
我们之前都学习到怎么添加,debug还有update plugin. 今天带大家过一下怎么从CRM instance当中删除plugin. 首先让我们打开Settings -> Customiz ...
- http://www.vaikan.com/docs/jquery.form.plugin/jquery.form.plugin.html#getting-started
http://www.vaikan.com/docs/jquery.form.plugin/jquery.form.plugin.html#getting-started Jquery.Form 异步 ...
- Struts Convention Plugin 流程 (2.1.6+)
首先添加lib: <dependency> <groupId>org.apache.struts</groupId> <artifactId>strut ...
- JQuery Plugin 1 - Simple Plugin
1. How do you write a plugin in jQuery? You can extend the existing jQuery object by writing either ...
- 在POM配置Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”的解决方案
eclipse在其POM文件的一处提示出错如下: Plugin execution not covered by lifecycle configuration: org.apache.maven.p ...
- 解决 在POM配置Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”
eclipse在其POM文件的一处提示出错如下: Plugin execution not covered by lifecycle configuration: org.apache.maven.p ...
- Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”
myeclipse在其POM文件的一处提示出错如下: Plugin execution not covered by lifecycle configuration: org.apache.maven ...
- "Loading a plug-in failed The plug-in or one of its prerequisite plug-ins may be missing or damaged and may need to be reinstalled"
The Unarchiver 虽好,但存在问题比我们在mac上zip打包一个软件xcode, 然后copy to another mac, 这时用The Unarchiver解压缩出来的xcode包不 ...
随机推荐
- [转载]50个Demo展示HTML5无穷的魅力
Flash和HTML5的比较已经成为现在最热门的主题之一,我们不去争论哪个好哪个不好.和HTML5在很酷的动画和简单的游戏等方面一样,除非HTML5在未来几年有一些重大发展,否则Flash在富内容网页 ...
- 从MVC框架看MVC架构的设计
尽管MVC早已不是什么新鲜话题了,但是从近些年一些优秀MVC框架的设计上,我们还是会发现MVC在架构设计上的一些新亮点.本文将对传统MVC架构中的一些弊病进行解读,了解一些优秀MVC框架是如何化解这些 ...
- Second glance in Go
Github上的"the way to Go"翻譯有時候真讓人搞不懂,我經常會暈,比如 如果需要申明一个在外部定义的函数,你只需要给出函数名与函数签名,不需要给出函数体: func ...
- [MySQL Reference Manual] 5 MySQL 服务管理
5. MySQL 服务管理 5. MySQL 服务管理 5.1 The Mysql Server 5.2 Mysql 服务日志 5.2.1 选择General query log和slow query ...
- 与POS机通信时的3DES(双倍长)加密解密
项目中有个SocketServer要和移动便携POS机通信,POS开发商就告诉我们他们用的3DES(双倍长)加密,给了个Key.数据和结果,让我们实现. c#用TripleDESCryptoServi ...
- Linux IPC POSIX 信号量
模型 #include<semaphore.h> #include<sys/stat.h> #include<fcntl.h> sem_open() //初始化并打 ...
- 2. Docker - 安装
一.Docker介绍 1. Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上, 也可以实现虚拟化. 容器时完全使用沙 ...
- ELF Format 笔记(八)—— 符号的类型和属性(st_info)
我是天空里的一片云,偶尔投影在你的波心,你不必讶异,更无须欢喜,在转瞬间消灭了踪影.你我相逢在黑夜的海上,你有你的,我有我的,方向:你记得也好,最好你忘掉,在这交会时互放的光亮! —— 徐志摩·偶然 ...
- oracle--trunc与to_char的区别
trunc取得是天(可比较),而to_char取得是数值(可计算): 但trunc(date) 具有与to_char(date) 相似的功能,但有区别: trunc(sysdate,'cc') ...
- C++浅析——虚函数的动态和静态绑定
源自一道面试题,觉得很有意思 class CBase { public: virtual void PrintData(int nData = 111); }; void CBase::PrintDa ...