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

本文深入探讨了Struts2框架的核心组件与工作原理,包括其在web应用中的部署配置、数据传输方式、注解使用、拦截器机制及国际化支持等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值