2009-3-28
<filter>
<filter-name>struts2</filter-name> ANNOTATION 1
<filterclass>
org.apache.struts2.dispatcher.FilterDispatcher</filterclass>
<init-param> ANNOTATION 2
<param-name>actionPackages</param-name>
<param-value>manning</param-value>
</init-param>
</filter>
(annotation) <#1”The FilterDispatcher: Struts 2 Begins Here”>
过滤器struts2拦截所有http请求,所以说,struts正是通过这个过滤器来完成充当controller角色的。
The actionPackages parameter is
necessary if you are going to use annotations in your application.
对于actionPackages参数,是非常重要的,它告诉程序去什么地方查找注解(关于action的)。
2009-3-28
关于struts2中Action作为Data Transfer Object的问题:
在struts1中,每个Action只会有一个实例,因此Action是不能作为DTO的,但是在struts2中,这种情况已完全不同了,每次有请求过来时,Struts都会重新创建一个新的Action实例。因此struts2中的Action都有天生就可以是一个DTO。
关于 default namespace
Note that the default namespace is actually the empty string "".
在package声明中,往往都继承一个默认包,这个默认包的namespace就是一个空串“”。
我们也可以看到,其实一个package的namespace,其实就是URL中对介于于servlet 上下文与一个Action名称之间的部分。如:
Package namespace是很必要的机制,基本上就像是代码的包一样,用来合理地组织action用的。更为重要的是我们常常会基于package namespace来进行权限配制。
注意:package的namespace不是jsp的组织结构!
关于package和namespace
package和namespace不一定非得是一一对应的关系,一个namespace是可以对应多个package的!
Note that you can give the samenamespace to more than one package. If you do this, the actions from the two packagesmap to the same namespace. This isn’t necessarily a problem. You might chooseto separate your actions into separate packages for some functional reason thatdoesn’t warrant a distinct namespace.
通常来说:namespace的组织原则是:依据action的相关性.其实默认情况下,一个action的namespace就是这个action类的包路径,这也体现了namespace的基本作用.就像我们按功能相关性来组织jsp一样,action的组织最好是用功能相关性来组织,有可能话也许会和jsp的组织结构能够对应起来.如果namespace是按功能来划分的,那么,对于其内部的action来说,如果它们需要有不同的配置,那就分成多个package,但是namespace是一样的.
2009-4-1
关于Action的执行机理:
在Struts2中一个Action的执行过程被 封闭在一个ActionInvoction中。下图是对一个Action的执行过程的详细描述。这个过程基本就是将拦截器栈中的拦截器依次读出并执行,最后执行Action。同样,在执行完毕返回的过程中也会依次调用这些拦截器。基本上这些拦截器就是负责一些横切性质的工作。也就是一些 preprocess和postprocess.
2009-4-11
关于Struts和spring集成的问题:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
At this point, you’re completely ready to start managing your objects with Spring. But,
as we indicated earlier, Spring won’t just start handling all of your objects. You must
tell Spring to intervene. To have Spring manage your objects, you need to declare
these objects as Spring beans in a Spring configuration file. By default, the Spring
container created by the ContextLoaderListener looks for metadata in an XML file at
/WEB-INF/applicationContext.xml.
以上是web.xml中一个listener的注册。正是这个listener让spring开始读取配件文件,并开始创建bean,而这个配件文件在哪呢?默认的位置就是/WEB-INF/applicationContext.xml
当然,我们也可以指定文件位置,方法如下:
<!-- Context Configuration locations for Spring XML files -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/applicationContext-resources.xml
classpath:/applicationContext-dao.xml
classpath:/applicationContext-service.xml
classpath*:/applicationContext.xml
/WEB-INF/applicationContext*.xml
/WEB-INF/xfire-servlet.xml
/WEB-INF/security.xml
</param-value>
</context-param>
2009-5-15
while actions that don’t define any interceptor-refs themselves will inherit the default
interceptors, as soon as an action declares its own interceptors, it loses that automatic
default and must explicitly name the defaultStack in order to use it.
Struts2action和Url的数据绑定。
Struts2采用了JavaBean的风格——要访问数据的话,就给字段提供一个getter和setter,要访问请求字符串和表单也是一样的道理。每一个请求字符串和表单的值都是一个简单的名/值对,所以要设定一个特定名称的值的话,就要为它提供一个setter。比如,如果一个JSP调用了“/home.action?framework=struts&version=2”这样一个请求,那么action就应该提供如下两个setter:“setFramework( String frameworkName )”和“setVersion( int version )”。