错过的情人的歌词:Struts学习笔记(一)

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 17:43:15

Struts2  学习笔记

http://www.vaannila.com/struts-2/struts-2-tutorial/struts-2-framework-tutorial-1.html

The controller receives the user request and determine whichStruts 2 action to invoke.

The framework creates an instance of this action and associate itwith the newly created instance of the ActionInvocation.

In Struts 2 the invocation of action should pass through a seriesof interceptors as defined in the application's XML file.

The framework calls the ActionInvocations invoke() method to startthe execution of the action.

Each time the invoke() method is called, ActionInvocation consultsits state and executes whichever interceptor comes next.

ActionInvocation hands control over to the interceptor in thestack by calling the interceptors intercept() method.

The intercept() method of the interceptor inturn calls theinvoke() method of the ActionInvocation till all the interceptors are invoked,in the end the action itself will be called and the corresponding result willbe returned back to the user.

Some interceptor do work before the action is executed and some dowork after the action is executed. It's not necessary that it should dosomething 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 aredefined in the stack.

Then the action is invoked and the result is generated.

Again all the interceptors present in the stack are invoked in thereverse order.

The other important features of Struts 2 are OGNL and ValueStack.

Object-Graph Navigation Language (OGNL) is a powerful expressionlanguage 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 referencejava objects.

OGNL is used to bind the java-side data properties to thestring-based view layer.

In Struts 2 the action resides on the ValueStack which is a partof the ActionContext.ActionContext is a global storage area that holds all the data associated with theprocessing of a request.

When a request comes the params interceptorhelps in moving the request data to the ValueStack.

Now the OGNL does the job of converting the string based form datato 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 javatypes of the property on the ValueStack to the string-based HTML output.

ActionContext is thread local which means that the values storedin 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 theinvocation

·        Interceptor的抽象增加了reuse和configure的灵活性。启示:了解struts提供了哪些常用的interceptor,充分利用工具。

·        Interceptor的执行顺序是由xml文件中的配置顺序决定的。

·        Interceptor中可以做哪些事情:

Preprocessing.Prepare,  filter,  alter, log , time,  authority.

Pass thecontrol on to the successive or divert the execution by itself.

Postprocessing.

·        自定义interceptor时,intercept方法中,在调用了invoke方法之后,得到的result并不能说明action是否被执行,有可能是deeperaction alter 了workflow。在此时也不能够拦截response,因为result已经render并send toclient了。

·        因为拦截器是有顺序执行的,所以如果两个拦截器都给同一个名称的属性负责,则后执行interceptor会override之前的值。

·        通过配置interceptor的excludeMethod参数来避免不必要的方法验证。第一次访问一个action时,由于表单是autocreated,所以不需要验证,这时候可以通过给interceptor添加参数someMethod来解决。

·        常用的拦截器:token,exception,exeAndWait

·        ExceptionInterceptor的使用:在default package 里面定义一个global-results,并添加一个global-exception-mappings。可以为不同类型的exception指定不同的result.

·        Interceptor的配置。Specific interceptors, writestacks, pass parameters to interceptor,specific an interceport for an action.

·        Inteceptordeclarations. Interceport需要定义在package标签里。

·        。?指至多一个,这里“,”指明的标签出现的顺序。

·        inteceptor的定义:

·        可以为specific action指定interceptor-ref,这个ref即可以是单独的interceptor也可以是一个interceptor-stack,可以只当前package中所声明的,也可以是parentpackage中声明的。

·        为interceptor传递参数。

? 第一种,直接修改某个interceptor的参数

input,validate

?  第二种,通过interceptor-stack间接设置,如name=”workflow.excludeMethods”

·        自定义Interceptor,可以直接实现Interceptor的interface,为了支持参数,可以从MethodFilterInterceptor继承。

Struts2 JSON

·        Struts2提供了JSON的插件,在其lib目录下可以找到。google JSON plugin即可找到对应的参考文档。

·        It seemsthat unused interceptors does not affect the performance so much.

·