`
自治州
  • 浏览: 34927 次
  • 性别: Icon_minigender_1
  • 来自: 奥克兰
社区版块
存档分类
最新评论

Struts2 In Action读书笔记以及面试常见问题

 
阅读更多

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

 

  • 大小: 6.5 KB
  • 大小: 47.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics