- 浏览: 391666 次
- 性别:
- 来自: 昆明
文章分类
- 全部博客 (198)
- java (28)
- J2EE (19)
- struts (33)
- sping (13)
- hibernate (18)
- mybatis (15)
- connection pool (3)
- DB (26)
- SQL (21)
- html (13)
- js (7)
- json (3)
- jquery (2)
- document (17)
- linux (15)
- C# (1)
- url (2)
- eweb4j (1)
- Turbine (1)
- 框架 (11)
- jdbcTemplate (3)
- aop (2)
- windows (2)
- ubuntu (1)
- eclipse (11)
- JPA (8)
- svn (4)
- get 传值 (2)
- mysql (5)
- oracle (2)
- hadoop (1)
- MongoDB (2)
- spring (1)
- python (1)
最新评论
-
jcbingjc:
楼主,你好,按照上面的配置,我报如下错误:Missing Pe ...
[properJavaRDP]在网页中实现远程连接 -
sucful:
折腾了半天跑不通 ,要传就搞个完整的吧
Struts2 <sx:div/>实现页面模块异步刷新 -
Kattou:
你好! 看了你写的这个分页 感觉很好, 但是不怎么会用么,请指 ...
jsp分页控件 -
我叫PSB:
地址完全用不了
eclipse中的安装 jseclipse -
1111emotion:
我的工程里还是有乱码的情况,我该改的地方都改了。
Eclipse/MyEclipse更改默认字符集 设置UTF-8
受到大环境的影响:Sping、Hibernate、Struts等框架纷纷抛弃XML配置文件,改为使用Annotaction管理配置信息。
Struts2.1开始后,Struts2升级了“零配置”支持:抛弃了Codebehind插件,改为使用Convention插件,它完全抛弃了XML配置和Annotaction,改为根据“约定”来搜索Action,管理Action和Result映射。
一、Convention插件的相关常量
Convention插件主要解决Action管理、Result管理等常见、也最琐碎的配置。
常量名 | 说明 |
struts.convention.action.disableJarScanning | 设置是否从JAR包里搜索Action类,如果开发者喜欢将Action类打包成JAR,则应该将该常量设为tue,默认值为false |
struts.convention.action.packages | Convention以该常量指定包作为根包来搜索Action类 |
struts.convention.result.path | 设置Convention插件定位视图资源的根路径,默认为/WEB-INFO/content |
struts.convention.result.flatLayou | 如果设置为false,则可以将视图页面放置Action对应的目录(无需放入WEB-INFO/content) |
struts.convention.action.suffix | Convention搜索Action类的类名后缀,默认为Action |
struts.convention.action.disableScanning | 是否禁止通过包扫描Action,默认是false |
struts.convention.action.mapAllMatches | 设置即使没有@Action注释,依然创建Action映射。默认值为false |
struts.convention.action.checkImplementsAction | 设置Convention映射的Action所在包的默认父包。默认值是convention-default |
struts.convention.action.name.lowercase | 设置映射Action时,是否将Action的name属性值转换为所有字母小写,默认值是true |
struts.convention.aciton.name.separator | 设置映射Action时指定name属性值各单词之间的分隔符。默认值为中画线(“-”) |
struts.convention.package.locators | Convention插件使用该常量指定的包作为搜索Action的根包。默认值为action,actions,struts,struts2 |
struts.convention.package.locators.disable | 指定禁止从Action的根包里搜索Action。默认值为false |
struts.convention.exclude,packages | 指定排除在搜索Action之外的包。默认值为org.hibernate.* ... |
struts.convention.package.locators.basePackage | 如果指定了该常量,Convention只会以该常量值开始的包中搜索Action类 |
struts.convention.relative.result.types | 指定Convention映射的Result时默认支持的结果类型。默认值为dispatcher,velocity,freemarker |
struts.convention.redirect.to.slash | 设置是否重定向到斜线(/)。例如用户请求/foo,但/foo不存在时,如果设置该常量为可重定向到/foo/。默认值是true |
二、使用Convention
除了struts2的相关jar包外还需要引入:struts2-convention-plugin-2.1.6.jar
(1)、Aciton配置相关Annotation
与Action相关的两个Annotation是@Actions和@Action。
@Action主要用于修饰Action类里的方法,用于将该方法映射到指定的URL。相当与strus.xml文件中<action .. />
value属性:指定该Action映射的URL
params属性:是一个字符串数组,用于将Action指定的参数名和参数值;格式:{"name1","value1","name}
@Action(value = "/login", //使用results属性为该方法指定Result映射 results = { @Result(name = "success", location = "/index.jsp")}) public String login() throws Exception { return "login"; }
@Actions 修饰类里的方法,用于将该方法映射到多个URL。
//使用@Actions将该方法映射成两个逻辑Action
@Actions({ @Action(value="/crazyit" ,params={"injectProp" , "疯狂Java联盟"}), @Action("/leegang") }) public String login() throws Exception { return "login"; }
(2)、Result配置相关的Annotation
与Result配置相关的Annotation:@ResultPath、@Result、@Results。
@result用于定义逻辑视图和物理视图之间的对应关系,相当于struts.xml中<result.. />
name属性:必须属性,相当与<result ../>中的name,结果映射的名字。
type属性:指定视图资源的类型,相当于<result ../> 中的type。
location属性:用于指定实际视图资源的位置。相当于<result>..</result>的中间部分
params属性:用于为该Result指定参数值,格式{name1,value1m,name2,value2...} 相当于<result ..>元素里多个<param../>
@ResultPath 则用于修饰包和Action类,用于改变被修饰Action所对应的物理视图资源的跟路径。
//namespace /abc/action @ResultPath("/zf") //为该Action指定2个Action级的Result映射 @Results({ @Result(name = "input", location = "/login.jsp"), @Result(name = "error", location = "/error.jsp") })
(3)、包和命名空间相关的Annotation
@Namespace:修饰Action类或其所在的包。
如:@Namespace(value="/zf")
@Namespaces:修改Action类或其所在的包。用于组合多个@Namespace
@ParentPackage:修饰Action类或其所在的包,用于指定被修饰的Action 所在包的父包
(4)、异常处理相关的Annotation
与异常相关的Annotation有@ExceptionMapping和@ExceptionMappings。
@ExceptionMapping 用于定义异常类和物理视图之间的对应关系,也就是相当于struts.xml 文件里的<exception-mapping../>元素的作用。
@ExceptionMappings 用于组织多个@ExceptionMapping
//指定该Action到/abc目录下寻找物理视图资源 @ResultPath("/abc") //为该Action定义2个Action级的异常定义 @ExceptionMappings({ @ExceptionMapping(result="null" , exception = "java.lang.NullPointerException"), @ExceptionMapping(result="cantcast" , exception = "java.lang.ClassCastException") }) public class LoginAction { //使用@Action将该方法映射到/crazyit @Action(value="/crazyit", //使用exceptionMappings 属性为该方法指定异常定义 exceptionMappings = { @ExceptionMapping(result = "success" , exception = "java.sql.SQLException")} ) public String login() throws Exception { return "success"; } }
(4)拦截器配置相关的Annotation
与拦截器相关的Annotation有@InterceptorRef、@InterceptorRefs、@DefaultInterceptorRef。
@InterceptorRefs 用于组织多个@InterceptorRef。
value属性:为多个@InterceptorRef
@InterceptorRef 用于指定Action引用拦截器或者拦截器栈。相当于struts.xml中的<action ../>元素内部的<interception-ref ../>子元素的作用.
params属性:用于覆盖所引用该拦截器的默认参数值,格式满足{name1,value1,name2,value2,..} 相当于<interception-ref ../>中的<param ../>
@DefaultInterceptorRef 主要用于修饰包,用于指定该包的默认拦截器。相当于<interceptor-ref name="defaultStack"/>=》value="defaultStack"
value属性:指定默认拦截器的名字
三、相关配置
1、web.xml
<filter> <!-- 定义核心Filter的名字 --> <filter-name>struts2</filter-name> <!-- 定义核心Filter的实现类 --> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <!-- 配置Action作为搜索Action的根包 --> <init-param> <param-name>struts.convention.action.packages</param-name> <param-value>com.cn.actions</param-value> </init-param> </filter> <!-- FilterDispatcher用来初始化Struts 2并且处理所有的HTTP请求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2、LoginAction.java
import org.apache.struts2.dispatcher.*; import org.apache.struts2.convention.annotation.*; public class LoginAction { //封装请求参数的username和password属性 private String username; private String password; private String injectProp; //依赖注入injectProp属性值的setter方法 public void setInjectProp(String injectProp) { this.injectProp = injectProp; } public String getInjectProp() { return this.injectProp; } //省略username和password的setter和getter方法 //username属性的setter和getter方法 public void setUsername(String username) { this.username = username; } public String getUsername() { return this.username; } //password属性的setter和getter方法 public void setPassword(String password) { this.password = password; } public String getPassword() { return this.password; } //使用@Actions将该方法映射成两个逻辑Action @Actions({ @Action(value="/crazyit" ,params={"injectProp" , "疯狂Java联盟"}), @Action("/leegang") }) public String login() throws Exception { return "login"; } //将abc()方法映射到指定URL。 @Action("/other-url") public String abc() throws Exception { return "abc"; } }
发表评论
-
Struts2.1.8升级到Struts2.3.15.1的时候遇到了问题
2013-08-09 10:14 1964我把原来的Struts2.1.8升级到Struts2.3. ... -
struts2 url传值中文乱码解决方案
2012-09-21 16:21 1387HttpServletRequest request = Ac ... -
使用struts2中的ognl表达式调用类方法
2012-04-19 09:35 1328struts标签中value都会被解析,如,<s:pro ... -
使用struts2中的ognl表达式调用类方法(转)
2012-04-15 16:54 1251struts标签中value都会被解析,如,<s:pro ... -
Struts2 get 传值乱码过滤器配置
2012-04-15 16:31 1330package cn.org.du.Encode; im ... -
struts2 url传值中文乱码解决方案
2012-04-15 15:51 1476HttpServletRequest request ... -
struts2 result type 介绍
2012-03-23 09:14 1100在默认时,<result>标签的type属性值是“ ... -
Struts2中<jsp:forward page="xxx.action"></jsp:forward>失效
2012-03-08 17:43 1925问题:在Struts2中<jsp:forward pag ... -
Struts2整合Spring、JPA
2012-02-27 09:46 1881一直觉得JPA很神秘,最近抽空看了下,下面贴出刚才做的St ... -
JAVA三大框架的各自作用
2011-11-25 00:04 3150一、Spring Spring是 ... -
使用SSH到底是为了快速开发,还是为了标准?
2011-11-25 00:00 2427使用SSH到底是为了快速开发,还是为了标准? 使用S ... -
Struts2、Spring、Hibernate 高效开发的最佳实践
2011-11-26 00:16 1230引言 SSH(Struts2+Spring+Hi ... -
Struts2与urlrewrite整合
2011-11-24 00:15 3314Struts2与URL Rewrite整合注意的地方 ... -
SiteMesh模板应用与struts2整合
2011-11-23 09:23 1702SiteMesh是一个非常优秀 ... -
struts2 配置详解
2011-11-18 10:00 1623基础Constants struts.devMo ... -
在Struts 2中实现单文件上传
2011-11-12 19:43 1029实现原理 Struts 2是通过Commons Fi ... -
SSH中在Action中用Spring的aop来验证用户是否已经登录的拦截器
2011-11-12 19:45 22821.Spring的aop来验证用户是否已经登录的拦截器 ... -
Struts2拦截器
2011-11-13 10:49 10511. 理解拦截器 1.1. ... -
Struts2 将两个<sx:autocompleter/>关联起来
2011-11-13 10:50 2302本人最近两天想使用sx:autocpmpleter来实现地区的 ... -
Struts2 <sx:div/>实现页面模块异步刷新
2011-11-13 10:50 3797package lee; import com.open ...
相关推荐
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
简述STRUTS2_Convention零配置
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
struts2采用convention-plugin实现零配置,希望大家用的到!
struts-convention实现零配置
Struts-Convention实现零配置,可直接部署运行,主要针对于新手,很简单的一个项目,内有说明:explanation.txt
如何使用插件convention来实现struts2框架的零配置以及整合ssh架构
你可以通过设置struts.convention.package.locators属性来修改这个配置。如: <constant name="struts.convention.package.locators" value="web,action" /> 则定义了在项目中,包路径包含web和action的将被视为...
从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。 • 包命名习惯来指定Action位置 • 命名习惯制定结果(支持JSP,FreeMarker等)路径 • 类名到URL的约定转换 • 包名...
Struts采用convention-plugin实现零配置
从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。
配置struts2 - 风轻云淡 配置struts2 - 风轻云淡
Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置,使用Convention插件,你需要此JAR文件
struts2-convention-plugin-2.3.15.3.jar struts2配置Action注解要用到的包,真正实现零配置
struts2 spring4 hibernate4 Struts2零配置基本架包 struts2-json-plugin-2.5.5.jar struts2-convention-plugin-2.5.5.jar
NULL 博文链接:https://fish119.iteye.com/blog/542614
从 Struts 2.1 开始,Struts 2 改为使用 Convention 插件来支持零配置。Convention 插件彻底地抛弃了配置信息,不仅不需要使用 struts.xml 文件进行配置,甚至不需要使用 Annotation 进行配置。而是由 Struts 2 根据...