1. Struts is integrated into container in web.xml as an filter. It is different from Spring, which is an Listener.
<?xml version="1.0" encoding="UTF-8"> ... <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-patter> </filter-mapping> ... </xml>
2. Struts uses JavaBean to carry the data from JSP to Action and vice versa. The JavaBean is defined as properties and its set and get methods.
After transferred with the request, the data from JSP is stored in ValueStack and in the action (Struts initializes one action object for each request. In contrast, the container initializes only one servlet to serve all the requests).
3. When we do not define actions in struts.xml, we want instead, use annotation in java to do so. By doning this, there is no below definition in struts.xml
<action name="Register" class="manning.Register"> <result>/RegistrationSuccess.jsp</result> <result name="input">/Registration.jsp</result> </action>
as we lost the action -> class mapping, we need to specify the default location to find the action classes, so we tell struts where to find the annotation:
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>manning</param-value> </init-param> </filter>
The framework looks for classes either implement Action interface or named XXXAction for actions.
5. struts-default package provides lots of useful intercepter and etc. Normally, our action package should be inheriting it in 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.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="Menu"> <result>/menu/Menu.jsp</result> </action> </package> <include file="manning/chapterTwo/chapterTwo.xml"/> <include file="manning/chapterThree/chapterThree.xml"/> . . . <include file="manning/chapterEight/chapterEight.xml"/> </struts>
6. Action 通过 extend ActionSupport来继承default execute()实现. ActionSupport相当于java里的wrapper类. 同时ActionSupport还提供很多有用的方法, 比如配合ValidationInterceptor的addFieldError()方法.
7. ValidationIntercepter如果发现有error, 将redirect the workflow到action的"input" result.
Filter VS Action
- Filter只能执行定义在filterclass的logic. 而Intercepter能够执行action class里面的方法. e.g. the public void validate() 方法.
8. Struts implements i18N with ActionSupport's TextProvider.
9. Model driver could be used to transfer obejct between Page and Actions.
10. ParameterIntercepter passes params from Page to action & ValueStack. (what is relationship between parameterintercepter and OGNL?)
11. FileUploadIntercepter gets run before ParamIntercepter. It converts File into Parameters:
<h4>Complete and submit the form to create your own portfolio.</h4> <s:form action="ImageUpload" method="post" enctype="multipart/form-data"> <s:file name="pic" label="Picture"/> <s:submit/> </s:form>
? Where is the type of file
? what is the path of the "pic" file.
12. OGNL = expression (in JSP) + data type converter(struts)
13. steps to define customized OGNL converter
1) write a class to extend StrutsTypeConverter's two abstract methods:
public abstract Object convertFromString(Map context, String[] values, Class toClass); public abstract String convertToString(Map context, Object o);
2) connect the field name in <s:textfield name="XXX", label="..."/> to class in .property file: XXX=ConverterClassName
to be continued...
struts 2 interview questions: http://www.journaldev.com/2354/struts2-interview-questions-and-answers#custom-interceptor
相关推荐
Struts 2实战 struts2 in Action
Struts2 in action 中文版
struts2 in action 源码
Struts2inAction 英文版 非常实用的一本Struts教材
Struts 2实战,Struts 2 in action 的中文版,pdf格式,讲的是Struts 2。此书是Struts 2的一本很经典的书。花几块钱在网上买的。。。。
Struts2 In Action中文版,完整PDF。
学习Struts2第三天笔记
11.1.2 Struts 2如何解决本地Java对i18n支持的问题 243 11.2 Struts 2 i18n示例 244 11.2.1 Struts 2 i18n快速展示 244 11.2.2 幕后一览 246 11.3 Struts 2 i18n详情 247 11.3.1 Struts 2默认的TextProvider Re-...
Struts2 in action 中文版.zip 经典的Struts2图书
Manning - Struts 2 in Action源码
struts2 in actionstruts2 in actionstruts2 in actionstruts2 in actionstruts2 in actionstruts2 in actionstruts2 in action
学习Struts2第四天笔记
Struts2 in action 中文版 很不错的一本书
Struts2_Action学习笔记、通配符{1},{2}......
这是学习struts2时记得重点笔记,包括了一些原理,ognl语句的编写,以及如何设置拦截器等等一些基本知识,起到复习和巩固的作用
如果你想自学struts2,此文档是很好的自学宝典,经典struts2 in Action,能帮你建立整体的struts2思想,你还在犹豫什么
Struts2 实战 Struts 2 in Action
Struts2作为javaweb开发的三大框架之一,很有必要了解其使用以及原理
Struts2实战(带标签)+随书配套源代码