`
高级java工程师
  • 浏览: 393706 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2配置详解

阅读更多
struts 2 配置详解  2010-03-02 22:44:46|  分类: struts2 相关 |  标签: |字号大中小 订阅 .

struts 2 配置详解

struts2的完整类库列表

antlr-2.7.2.jarcommons-beanutils-1.6.jarcommons-chain-1.1.jarcommons-logging-1.0.4.jarcommons-logging-api-1.1.jarcommons-validator-1.3.0.jarfreemarker-2.3.8.jarognl-2.6.11.jaroro-2.0.8.jarstruts-core-1.3.5.jarstruts2-codebehind-plugin-2.0.11.1.jarstruts2-config-browser-plugin-2.0.11.1.jarstruts2-core-2.0.11.1.jarstruts2-jasperreports-plugin-2.0.11.1.jarstruts2-jfreechart-plugin-2.0.11.1.jarstruts2-jsf-plugin-2.0.11.1.jarstruts2-pell-multipart-plugin-2.0.11.1.jarstruts2-plexus-plugin-2.0.11.1.jarstruts2-sitegraph-plugin-2.0.11.1.jarstruts2-sitemesh-plugin-2.0.11.1.jarstruts2-spring-plugin-2.0.11.1.jarstruts2-struts1-plugin-2.0.11.1.jarstruts2-tiles-plugin-2.0.11.1.jartiles-api-2.0.4.jartiles-core-2.0.4.jartiles-jsp-2.0.4.jarxwork-2.0.4.jar
基础类库列表(struts五个必须的jar包)

commons-logging-1.0.4.jarfreemarker-2.3.8.jarognl-2.6.11.jarstruts2-core-2.0.11.jarxwork-2.0.4.jar
基础配置(web.xml配置)


WEB.XML 

<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

Struts.xml 

<?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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <include file="example.xml"/> <!-- Add packages here --></struts>

 


配置文件

struts.xml 位置:classes下 配置 Action 

1、package 
struts对配置的组织结构类似于java package属性: 

1.1   name:包的名字,唯一标示

1.2   extends:  继承

1.3   namespace:命名空间


1 default namespace "" 

2 root namespace "/" 

3 action 搜索的顺序 当前namespace ---一级一级向上搜索 Web应用的请求路径被视为namespace     

4 访问 /aaa.action的时候的搜索的顺序

          "/"    --->   ""

    

5 namespace 没有目录信息 

6 package 包含0 - N 个 action <pre>;Action:<pre> name: 唯一标示 name.action ---> action name class:实现类 继承actionsupport的类 method:调用方法 动态调用的方法名称


1.4  abstract:  抽象申明 

2、Result 

name: 用来匹配action的返回值 type: 是result-type 进行预定义的/自定义的 常用dispatcher,转发 <result-types> <result-type name="dispatcher" class ="" default="true" ></result-type> </result-types>特殊位置:<global-results> <result name=""></result> ....</global-results>特殊参数:<result name="next_opertor" >${var_name}</result> var_name 必须是action里面的一个私有属性,并且有get/set方法 必须符合bean的命名规则: 小写开头 set/get大写开头的属性

3、include 

只能放在struts 标签的下一级 用来引入其他的struts配置文件 file = " " 被他引入xml文件是一个标准的struts配置


4、interceptors = > 拦截器

<interceptors> <interceptor name="" class=""/> <intercptor-stack name=""> <interceptor-ref name=""/> <interceptor-ref name=""/> </interceptor-stack><interceptors><default-interceptor-ref name=""><action。。。。。。> <interceptor-ref name="" /> 

5、constants 

<constant name="" value="" />可以完成struts.properties 所完成的工作可以完成 constant 所完成的工作


struts.properties

配置struts特性(动态方法调用,虚拟机)

struts-default.xml struts...jar里面,----〉零配置 struts-default.vm 模板


Action


I18N/I10N


拦截器


1.拦截器在struts 2接收到请求之后,抵达Action之前 以及离开Action之后 发挥作用 
1.抵达Action之前,可以处理参数(文件上传,struts2有内置拦截器),或者拦截请求 
2.离开Action之后,可以处理Action返回的Result(嵌套模板的拦截器) 
2.配置拦截器 
1.struts自己提供了缺省的拦截器, 
2.拦截器的使用的第一步 ,配置、可以理解为登记 
3.拦截器的使用的第二步 ,配置、定义缺省interceptor-stack 、或者 直接指明 某个Action使用的拦截器 
4.使用拦截器的时候以Stack为单位 
5.拦截器可以被覆盖,采取就近配置的拦截器:: namespace范围 或者 action范围内部 
6.拦截器之行的先后顺序:根据配置文件内容,由上至下(抵达Action之前),由下至上(离开Action之后) 

注册Interceptors: <interceptors> <interceptor name="name_1" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/> ....... </interceptors>注册Interceptor-stack: <interceptor-stack name="stack_name_1"> <interceptor-ref name="name_1"/> .......</interceptor-stack>使用Interceptor-stack在package中定义 :<default-interceptor-ref name="stack_name_1"/>在Action中定义 <action .......> <interceptor-ref name="stack_name_1"/> </action><pre>##系统自带拦截器###Alias Interceptor####转换参数名,内容不变<pre>name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor" 

1.实现参数读取的拦截器 Parameters Interceptor 
1.把请求中的参数放置到Action中 : 参数 ---》 Action中的 Property(attribute) 
2.放置在 default stack ,在覆盖的情况下可以直接使用 

name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"

2.实现国际化的拦截器 I18n Interceptor 
1.提供读取国际化 properties文件键值的方法 
2.放置在 default stack ,在覆盖的情况下可以直接使用 

name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"

3.实现cookie的读写和配置的拦截器 cookie Interceptor 
1.提供Action中操作Cookie的方法 
2.放置在 default stack ,在覆盖的情况下可以直接使用 

name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"

4.实现struts2校验功能的拦截器 validation Interceptor 
1.放置在 default stack ,在覆盖的情况下可以直接使用 
2.在Action执行缺省或者配置的方法之前进行调用validation方法 

name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"

5.实现Struts中数据(参数)前后共享的 Chainning Interceptor 
1.Action前后可以读取上下文的参数 (比如前一个 Action的属性) 
2.把所有使用到的Action 的属性放置在上下文的attribute中 
6.Create Session Interceptor 自动创建 Session 
1.提供 其他基于Session的Interceptor/Action提供session 

name="createSession"class="org.apache.struts2.interceptor.CreateSessionInterceptor"

7.异常拦截器 Exception Interceptor 
1.把异常定位到一个确切的页面 

name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"

8.提供Action操作 Session 与 Application(Servlet context )的方法Scope Interceptor 
1.Action直接操作session/application的方法 

name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"

9.文件上传的拦截器 fileuploader interceptor 
1.提供基于struts2的文件上传功能 

name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"

10.Logger Interceptor 输出Action的名字 
 


要点


在Struts 2中实现文件上传


1.所需要的 jar文件 

基本的 struts2的 類庫 (commons-logging-1.0.4.jar日志記錄 ,freemarker-2.3.8.jar 模板實現 ,ognl-2.6.11.jar onnl語言支持 ,struts2-core-2.0.11.jar struts2的核心類庫,xwork-2.0.4.jar struts2的基礎類庫)apache upload 組件 (commons-fileupload-x.xxxx.jar)apache io 組件 (commons-io-x.xx.xx.jar)

2.所需要的配置文件 

;struts.properties : 在該文件中需要修改臨時文件存儲路徑,文件的大小 struts.multipart.saveDir= /upload 保存目录,值得是realpath struts.multipart.maxSize=209715200 上传文件的大小限制 struts.multipart.parser=jakarta 解析上传文件信息(multipart/form-data,上传文件时的form数据类型一定是这种情况);struts.xml : 在该文件中配置文件类型,以参数的形式放置在fileUpload拦截器引用中 <interceptor-ref name="fileUpload"> <param name="allowedTypes"> image/png,application/pdf </param> </interceptor-ref>

3.所需要的操作 

在Action 中,直接使用 MultiPartRequestWrapper 来获取上传的文件及参数 (文件上传的时候 ,通过正常的参数获取渠道---params拦截器无法的到我们需要的参数,因为该拦截器无法解析multipart类型的请求) 这个时候 我们通过ServletActionContext得到的Request本身就是一个MultiPartRequestWrapper的实例,我们通过前置类型转换的到该对象。 multipartInfo.getParameter("") 得到我们需要的普通参数 multipartInfo.getFiles(""); 得到我们需要的文件对象(但我们得到该文件的时候,文件已经被上传,并且保存在struts.properties中所定义的目录中,该 Request被返回给客户端的时候,临时文件会被删除)

 


在Struts 2.0中国际化(i18n)应用程序


1.使用struts 的 Text标签可以读取在struts.properties文件中定义的消息文件中的消息文本 

struts.custom.i18n.resources 定义文件名称(包含路径信息,包含路径信息的时候,路径的分隔符用.)struts.locale=zh-cn 定义缺省的Location实际读取的文件: 文件名称+Locale的值+.properties 如果该文件找不到,会读取 文件名称+.properties 如果该文件依旧找不到,显示key的值在所有的struts ui tag(用于界面显示的标签 都有一个key属性,可以直接读取消息文件中key对对应的值 当出现非ASCII CODE的时候,我们需要 先使用本地语言编码编写文字内容,然后使用jdk提供的native2ascii工具转码,成为iso8859-1编码的文本文件,否则会出现乱码,该操作与IDE(Eclipse)所设定的缺省字符集,文件字符集等 无关

2.使用struts标签中以及Action中可以使用getText的方式的到消息文件中的消息文本 

在struts中的Text标签也是调用该方法 在Action中的父类ActionSupport中已经定义了该方法,可以直接使用 在Action中(不包含jsp页面、模板页面),不需要定义package.properties文件,但是可以直接读取该文件中的key-value对应信息, package.properties 局部消息文件 struts.custom.i18n.resources 全局消息文件 定义了局部消息文件后,局部消息文件会首先被读取。 文件名的定义不变 filename_local.properties

3.struts2中使用资源文件的local可以通过参数传递来得到 

参数是 java.util.Locale的实例 设置方式: ActionContext.getContext().setLocale(locale参数) 可以通过其它方式来设定locale,或者自我实现,但都不是Struts2的实现方式。

4.struts2 中的国际化有可能出现乱码,出现乱码后的解决方式 

引用文件的字符集 (jsp有三个: jsp自己 两个:contentType中的charset和pageEncoding, html 一个: meta 中的contentType中的charset,模板文件根据模板实现不同,但是与jsp方式类似),预定义不符合,或者三个字符集不一致。 服务器返回给客户端的数据编码与客户端请求不符合 消息文件本身是乱码:不一定是编写的乱码,而是读取的时候使用的字符集与文件本省的字符集不一直,缺省情况下读取的时候使用操作系统自己的缺省字符集读取,单独xp本身就有四种(其中三种是unicode,unicode的不同类型互补兼容--ASCII例外) 解决方式: 1、转码或过滤器,可以注册使用apache提供的字符集过滤器() 2、更新本地消息文件字符集、编码内容,(另存为新的字符集文件 转成新的字符集编码的文件 或者使用 java提供的 native2ascii转换成iso8859-1的字符集) 3、修改jsp/模板文件中的有关字符集定义,如果jsp自己定义的两个charset不一直,编译本身就会出错,只需要修改一致,如果修改一致后依旧是乱码,可以一起替换成新的字符集事实上,会涉及字符集只有:utf-8 / iso8859-1 / gbk CJK字符集 : 中日韩 字符集 JPN-xxxx;外包项目开发的时候才会遇到

 


在Struts 2.0中实现表单数据校验(Validation)


1.通过拦截器来实现,在Action中的方法被调用之前,首先调用validation方法或者其他验证方法 
2.验证结束后,如果验证失败,返回结果(result)为固定的input,返回内容中会包含错误信息。 

返回的错误集合以 key为 "fieldErrors"的一个数据对象保存在request中,如果使用模板技术,该错误信息会显示在该控件的下部(具体位置由模板控制),如果不使用模板技术,我们可以使用Struts2 Tag读取"fieldErrors"的值进行显示.

3.Struts的校验功能为服务器端校验 

校验代码: 配置文件struts.xml中的Action必须要有<result name="input"> </result> 校验代码中需要有 addfieldError方法(尽量在该方法中使用I18N代码,以方便国际化显示,因为这段代码是硬编码,不便于修改) 需要添加 @Override public void validate() { addfieldError(....); } 方法到Action中,所有的验证操作在该方法中实现.

4.如果验证成功,调用Action定义的方法,返回逻辑上应该得到的结果。 
 


在Struts 2中数据类型转换

Struts field user.name user.password ----> 在action中寻找一个属性,名称为user if find set user.name to user.name's value set user.passwordto user.name's password else DEBUG:打印消息 -----> After Find it: 数据类型转换: 得到属性 user的实例 (如果为null,通过class创建实例,注意field对应的类、bean需要包含无参构造方法) 调用set方法,给该对象赋值 【在这个地方完成数据类型转换】 【在这个地方完成数据类型转换】 基本数据类型 <----> String 引用数据类型 <----> String 比如: 在数据类型转换中,Date需要单独处理,原因是格式的问题: Date 请求的 Locale 对应的 DateFormat 的 short 格式 手动转换类型(日期): 转换方式, 1 重写Struts的 StrutsTypeConverter 2 注册在Struts中 ActionName-conversion.properties [针对Action] xwork-conversion.properties [针对Struts App] 3 非 Struts 类型自定义转换 @interface,就是jdk5之后的注释在程序中注册过的日期类型可以自动被调用,无需干涉。
在Struts 2中实现IoC


1.在Struts 2中实现IoC 
IoC 
Inversion Of Control 反转of控制,我们自己定义的类交给容器(运行上下文环境)去控制,并非在类的内部进行控制,依赖注入 (Dependency Injection) 与Ioc实际是一回事 

class A implement x class B implement x interface x xxxFactory xxxFactory.get(parameter); 

通过这种方式可以 动态的调用 某一个已知接口的实现类、或者是某一个抽象类的子类。 

在调用之前,被调用的类是未知的。 

这种方式被称为接口驱动(Interface Driver Design) 

在这种方式的基础上 使用接口实现的形式对已经实现的类进行非浸入方式的注入,从而达到不修改实现类的情况下完成比原有代码更多的工作,直接后果就是延缓接口的实现,在接口实现之前可以添加代码。 

这种行为被称为依赖注入(Dependency Injection) === Inversion Of Control 
配置 
把谁注入给谁,如果没有定义的话,依旧是一个典型的factory模式的代码不能完成真正的Ioc操作 ,通过 framework 进行配置和管理 
Struts2中使用 Ioc; 
加入spring-xxx.jar 【提供spring支持,spring是一个轻量级的Ioc容器,如果没有完整的jar文件 ,可以使用必须的四个包:spring-context-2.5.4.jar,spring-web-2.5.4.jar,spring-core-2.5.4.jar,spring-beans-2.5.4.jar】 
加入struts2-spring-plugin-2.0.11.1.jar【用于提供在Struts中使用spring Ioc控制的链接】 
加入配置文件 
ApplicationContext.xml 【配置Ioc中的Java Bean ( POJO,非 Ejb 的 JavaBean ) 】 

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans> <bean id="" class=""> <property name ="Field_Name" > <ref local ="another_bean_id" /> </property > <property name ="Field_Name" > </propertie> </bean> </beans>

1.find action 
2.search action with classname in applicationcontext.xml 
3.use a action class's instance defined by applicationcontext.xml 
 


完成一個Struts2 with Spring的IOC程序的步驟


1.定義 Struts2中的Action 【使用 applicationContext.xml中的bean_id作為我們的Action的class】 

<action name="CatchIocDataMethod" class="com.baoding.Action.CatchIocDataMethod"> 成為: <action name="CatchIocDataMethod" class="ActionClassId in ApplicationContext">

2.定義Spring 
1.拷貝jar:spring-context-2.5.4.jar,spring-web-2.5.4.jar,spring-core-2.5.4.jar,spring-beans-2.5.4.jar,struts2-spring-plugin-2.0.11.1.jar 5個文件到/WEB-INFO/lib 
2.建立文件 applicationContext.xml 到 /WEB-INFO/目錄 【如果在web.xml中定義了spring配置文件的路徑,另當別論】 
WEB.xml中路進的配置方式 

<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> </context-param>

1.
1.修改struts.properties文件或者struts.xml文件,定義struts中ObjectFactory使用的容器為Spring 

【struts.properties】 struts.objectFactory = spring //去掉改行注釋 【struts.xml】 <constant name="struts.objectFactory" value="spring" /> 所有的在struts.properties文件中出現的常量都可以使用這種方式在struts.xml中進行覆蓋

2.提示信息 

Initializing Struts-Spring integration ** struts2-spring-plugin 被使用后的提示信息Bean factory for application context ..... Spring Ioc容器成功初始化后的信
分享到:
评论
1 楼 dingz21 2012-08-10  
lz:
有没有比较复杂的s2sh项目的源码!

相关推荐

Global site tag (gtag.js) - Google Analytics