Struts2 学习笔记 http://www./struts-2/struts-2-tutorial/struts-2-framework-tutorial-1.html
The controller receives the user request and determine which
Struts 2 action to invoke. The framework creates an instance of this action and associate it
with the newly created instance of the ActionInvocation. In Struts 2 the invocation of action should pass through a series
of interceptors as defined in the application's XML file. The framework calls the ActionInvocations invoke() method to start
the execution of the action. Each time the invoke() method is called, ActionInvocation consults
its state and executes whichever interceptor comes next. ActionInvocation hands control over to the interceptor in the
stack by calling the interceptors intercept() method. The intercept() method of the interceptor inturn calls the
invoke() method of the ActionInvocation till all the interceptors are invoked,
in the end the action itself will be called and the corresponding result will
be returned back to the user. Some interceptor do work before the action is executed and some do
work after the action is executed. It's not necessary that it should do
something each time it is invoked. These interceptors are invoke both before and after the action. First all the interceptors are executed in the order they are
defined in the stack. Then the action is invoked and the result is generated. Again all the interceptors present in the stack are invoked in the
reverse order. The other important features of Struts 2 are OGNL and ValueStack. Object-Graph Navigation Language (OGNL) is a powerful expression
language that is used to reference and manipulate data on the ValueStack. OGNL help in data transfer and type conversion. OGNL expression language provides simplified stytax to reference
java objects. OGNL is used to bind the java-side data properties to the
string-based view layer. In Struts 2 the action resides on the ValueStack which is a part
of the ActionContext.
ActionContext is a global storage area that holds all the data associated with the
processing of a request. When a request comes the params interceptor
helps in moving the request data to the ValueStack. Now the OGNL does the job of converting the string based form data
to their corresponding java types. OGNL does this by using the set of available built-in type converters. Again when the results are generated the OGNL converts the java
types of the property on the ValueStack to the string-based HTML output. ActionContext is thread local which means that the values stored
in the ActionContext are unique per thread, this makes the Struts 2 actions thread safe. Intercepor ·
Interceptor可以看做是织入点(crossing-cut),指的是Log这类的工作不是针对某个特定的action的,而是所有的action。另一方面,interceptor实际上就是在action执行之前做了一些preporcessing和postprocessing,如param interceptor。拦截器还可以alter the workflow of the
invocation。 ·
Interceptor的抽象增加了reuse和configure的灵活性。启示:了解struts提供了哪些常用的interceptor,充分利用工具。 ·
Interceptor的执行顺序是由xml文件中的配置顺序决定的。 ·
Interceptor中可以做哪些事情: ?
Preprocessing.
Prepare, filter, alter, log , time, authority. ?
Pass the
control on to the successive or divert the execution by itself. ?
Postprocessing. ·
自定义interceptor时,intercept方法中,在调用了invoke方法之后,得到的result并不能说明action是否被执行,有可能是deeper
action alter 了
workflow。在此时也不能够拦截response,因为result已经render并send to
client了。 · 因为拦截器是有顺序执行的,所以如果两个拦截器都给同一个名称的属性负责,则后执行interceptor会override之前的值。 · 通过配置interceptor的excludeMethod参数来避免不必要的方法验证。第一次访问一个action时,由于表单是auto created,所以不需要验证,这时候可以通过给interceptor添加参数<param name=”excludeMethods”>someMethod</param>来解决。 · 常用的拦截器:token,exception,exeAndWait · Exception Interceptor的使用:在default package 里面定义一个global-results,并添加一个global-exception-mappings。可以为不同类型的exception指定不同的result. · Interceptor的配置。Specific interceptors, write stacks, pass parameters to interceptor,specific an interceport for an action. ·
Inteceptor
declarations. Interceport需要定义在package标签里。 ·
<!ELEMENT package (result-types?, interceptors?,
default-interceptor-ref?, default-action-ref?, default-class-ref?,
global-results?, global-exception-mappings?, action*)>。?指至多一个,这里“,”指明的标签出现的顺序。 ·
inteceptor的定义:<!ELEMENT
interceptors (interceptor|interceptor-stack)+> ·
可以为specific action指定interceptor-ref,这个ref即可以是单独的interceptor也可以是一个interceptor-stack,可以只当前package中所声明的,也可以是parent
package中声明的。 ·
为interceptor传递参数。 ?
第一种,直接修改某个interceptor的参数 <interceptor-ref
name=”workflow”> <param name=”excludeMethods”>input,
validate</param> </interceptor-ref> ? 第二种,通过interceptor-stack间接设置,如name=”workflow.excludeMethods” ·
自定义Interceptor,可以直接实现Interceptor的interface,为了支持参数,可以从MethodFilterInterceptor继承。 Struts2 JSON ·
Struts2提供了JSON的插件,在其lib目录下可以找到。google JSON plugin即可找到对应的参考文档。 ·
It seems
that unused interceptors does not affect the performance so much. ·
|
|
来自: xue_dong5437 > 《Java》