struts2面试问题
Struts2 is one of the famous framework for developing web application in java. Recently I have wrote a lot of Struts2 Tutorials and in this post, I am listing down some of the important Struts2 interview questions with answers to help you in interview.
Struts2是用Java开发Web应用程序的著名框架之一。 最近,我写了很多Struts2教程 ,在这篇文章中,我列出了一些重要的Struts2面试问题以及答案,以帮助您进行面试。
Apache Struts2 is an open source framework to build web applications in Java. Struts2 is based on OpenSymphony WebWork framework. It’s highly improved from Struts1 and that makes it more flexible, easy to use and extend. The core components of Struts2 are Action, Interceptors and Result pages.
Struts2 provides many ways to create Action classes and configure them via struts.xml or through annotations. We can create our own interceptors for common tasks. Struts2 comes with a lot of tags and uses OGNL expression language. We can create our own type converters to render result pages. Result pages can be JSPs and FreeMarker templates.
Apache Struts2是一个开放源代码框架,可以用Java构建Web应用程序。 Struts2基于OpenSymphony WebWork框架。 它比Struts1有了很大的改进,使其更加灵活,易于使用和扩展。 Struts2的核心组件是“操作”,“拦截器”和“结果”页面。
Struts2提供了许多方法来创建Action类并通过struts.xml或通过注释对其进行配置。 我们可以为常见任务创建自己的拦截器。 Struts2带有很多标签,并使用OGNL表达式语言。 我们可以创建自己的类型转换器来呈现结果页面。 结果页面可以是JSP和FreeMarker模板。
Struts2 is designed to overcome the shortcomings of Struts1 and to make it more flexible, extendable. Some of the noticeable differences are:
Components | Struts1 | Struts2 |
---|---|---|
Action Classes | Struts1 action classes are forced to extend an Abstract Class that makes it not extendable. | Struts2 action classes flexible and we can create them by implementing Action interface, extending ActionSupport class or just by having execute() method. |
Thread Safety | Struts1 Action Classes are Singleton and not thread safe, that makes extra care on developer side to avoid any side effects because of multithreading. | Struts2 action classes gets instantiated per request, so there is no multithreading and makes them thread safe. |
Servlet API coupling | Struts1 APIs are tightly coupled with Servlet API and Request and Response objects are passed to action classes execute() method. | Struts2 API is loosely coupled with Servlet API and automatically maps the form bean data to action class java bean properties that we mostly use. If however we need reference to Servlet API classes, there are *Aware interfaces for that. |
Testing | Struts1 action classes are hard to test because of Servlet API coupling. | Struts2 Action classes are like normal java classes and we can test them easily by instantiating them and setting their properties. |
Request Parameters mapping | Struts1 requires us to create ActionForm classes to hold request params and we need to configure it in the struts configuration file. | Struts2 request params mapping is done on the fly and all we need is to have java bean properties in action classes or implement ModelDriven interface to provide the java bean class name to be used for mapping. |
Tag Support | Struts1 uses JSTL Tags and hence are limited. | Struts2 uses OGNL and provide different kinds of UI, Control and Data Tags. It’s more versatile and easy to use. |
Validation | Struts1 supports validation through manual validate() method | Struts2 support both manual validation as well as Validation framework integration. |
Views Rendering | Struts1 uses standard JSP technology for providing bean values to JSP pages for views. | Struts2 uses ValueStack to store request params and attributes and we can use OGNL and Struts2 tags to access them. |
Modules support | Struts1 modules are complex to design and looks like separate projects | Struts2 provides “namespace” configuration for packages to easily create modules. |
Struts2旨在克服Struts1的缺点,并使其更加灵活,可扩展。 一些明显的区别是:
组件 | Struts1 | Struts2 |
---|---|---|
动作班 | Struts1动作类被强制扩展一个Abstract Class,使其无法扩展。 | Struts2动作类非常灵活,我们可以通过实现Action接口,扩展ActionSupport类或仅具有execute()方法来创建它们。 |
线程安全 | Struts1操作类是Singleton且不是线程安全的,因此在开发人员方面要格外小心,以避免由于多线程而产生任何副作用。 | 每个请求都会实例化Struts2动作类,因此不存在多线程处理并使它们成为线程安全的。 |
Servlet API耦合 | Struts1 API与Servlet API紧密耦合,并且Request和Response对象传递给操作类的execute()方法。 | Struts2 API与Servlet API松散耦合,并自动将表单bean数据映射到我们最常用的动作类java bean属性。 但是,如果需要引用Servlet API类,则可以使用* Aware接口。 |
测试中 | 由于Servlet API耦合,Struts1动作类很难测试。 | Struts2 Action类就像普通的Java类一样,我们可以通过实例化它们并设置它们的属性来轻松地对其进行测试。 |
请求参数映射 | Struts1要求我们创建ActionForm类来保存请求参数,并且需要在struts配置文件中对其进行配置。 | Struts2请求参数映射是动态完成的,我们需要做的是在动作类中具有Java Bean属性,或者实现ModelDriven接口以提供用于映射的Java Bean类名称。 |
标签支持 | Struts1使用JSTL标签,因此受到限制。 | Struts2使用OGNL并提供各种UI,控件和数据标签。 它更加通用且易于使用。 |
验证方式 | Struts1支持通过手动validate()方法进行验证 | Struts2支持手动验证以及验证框架集成。 |
视图渲染 | Struts1使用标准的JSP技术为JSP页面提供bean值以供查看。 | Struts2使用ValueStack存储请求参数和属性,我们可以使用OGNL和Struts2标签访问它们。 |
模块支持 | Struts1模块设计复杂,看起来像单独的项目 | Struts2为软件包提供“命名空间”配置,以轻松创建模块。 |
Struts2 core components are:
Struts2的核心组件是:
Interceptors are the backbone of Struts2 Framework. Struts2 interceptors are responsible for most of the processing done by the framework, such as passing request params to action classes, making Servlet API request, response, session available to Action classes, validation, i18n support, etc.
ActionInvocation is responsible to incapsulate Action classes and interceptors and to fire them in order. The most important method for use in ActionInvocation is invoke() method that keeps track of the interceptor chain and invokes the next interceptor or action. This is one of the best example of Chain of Responsibility pattern in Java EE frameworks.
拦截器是Struts2 Framework的骨干。 Struts2拦截器负责框架完成的大多数处理,例如将请求参数传递给动作类,使Servlet API请求,响应,可用于动作类的会话,验证,i18n支持等。
ActionInvocation负责封装Action类和拦截器,并按顺序触发它们。 在ActionInvocation中使用的最重要的方法是invoke()方法,该方法跟踪拦截器链并调用下一个拦截器或操作。 这是Java EE框架中“责任链”模式的最佳示例之一。
Struts2 interceptors are based on intercepting filters design pattern. The invocation of interceptors in interceptor stack closely resembles Chain of Responsibility design pattern.
Struts2拦截器基于拦截过滤器设计模式。 拦截器堆栈中拦截器的调用非常类似于“责任链”设计模式。
Struts2 provide different ways to create action classes.
Struts2提供了创建动作类的不同方法。
Struts2 Action classes are thread safe because an object is instantiated for every request to handle it.
Struts2 interceptors are singleton classes and a new thread is created to handle the request, so it’s not thread safe and we need to implement them carefully to avoid any issues with shared data.
Struts2 Action类是线程安全的,因为会为处理该请求的每个请求实例化一个对象。
Struts2拦截器是单例类,并且创建了一个新线程来处理请求,因此它不是线程安全的,我们需要仔细实现它们,以避免共享数据出现任何问题。
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
is the Front Controller class in Struts2 and every request processing starts from this class. Earlier versions of Struts2 uses org.apache.struts2.dispatcher.FilterDispatcher
as Front Controller class.
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
是Struts2中的Front Controller类,每个请求处理都从该类开始。 早期版本的Struts2使用org.apache.struts2.dispatcher.FilterDispatcher
作为Front Controller类。
Some of the benefits of interceptors are:
拦截器的一些好处是:
ValueStack is the storage area where the application data is stored by Struts2 for processing the client requests. The data is stored in ActionContext
objects that use ThreadLocal to have values specific to the particular request thread.
Object-Graph Navigation Language (OGNL) is a powerful Expression Language that is used to manipulate data stored on the ValueStack. As you can see in architecture diagram, both interceptors and result pages can access data stored on ValueStack using OGNL.
ValueStack是Struts2在其中存储应用程序数据以处理客户端请求的存储区域。 数据存储在使用ThreadLocal的ActionContext
对象中,该对象具有特定于特定请求线程的值。
对象图导航语言(OGNL)是一种功能强大的表达语言,用于处理存储在ValueStack上的数据。 从体系结构图中可以看到,拦截器和结果页面都可以使用OGNL访问存储在ValueStack上的数据。
Some of the important annotations introduced in Struts2 are:
Struts2中引入的一些重要注释是:
Some of the Struts2 constants that I have used are:
We can configure above constants in the struts.xml file like below.
我使用的一些Struts2常量是:
我们可以在struts.xml文件中配置上述常量,如下所示。
Struts2 namespace configuration allows us to create modules easily. We can use namespace to separate our action classes based on their functionality, for example admin, user, customer etc.
Struts2名称空间配置使我们可以轻松创建模块。 我们可以使用名称空间根据操作类的功能来分隔它们,例如admin,user,customer等。
com.opensymphony.xwork2.interceptor.ParametersInterceptor
interceptor is responsible for mapping request parameters to the Action class java bean properties. This interceptor is configured in struts-default package with name “params”. This interceptor is part of basicStack and defaultStack interceptors stack.
com.opensymphony.xwork2.interceptor.ParametersInterceptor
拦截器负责将请求参数映射到Action类java bean属性。 该拦截器在struts-default软件包中配置为“ params”。 此拦截器是basicStack和defaultStack拦截器堆栈的一部分。
com.opensymphony.xwork2.interceptor.I18nInterceptor
interceptor is responsible for i18n support in Struts2 applications. This interceptor is configured in struts-default package with name “i18n” and it’s part of i18nStack and defaultStack.
com.opensymphony.xwork2.interceptor.I18nInterceptor
拦截器负责Struts2应用程序中的i18n支持。 该拦截器在名为“ i18n”的struts-default软件包中配置,并且是i18nStack和defaultStack的一部分。
We can implement Action interface to create our action classes. This interface has a single method execute() that we need to implement. The only benefit of using this interface is that it contains some constants that we can use for result pages, these constants are SUCCESS, ERROR, NONE, INPUT and LOGIN.
ActionSupport class is the default implementation of Action interface and it also implements interfaces related to Validation and i18n support. ActionSupport class implements Action, Validateable, ValidationAware, TextProvider and LocaleProvider interfaces. We can override the validate() method of ActionSupport class to include field level validation login in our action classes.
Depending on the requirements, we can use any of the approaches to creating Struts 2 action classes, my favorite is ActionSupport class because it helps in writing validation and i18n logic easily in action classes.
我们可以实现Action接口来创建我们的动作类。 该接口具有我们需要实现的单个方法execute()。 使用此接口的唯一好处是它包含一些可用于结果页的常量,这些常量是SUCCESS,ERROR,NONE,INPUT和LOGIN。
ActionSupport类是Action接口的默认实现,它也实现与Validation和i18n支持相关的接口。 ActionSupport类实现Action,Validateable,ValidationAware,TextProvider和LocaleProvider接口。 我们可以重写ActionSupport类的validate()方法以在我们的操作类中包括字段级验证登录。
根据需求,我们可以使用任何方法来创建Struts 2动作类,我最喜欢的是ActionSupport类,因为它有助于在动作类中轻松编写验证和i18n逻辑。
Struts2 action classes don’t provide direct access to Servlet API components such as Request, Response, and Session. However, sometimes we need these access in action classes such as checking HTTP method or setting cookies in response.
That’s why Struts2 API provides a bunch of *Aware interfaces that we can implement to access these objects. Struts2 API uses dependency injection to inject Servlet API components in action classes. Some of the important Aware interfaces are SessionAware, ApplicationAware, ServletRequestAware, and ServletResponseAware.
You can read more about them in How to get Servlet API Session in Struts2 Action Classes tutorial.
Struts2操作类不提供对Servlet API组件(例如请求,响应和会话)的直接访问。 但是,有时我们需要在操作类中进行这些访问,例如检查HTTP方法或设置cookie作为响应。
因此,Struts2 API提供了一堆* Aware接口,我们可以实现这些接口来访问这些对象。 Struts2 API使用依赖注入将Servlet API组件注入到操作类中。 一些重要的Aware接口是SessionAware,ApplicationAware,ServletRequestAware和ServletResponseAware。
您可以在Struts2 Action Classes教程中的如何获取Servlet API会话中了解有关它们的更多信息。
Struts2 provides execAndWait interceptor for long running action classes. We can use this interceptor to return an intermediate response page to the client and once the processing is finished, final response is returned to the client. This interceptor is defined in the struts-default package and implementation is present in ExecuteAndWaitInterceptor
class.
Check out Struts2 execAndWait interceptor example to learn more about this interceptor and how to use it.
Struts2为长时间运行的动作类提供了execAndWait拦截器。 我们可以使用此拦截器将中间响应页面返回给客户端,一旦处理完成,最终响应将返回给客户端。 该拦截器在struts-default包中定义,实现在ExecuteAndWaitInterceptor
类中提供。
查看Struts2 execAndWait拦截器示例,以了解有关此拦截器以及如何使用它的更多信息。
One of the major problems with web applications is the double form submission. If not taken care, double form submission could result in charging double amount to customer or updating database values twice. We can use a token interceptor to solve the double form submission problem. This interceptor is defined in the struts-default package but it’s not part of any interceptor stack, so we need to include it manually in our action classes.
Read more at Struts2 token interceptor example.
Web应用程序的主要问题之一是双重表单提交。 如果不小心,重复提交表单可能导致向客户收取双倍的费用或两次更新数据库值。 我们可以使用令牌拦截器来解决双重表单提交问题。 该拦截器是在struts-default包中定义的,但它不是任何拦截器堆栈的一部分,因此我们需要在操作类中手动添加它。
在Struts2令牌拦截器示例中了解更多信息。
Struts2 provides easy integration of log4j API for logging purpose, all we need to have is log4j configuration file in the WEB-INF/classes directory.
You can check out the sample project at Struts2 Log4j integration.
Struts2为日志目的提供了log4j API的轻松集成,我们所需要的只是WEB-INF / classes目录中的log4j配置文件。
您可以在Struts2 Log4j集成中签出示例项目。
Struts2 provides a lot of custom tags that we can use in result pages to create views for client request. These tags are broadly divided into three categories- Data tags, Control tags and UI tags.
We can use these tags by adding these in JSP pages using taglib directive.
<%@ taglib uri="/struts-tags" prefix="s" %>
Some of the important Data tags are property, set, push, bean, action, include, i18n and text tag. Read more at Struts2 Data Tags.
Control tags are used for manipulation and navigation of data from a collection. Some of the important Control tags are if-elseif-else, iterator, append, merge, sort, subset and generator tag. Read more at Struts2 Control Tags.
Struts2 UI tags are used to generate HTML markup language, binding HTML form data to action classes properties, type conversion, validation, and i18n support. Some of the important UI tags are form, textfield, password, textarea, checkbox, select, radio and submit tag. Read more about them at Struts2 UI Tags.
Struts2提供了许多自定义标签,我们可以在结果页中使用它们来创建客户端请求的视图。 这些标签大致分为三类-数据标签,控制标签和UI标签。
我们可以通过使用taglib指令在JSP页面中添加这些标记来使用这些标记。
一些重要的数据标签是属性,设置,推送,Bean,操作,包含,i18n和文本标签。 在Struts2数据标签中了解更多信息 。
控制标签用于操作和导航集合中的数据。 一些重要的控制标签是if-elseif-else,迭代器,附加,合并,排序,子集和生成器标签。 在Struts2控制标签上了解更多信息。
Struts2 UI标记用于生成HTML标记语言,将HTML表单数据绑定到动作类属性,类型转换,验证和i18n支持。 一些重要的UI标签是表单,文本字段,密码,文本区域,复选框,选择,广播和提交标签。 在Struts2 UI标签上阅读有关它们的更多信息。
Struts2 support OGNL expression language and it performs two important tasks in Struts 2 – data transfer and type conversion.
OGNL is flexible and we can easily extend it to create our own custom converter class. Creating and configuring custom type converter class is very easy. The first step is to fix the input format for the custom class. The second step is to implement the converter class. Type converter classes should implement com.opensymphony.xwork2.conversion.TypeConverter
interface.
Since in web application, we always get the request in form of String and send the response in the form of String, Struts 2 API provides a default implementation of TypeConverter interface, StrutsTypeConverter. StrutsTypeConverter contains two abstract methods – convertFromString to convert String to Object and convertToString to convert Object to String.
For implementation details, read Struts2 OGNL Example Tutorial.
Struts2支持OGNL表达式语言,它在Struts 2中执行两项重要任务-数据传输和类型转换。
OGNL非常灵活,我们可以轻松地对其进行扩展以创建自己的自定义转换器类。 创建和配置自定义类型转换器类非常容易。 第一步是修复自定义类的输入格式。 第二步是实现转换器类。 类型转换器类应实现com.opensymphony.xwork2.conversion.TypeConverter
接口。
由于在Web应用程序中,我们总是以String形式获取请求,并以String形式发送响应,因此Struts 2 API提供了TypeConverter接口的默认实现StrutsTypeConverter。 StrutsTypeConverter包含两个抽象方法– convertFromString将String转换为Object,convertToString将Object转换为String。
有关实现的详细信息,请阅读Struts2 OGNL示例教程 。
We can implement com.opensymphony.xwork2.interceptor.Interceptor
interface to create our own interceptor. Once the interceptor class is ready, we need to define that in struts.xml package where we want to use it. We can also create interceptor stack with our custom interceptor and defaultStack interceptors. After that we can configure it for action classes where we want to use our interceptor.
One of the best example of using custom interceptor is to validate session, read more about it at Struts2 Interceptor Tutorial.
我们可以实现com.opensymphony.xwork2.interceptor.Interceptor
接口来创建我们自己的拦截器。 拦截器类准备好后,我们需要在struts.xml包中定义要使用它的位置。 我们还可以使用我们的自定义拦截器和defaultStack拦截器创建拦截器堆栈。 之后,我们可以为要使用拦截器的动作类配置它。
使用自定义拦截器的最佳示例之一是验证会话,请在Struts2 Interceptor Tutorial中阅读有关会话的更多信息。
Interceptor interface defines three methods – init(), destroy() and intercept(). init and destroy are the life cycle methods of an interceptor. Interceptors are Singleton classes and Struts2 initialize a new thread to handle each request. init() method is called when interceptor instance is created and we can initialize any resources in this method. destroy() method is called when application is shutting down and we can release any resources in this method.
intercept() is the method called every time client request comes through the interceptor.
拦截器接口定义了三种方法-init(),destroy()和intercept()。 初始化和销毁是拦截器的生命周期方法。 拦截器是Singleton类,Struts2初始化一个新线程来处理每个请求。 创建拦截器实例时将调用init()方法,我们可以在此方法中初始化任何资源。 当应用程序关闭时会调用destroy()方法,我们可以使用此方法释放任何资源。
Intercept()是每次客户端请求通过拦截器时调用的方法。
An interceptor stack helps us to group together multiple interceptors in a package for further use. struts-default package creates some of the mostly used interceptor stack – basicStack and defaultStack. We can create our own interceptor stack at the start of the package and then configure our action classes to use it.
拦截器堆栈可帮助我们将一个包装中的多个拦截器组合在一起以备将来使用。 struts-default软件包创建一些最常用的拦截器堆栈-basicStack和defaultStack。 我们可以在包的开头创建自己的拦截器堆栈,然后配置操作类以使用它。
struts-default is an abstract package that defines all the Struts2 interceptors and commonly used interceptor stack. It is advisable to extend this package while configuring our application package to avoid configuring interceptors again. This is provided to help developers by eliminating the trivial task of configuring interceptor and result pages in our application.
struts-default是一个抽象包,它定义了所有Struts2拦截器和常用的拦截器堆栈。 建议在配置我们的应用程序包时扩展此程序包,以避免再次配置拦截器。 通过消除在我们的应用程序中配置拦截器和结果页面的繁琐任务,可以帮助开发人员。
The default URI suffix for Struts2 action is .action, in Struts1 default suffix was .do. We can change this suffix by defining struts.action.extension constant value in our Struts2 configuration file as:
Struts2操作的默认URI后缀为.action,在Struts1中的默认后缀为.do。 我们可以通过在Struts2配置文件中定义struts.action.extension常量值来更改此后缀:
By default Struts2 looks for result pages in {WEBAPP-ROOT}/{Namespace}/ directory but sometimes we want to keep result pages in another location, we can provide struts.convention.result.path constant value in Struts2 configuration file to change the result pages location.
Another way is to use @ResultPath annotation in action classes to provide the result pages location.
默认情况下,Struts2在{WEBAPP-ROOT} / {Namespace} /目录中查找结果页面,但有时我们希望将结果页面保留在另一个位置,我们可以在Struts2配置文件中提供struts.convention.result.path常量值来更改结果页面位置。
另一种方法是在操作类中使用@ResultPath批注提供结果页面的位置。
File Upload is one of the common tasks in a web application. That’s why Struts2 provides built-in support for file upload through FileUploadInterceptor. This interceptor is configured in the struts-default package and provide options to set the maximum size of a file and file types that can be uploaded to the server.
Read more about FileUpload interceptor at Struts2 File Upload Example.
文件上传是Web应用程序中的常见任务之一。 这就是Struts2为通过FileUploadInterceptor上传文件提供内置支持的原因。 该拦截器在struts-default软件包中配置,并提供选项来设置文件的最大大小和可以上载到服务器的文件类型。
在Struts2文件上传示例中阅读有关FileUpload拦截器的更多信息。
Some of the best practices while developing Struts2 application are:
开发Struts2应用程序时的一些最佳实践是:
Struts2 provides a very robust framework for exception handling. We can specify global results in packages and then map specific exceptions to these result pages. The exception mapping can be done at the global package level as well as the action level.
It’s a good idea to have exception result pages to provide some information to the user when some unexpected exception occurs that is not processed by the application. The sample configuration in the struts.xml file looks like below.
/exception.jsp
/runtime_exception.jsp
/error.jsp
/error.jsp
Read more at Struts2 Exception Handling Example.
Struts2提供了一个非常强大的异常处理框架。 我们可以在包中指定全局结果,然后将特定的异常映射到这些结果页面。 异常映射可以在全局包级别以及操作级别完成。
最好让异常结果页面在应用程序未处理的某些意外异常发生时向用户提供一些信息。 struts.xml文件中的示例配置如下所示。
在Struts2异常处理示例中了解更多信息。
That’s all for the Struts2 interview question and answers, if you come across any important question that I have missed, please let me know through comments.
这就是Struts2面试问答的全部内容,如果您遇到任何我遗漏的重要问题,请通过评论告知我。
翻译自: https://www.journaldev.com/2354/struts2-interview-questions-and-answers
struts2面试问题