- 浏览: 416264 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (184)
- IDE (4)
- dotCMS (3)
- Liferay Portal (1)
- J2EE (7)
- My SQL (16)
- IBM DB2 (3)
- Oracle (2)
- JDBC (4)
- Hibernate (3)
- JSP (0)
- Framework (4)
- Java编程 (30)
- C++编程 (0)
- Struts 1.X (2)
- Struts 2.X (6)
- Linux (11)
- Spring (7)
- JavaScript (6)
- Ajax (2)
- XML (3)
- IBM Websphere Portal (1)
- IBM Lotus Quickr (1)
- CMS (2)
- ERP (0)
- CRM (0)
- 大型网站架构开发 (1)
- 面试武器 (2)
- HTML 5 (2)
- dTree && webFxloadTree (2)
- JVM (7)
- SQL Server (3)
- Tomcat && Apache && Jboss && Weblogic-中间件 (4)
- FreeMarker (2)
- MongoDB (7)
- OpenSource开源 (24)
- Cloud (0)
- FFmpeg (3)
- Thrift (1)
- SpringSide (1)
- Design Pattern (1)
- solr&&ES (2)
- git&svn (1)
- 大数据 (8)
- 人工智能 (0)
- Hadoop (3)
- Spark (0)
- Sqoop (1)
- Flume (1)
- Hive (3)
- HDFS (4)
- ES (0)
- Redis (1)
- Kafka (3)
- MR (0)
- 机器学习 (0)
- 深度学习 (0)
- Impala (2)
- HBase (2)
- Spring Boot (1)
- Spring Cloud (0)
- 大数据架构 (6)
- 架构思想理论 (6)
- 技术管理 (4)
- 数据结构与算法 (4)
最新评论
-
huijz:
...
Spring Data JPA研究-使用Spring Data JPA 简化JPA 开发(ZZ) -
用户名不存在:
[img][/img][*]引用[u][/u][i][/i][ ...
MongoDB 模糊查询的三种实现方式-morphia实现 -
junsheng100:
请给个完整的例子吧!包括jar文件等
java调用ffmpeg获取视频文件信息参数代码 -
mj:
谢谢!!
SQL Server里面如何导出包含(insert into)数据的SQL脚本 (转载ZZ)
好的东西应该是简单而又功能强大的,struts2就是这样一个优秀的Java Web MVC框架。
其实使用struts2可以更简单,那就是使用struts-convention来达到零配置,彻底抛弃struts.xml文件。
具体配置方法网上有很多,官方文档见这里
https://cwiki.apache.org/WW/convention-plugin.html
--------------------------------------------------------------
struts2 convention plugin配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--开发模式设置开始//-->
<!--++++++++++++++++++++++++++++++++++++++++++++++++开发状态 -->
<constant name="struts.devMode" value="true" />
<!-- 国际化资源重新加载 -->
<constant name="struts.i18n.reload" value="true" />
<!-- 配置文件重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- convention类从新加载 -->
<constant name="struts.convention.classes.reload" value="true" />
<!--++++++++++++++++++++++++++++++++++++++++++++++++开发模式结束 -->
<!-- 主题 -->
<constant name="struts.ui.theme" value="simple" />
<!-- 地区 -->
<constant name="struts.locale" value="zh_CN" />
<!-- 国际化编码 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 扩展-->
<constant name="struts.action.extension" value="action,do,jsp" />
<!-- 启用动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 设置Struts 2是否允许在Action名中使用斜线 -->
<constant name="struts.enable.SlashesInActionNames" value="false" />
<!-- 结果资源所在路径 -->
<constant name="struts.convention.result.path" value="/"/>
<!-- action后缀 -->
<constant name="struts.convention.action.suffix" value="Action"/>
<!-- 名称首字母小写 -->
<constant name="struts.convention.action.name.lowercase" value="true"/>
<!-- 分隔符 一个action名字的获取。比如为HelloWorldAction。按照配置,actionName为hello_world。 -->
<constant name="struts.convention.action.name.separator" value="_"/>
<!-- 禁用扫描 -->
<constant name="struts.convention.action.disableScanning" value="false"/>
<!-- 默认包 -->
<constant name="struts.convention.default.parent.package" value="default"/>
<!--确定搜索包的路径。只要是结尾为action的包都要搜索。basePackage按照默认不用配置,如果配置,只会找以此配置开头的包。locators及locators.basePackage都是一组以逗号分割的字符串。 -->
<constant name="struts.convention.package.locators" value="actions"/>
<!-- 禁用包搜索 -->
<constant name="struts.convention.package.locators.disable" value="false"/>
<!-- 基于什么包 -->
<constant name="struts.convention.package.locators.basePackage" value="com.sunflower.actions"/>
<!-- 排除的包 -->
<constant name="struts.convention.exclude.packages" value="org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.*"/>
<!-- 包含的包 -->
<!-- 包括的jar,一般用于大型项目,其action一般打包成jar -->
<constant name="struts.convention.action.includeJars" value="" />
<!-- 结果类型 -->
<constant name="struts.convention.relative.result.types" value="dispatcher,freemarker"/>
<!--
如果此值设为true,如果一个action的命名空间为/login,名称为HelloWorldAction。result返回值是success,默认会找到/WEB-INF/pages/login/hello_world.jsp(如果有hello_world_success.jsp就找这个文件,连接符“_”是在<constant name="struts.convention.action.name.separator" value="_"/>中配置的)。如果有一个action的result返回值是“error”,就会找/WEB-INF/pages /login/hello_world_error.jsp。
如果此值设为false,如果一个action的命名空间为/login,名称为HelloWorldAction。result返回值是success,默认会找到/WEB- INF/pages/login/hello_world/index.jsp(如果有success.jsp就找这个文件)。如果有一个action的result返回值是“error”,就会找/WEB-INF/pages /login/hello_world/error.jsp。
-->
<constant name="struts.convention.result.flatLayout" value="true"/>
<constant name="struts.convention.action.mapAllMatches" value="false"/>
<!-- 检查是否实现action -->
<constant name="struts.convention.action.checkImplementsAction" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.convention.redirect.to.slash" value="true"/>
<package name="default" extends="struts-default">
<interceptors>
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="actionMappingParams" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="params" />
</interceptor-stack>
</interceptors>
</package>
</struts>
------------------------------------------------------------------------------------
最近开始关注struts2的新特性,从这个版本开始,Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置。
配置文件精简了,的确是简便了开发过程,但是,我们熟悉的配置突然disappear了,真是一下很不适应。跟着潮流走吧,看看该怎样来搞定convention-plugin。
使用Convention插件,你需要将其JAR文件放到你应用的WEB-INF/lib目录中,你也可以在你Maven项目的POM文件中添加下面包依赖
view plaincopy to clipboardprint?
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.1.6</version>
</dependency>
零配置并不是没有配置,而是通过约定大于配置的方式,大量通过约定来调度页面的跳转而使得配置大大减少。所以,首先应该了解下convention-plugin的约定:
1. 默认所有的结果页面都存储在WEB-INF/content下,你可以通过设置struts.convention.result.path这个属性的值来改变到其他路径。如:
<constant name="struts.convention.result.path" value="/WEB-INF/page" />
则将路径配置到了WEB-INF/page 下。
2. 默认包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。你可以通过设置struts.convention.package.locators属性来修改这个配置。如:
<constant name="struts.convention.package.locators" value="web,action" />
则定义了在项目中,包路径包含web和action的将被视为Action存在的路径来进行搜索。Com.ustb.web.*/com.ustb.action.*都将被视为含有Action的包路径而被搜索。
3. 接着,Convention从前一步找到的package以及其子package中寻找 com.opensymphony.xwork2.Action 的实现以及以Action结尾的类:
1. com.example.actions.MainAction
2. com.example.actions.products.Display (implements com.opensymphony.xwork2.Action)
3. com.example.struts.company.details.ShowCompanyDetailsAction
4. 命名空间。从定义的.package.locators标示开始到包结束的部分,就是命名空间。举个例子:
Com.ustb.web.user.userAction的命名空间是:”/user”。Com.ustb.web.user.detail.UserAction的命名空间是:”/user/detail”
5. Convention通过如下规则确定URL的具体资源部分:去掉类名的Action部分。然后将将每个分部的首字母转为小写,用’-’分割,你可以设置struts.convention.action.name.separator 如
1. <constant name="struts.convention.action.name.separator" value="-" />
还是举个例子:
UserAction->user UserDetailAction ->user-detail。结合上面的。对于com.ustb.web.user.detail.UserDetailAction,映射的url就是/WEB-INF/content/user/detail/user-detail.jsp
6. struts支持.jsp .html .htm .vm格式的文件。
下面是actiong和结果模版的映射关系:
URL
Result
File that could match
Result Type
/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
当然,简单的通过默认的方式来进行配置不能完全满足实际项目的需要。所幸,convention的零配置是非常灵活的。
通过@Action注释
对如下例子:
1. package com.example.web;
2.
3. import com.opensymphony.xwork2.Action;
4. import com.opensymphony.xwork2.ActionSupport;
5.
6. public class HelloAction extends ActionSupport {
7. @Action("action1")
8. public String method1() {
9. return SUCCESS;
10. }
11.
12. @Action("/user/action2")
13. public String method2() {
14. return SUCCESS;
15. }
16. }
方法名
默认调用路径
默认映射路径
method1
/hello!method1.action .
/WEB-INF/content/hello.jsp
method2
/hello!method2.action.
/WEB-INF/content/hello.jsp
通过@Action注释后
方法名
@Action注释后调用路径
@Action注释 后映射路径
method1
/action1!method1.action.
/WEB-INF/content/action1.jsp
method1
/user/action2!method2.action
/WEB-INF/content/user/action2.jsp
通过@Actions注释
1. package com.example.web;
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 HelloAction extends ActionSupport {
8. @Actions({
9. @Action("/different/url"),
10. @Action("/another/url")
11. })
12. public String method1() {
13. return “error”;
14. }
我们可以通过:/different/url!method1.action 或 /another/url!method1.action 来调用method1 方法。
对应的映射路径分别是/WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp
可能误导了大家,一个方法被@Action注释后,只是多了一种调用方式,而不是说覆盖了原来的调用方式。比如对于如下例子:
1. com.example.web;
2.
3. import com.opensymphony.xwork2.ActionSupport;
4. import org.apachestruts2.convention.annotation.Action;
5. import org.apache.struts2.convention.annotation.Actions;
6.
7. public class HelloAction extends ActionSupport {
8. @Action("/another/url")
9. public String method1() {
10. return “error”;
11. }
我们调用method1方法可以通过两种方式:
1 /hello!method1.action 映射 url:/WEB-INF/content/hello-error.jsp
2 /another/url!method1.action 映射 url:/WEB-INF/content/another/url-error.jsp
可见,两种方式均可对method1方法进行调用,唯一的区别就是,两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一个非常好的选择。
通过@Namespace 注释
1. package com.example.web;
2.
3. import com.opensymphony.xwork2.ActionSupport;
4. import org.apachestruts2.convention.annotation.Action;
5. import org.apache.struts2.convention.annotation.Actions;
6. @Namespace("/other")
7. public class HelloWorld extends ActionSupport {
8.
9. public String method1() {
10. return “error”;
11. }
12. @Action("url")
13. public String method2() {
14. return “error”;
15. }
16.
17. @Action("/different/url")
18. public String method3() {
19. return “error”;
20. }
21. }
通过 /other/hello-world!method1.action 访问method1 方法。
通过 /other/url!method2.action 访问method2 方法
通过 /different /url!method3.action 访问method3 方法
与@Action 注释不同的是,该注释覆盖了默认的namespace(这里是’/’),此时再用hello!method1.action 已经不能访问method1 了.
@Results和@Result
1 全局的(global)。
全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。
1. package com.example.actions;
2.
3. import com.opensymphony.xwork2.ActionSupport;
4. import org.apachestruts2.convention.annotation.Action;
5. import org.apachestruts2.convention.annotation.Actions;
6. import org.apache.struts2.convention.annotation.Result;
7. import org.apache.struts2.convention.annotation.Results;
8.
9. @Results({
10. @Result(name="failure", location="/WEB-INF/fail.jsp")
11. })
12. public class HelloWorld extends ActionSupport {
13. public String method1() {
14. return “failure”;
15. }
16. @Action("/different/url")
17. public String method2() {
18. return “failure”;
19. }
20.
21. }
当我们访问 /hello -world !method1.action 时,返回 /WEB-INF/fail.jsp
当我们访问 /hello -world !method2.action 时,返回 /WEB-INF/fail.jsp
当我们访问 /different/url!method2.action 时,返回 /WEB-INF/fail.jsp
2 本地的(local)。
本地results只能在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. import org.apache. struts2.convention.annotation.Result;
7. import org.apache. struts2.convention.annotation.Results;
8.
9. public class HelloWorld extends ActionSupport {
10. @Action(value="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")})
11. public String method1() {
12. return “error”;
13. }
14. }
当我们调用 /hello -world !method1.action 时,返回 /WEB-INF/content/hello-error.jsp
当我们调用 /other/bar!method1.action 时,返回 www.baidu.com
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yenching/archive/2010/06/23/5688246.aspx
发表评论
-
javaMelody监控接入使用
2016-03-03 23:55 13591.jar pom <!-- javamelody ... -
Struts 2的漏洞 与需要版本升级(From Apache官网)
2013-07-24 10:47 1764Struts 2的漏洞原理: Struts又爆远程代码 ... -
第三只眼看SpringSide-一个极富魅力的偶像团队(ZZ)
2012-03-15 14:06 1711第三只眼看SpringSide-一个极富魅力的偶像团队 ... -
Struts 2获取session和request对象方法汇总
2011-02-21 16:53 1432在struts1中,获得到系统的request或者sessio ... -
Struts 2.0全攻略之Struts 2.0参考手册
2011-01-28 10:58 1296附件是Struts 2.0全攻略之Struts 2.0参考手册 ...
相关推荐
struts2-Convention插件详细使用
Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置,使用Convention插件,你需要此JAR文件
Struts2 的第三方插件 使用注解的方式配置struts.xml文件
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
Struts2插件convention
从 Struts 2.1 开始,Struts 2 改为使用 Convention 插件来支持零配置。Convention 插件彻底地抛弃了配置信息,不仅不需要使用 struts.xml 文件进行配置,甚至不需要使用 Annotation 进行配置。而是由 Struts 2 根据...
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
如何使用插件convention来实现struts2框架的零配置以及整合ssh架构
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
1 Convention插件 1.1 设置结果页面路径 默认所有的结果页面都存储在WEB-INF/content下,你可以通过设置struts.convention.result.path这个属性的值来改变到其他路径。如: Xml代码 <constant name="struts....
升级到2.3.32所用到的jar包: freemarker-2.3.22.jar ognl-3.0.19.jar struts2-convention-plugin-2.3.32.jar struts2-core-2.3.32.jar struts2-spring-plugin-2.3.32.jar xwork-core-2.3.32.jar --来源 ...
无需配置Convention即可使用Convention,Convention的某些约束习惯可以通过配置属性来控制,您也可以在类中覆写其中的方法来达到扩展目地。
将 Struts 2 项目下 struts2-convention-plugin-2.1.6.jar、struts2-rest-plugin-2.1.6.jar 两个 JAR 包复制到 Web 应用的 WEB-INF/lib 路径下。 由于 Struts 2 的 REST 插件还需要将提供 XML、JSON 格式的数据,...
struts2的convention插件使用.docx )
最近温习ssh2整合编程,顺便浏览下struts2有什么更新的消息,下载了新版本的struts2的2.1.8.1版,使用的是MyEclipse8.0开发,但是问题就随之而来了。MyEclipse8.0中自带的struts2版本是2.1.6,spring版本有2.0,2.5...
1) 利用Struts2的struts2-convention-plugin插件,实现URL →Action、Result→JSP的自动映射。 2) 利用Struts2的struts2-spring-plugin插件,实现在Action中自动注入service对象。 3) 利用Spring的@Service、@...
struts2 convention plugin(插件)测试项目
- 如果您使用基于Jakarta插件,请升级到Apache Struts 2.3.32或2.5.10.1版本。(强烈推荐) - 升级到2.3.32所用到的jar包:(已经在附件中提供jar文件) - freemarker-2.3.22.jar - ognl-3.0.19.jar - struts2-...
SSH整合+struts convention插件小示例 全程使用注解扫包,无struts action的配置文件,无Hibernate配置文件, Spring自动扫包来获取bean 数据库内容太简单,不再上传。 只是一个小实例。 请用MyEclipse导入项目。 我...