打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Struts学习笔记(一)

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执行之前做了一些preporcessingpostprocessing,如param interceptor。拦截器还可以alter the workflow of theinvocation

·        Interceptor的抽象增加了reuseconfigure的灵活性。启示:了解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已经rendersend toclient了。

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

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

·        常用的拦截器: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标签里。

·        <!ELEMENT package (result-types?, interceptors?,default-interceptor-ref?, default-action-ref?, default-class-ref?,global-results?, global-exception-mappings?, action*)>。?指至多一个,这里“,”指明的标签出现的顺序。

·        inteceptor的定义:<!ELEMENTinterceptors (interceptor|interceptor-stack)+>

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

·        interceptor传递参数。

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

<interceptor-refname=”workflow”>

<param name=”excludeMethods”>input,validate</param>

</interceptor-ref>

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

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

Struts2 JSON

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

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

·         

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
struts2.x的ActionContext,ValueStack和常用标签介绍
struts2值栈 ognl
Struts2中OGNL,valueStack,stackContext的学习
Struts2技术详解
Struts2 ONGL表达式
Struts2 面试题分析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服