struts2面试问题_Struts2面试问答

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面试问题以及答案,以帮助您进行面试。

Struts2面试题 (Struts2 Interview Questions)

  1. What is Struts2?

    什么是Struts2?
  2. What are the differences between Struts1 and Struts2 or how Struts2 is better than Struts1?

    Struts1和Struts2之间有什么区别,或者Struts2比Struts1更好?
  3. What are Struts2 core components?

    什么是Struts2核心组件?
  4. What is interceptor in Struts2?

    Struts2中的拦截器是什么?
  5. Which design pattern is implemented by Struts2 interceptors?

    Struts2拦截器实现哪种设计模式?
  6. What are different ways to create Action classes in Struts2?

    在Struts2中创建Action类的不同方式有哪些?
  7. Does Struts2 action and interceptors are thread safe?

    Struts2动作和拦截器是否是线程安全的?
  8. Which class is the Front Controller in Struts2?

    Struts2中的Front Controller是哪个类?
  9. What are the benefits of Interceptors in Struts2?

    Struts2中的Interceptor有什么好处?
  10. What is ValueStack and OGNL?

    什么是ValueStack和OGNL?
  11. Name some useful annotations introduced in Struts2?

    命名一些在Struts2中引入的有用注释?
  12. Provide some important Struts2 constants that you have used?

    提供一些重要的Struts2常量?
  13. What is the use of namespace in action mapping in Struts2?

    Struts2中的动作映射中名称空间的用途是什么?
  14. Which interceptor is responsible for mapping request parameters to action class Java Bean properties?

    哪个拦截器负责将请求参数映射到动作类Java Bean属性?
  15. Which interceptor is responsible for i18n support?

    哪个拦截器负责i18n支持?
  16. What is the difference in using Action interface and ActionSupport class for our action classes, which one you would prefer?

    将Action接口和ActionSupport类用于我们的操作类有什么区别,您希望使用哪种?
  17. How can we get Servlet API Request, Response, HttpSession etc Objects in action classes?

    我们如何在动作类中获取Servlet API请求,响应,HttpSession等对象?
  18. What is the use of execAndWait interceptor?

    execAndWait拦截器有什么用?
  19. What is the use of token interceptor in Struts2?

    Struts2中的令牌拦截器有什么用?
  20. How can we integrate log4j in Struts2 application?

    我们如何将Log4j集成到Struts2应用程序中?
  21. What are different Struts2 tags? How can we use them?

    有什么不同的Struts2标签? 我们如何使用它们?
  22. What is Custom Type Converter in Struts2?

    Struts2中的自定义类型转换器是什么?
  23. How can we write our own interceptor and map it for action?

    我们如何编写自己的拦截器并将其映射为行动?
  24. What is life cycle of an interceptor?

    拦截器的生命周期是多少?
  25. What is an interceptor stack?

    什么是拦截器堆栈?
  26. What is struts-default package and what are it’s benefits?

    什么是struts-default软件包,它有什么好处?
  27. What is the default suffix for Struts2 action URI and how can we change it?

    Struts2操作URI的默认后缀是什么,我们如何更改它?
  28. What is the default location of result pages and how can we change it?

    结果页的默认位置是什么,我们如何更改它?
  29. How can we upload files in Struts2 application?

    我们如何在Struts2应用程序中上传文件?
  30. What are best practices to follow while developing Struts2 application?

    开发Struts2应用程序时应遵循的最佳实践是什么?
  31. How can we handle exceptions thrown by application in Struts2?

    我们如何处理Struts2中应用程序引发的异常?

Struts2面试问答 (Struts2 Interview Questions and Answers)

  1. 什么是Struts2? (What is 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模板。

  2. Struts1和Struts2之间有什么区别,或者Struts2比Struts1更好? (What are the differences between Struts1 and Struts2 or how Struts2 is better than Struts1?)

    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为软件包提供“命名空间”配置,以轻松创建模块。
  3. 什么是Struts2核心组件? (What are Struts2 core components?)

    Struts2 core components are:

    1. Action Classes
    2. Interceptors
    3. Result Pages, JSP of FreeMarker templates
    4. ValueStack, OGNL and Tag Libraries
    struts2面试问题_Struts2面试问答_第1张图片

    Struts 2 Architecture Diagram

    Struts2的核心组件是:

    1. 动作班
    2. 拦截器
    3. 结果页面,FreeMarker模板的JSP
    4. ValueStack,OGNL和标签库

    Struts 2架构图

  4. Struts2中的拦截器是什么? (What is interceptor in 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框架中“责任链”模式的最佳示例之一。

  5. Struts2拦截器实现哪种设计模式? (Which design pattern is implemented by Struts2 interceptors?)

    Struts2 interceptors are based on intercepting filters design pattern. The invocation of interceptors in interceptor stack closely resembles Chain of Responsibility design pattern.

    Struts2拦截器基于拦截过滤器设计模式。 拦截器堆栈中拦截器的调用非常类似于“责任链”设计模式。

  6. 在Struts2中创建Action类的不同方式有哪些? (What are different ways to create Action classes in Struts2?)

    Struts2 provide different ways to create action classes.

    1. By implementing Action interface
    2. Using Struts2 @Action annotation
    3. By extending ActionSupport class
    4. Any normal java class with execute() method returning String can be configured as Action class.

    Struts2提供了创建动作类的不同方法。

    1. 通过实现动作界面
    2. 使用Struts2 @Action注释
    3. 通过扩展ActionSupport类
    4. 任何将execute()方法返回String的普通Java类都可以配置为Action类。
  7. Struts2动作和拦截器是否是线程安全的? (Does Struts2 action and interceptors are thread safe?)

    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拦截器是单例类,并且创建了一个新线程来处理请求,因此它不是线程安全的,我们需要仔细实现它们,以避免共享数据出现任何问题。

  8. Struts2中的Front Controller是哪个类? (Which class is the Front Controller in 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类。

  9. Struts2中的Interceptor有什么好处? (What are the benefits of Interceptors in Struts2?)

    Some of the benefits of interceptors are:

    • Interceptor plays a crucial role in achieving high level of separation of concerns.
    • Struts2 interceptors are configurable, we can configure it for any action we want.
    • We can create our own custom interceptors to perform some common tasks such as request params logging, authentication etc. This helps us in taking care of common tasks at a single location, achieving low maintenance cost.
    • We can create interceptors stack to use with different actions.

    拦截器的一些好处是:

    • 拦截器在实现高级别关注点分离方面起着至关重要的作用。
    • Struts2拦截器是可配置的,我们可以为所需的任何操作配置它。
    • 我们可以创建自己的自定义拦截器来执行一些常见任务,例如请求参数记录,身份验证等。这有助于我们在单个位置处理常见任务,从而降低了维护成本。
    • 我们可以创建拦截器堆栈以用于不同的动作。
  10. 什么是ValueStack和OGNL? (What is ValueStack and OGNL?)

    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上的数据。

  11. 命名一些在Struts2中引入的有用注释? (Name some useful annotations introduced in Struts2?)

    Some of the important annotations introduced in Struts2 are:

    1. @Action to create action class
    2. @Actions to configure single class for multiple actions
    3. @Namespace and @Namespaces for creating different modules
    4. @Result for result pages
    5. @ResultPath for configuring result pages location

    Struts2中引入的一些重要注释是:

    1. @Action创建动作类
    2. @Actions为多个动作配置单个类
    3. @Namespace和@Namespaces用于创建不同的模块
    4. 结果页的@Result
    5. @ResultPath用于配置结果页面的位置
  12. 提供一些重要的Struts2常量? (Provide some important Struts2 constants that you have used?)

    Some of the Struts2 constants that I have used are:

    1. struts.devMode to run our application in development mode. This mode does reload properties files and provides extra logging and debugging feature. It’s very useful while developing our application but we should turn it off while moving our code to production.
    2. struts.convention.result.path to configure the location of result pages. By default Struts2 look for result pages at {WEBAPP-ROOT}/{Namespace}/ and we can change the location with this constant.
    3. struts.custom.i18n.resources to define global resource bundle for i18n support.
    4. struts.action.extension to configure the URL suffix to for Struts2 application. Default suffix is .action but sometimes we might want to change it to .do or something else.

    We can configure above constants in the struts.xml file like below.

    
    
    
    

    我使用的一些Struts2常量是:

    1. struts.devMode以开发模式运行我们的应用程序。 此模式不会重新加载属性文件,并提供额外的日志记录和调试功能。 这在开发应用程序时非常有用,但是在将代码移至生产环境时应将其关闭。
    2. struts.convention.result.path来配置结果页面的位置。 默认情况下,Struts2在{WEBAPP-ROOT} / {Namespace} /中查找结果页面,我们可以使用此常量更改位置。
    3. struts.custom.i18n.resources定义用于i18n支持的全局资源束。
    4. struts.action.extension为Struts2应用程序配置URL后缀。 默认后缀为.action,但有时我们可能希望将其更改为.do或其他名称。

    我们可以在struts.xml文件中配置上述常量,如下所示。

  13. Struts2中的动作映射中名称空间的用途是什么? (What is the use of namespace in action mapping in Struts2?)

    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等。

  14. 哪个拦截器负责将请求参数映射到动作类Java Bean属性? (Which interceptor is responsible for mapping request parameters to action class Java Bean properties?)

    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拦截器堆栈的一部分。

  15. 哪个拦截器负责i18n支持? (Which interceptor is responsible for i18n support?)

    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的一部分。

  16. 将Action接口和ActionSupport类用于我们的操作类有什么区别,您希望使用哪种? (What is the difference in using Action interface and ActionSupport class for our action classes, which one you would prefer?)

    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逻辑。

  17. 我们如何在动作类中获取Servlet API请求,响应,HttpSession等对象? (How can we get Servlet API Request, Response, HttpSession etc Objects in action classes?)

    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会话中了解有关它们的更多信息。

  18. execAndWait拦截器有什么用? (What is the use of execAndWait interceptor?)

    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拦截器示例,以了解有关此拦截器以及如何使用它的更多信息。

  19. Struts2中的令牌拦截器有什么用? (What is the use of token interceptor in Struts2?)

    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令牌拦截器示例中了解更多信息。

  20. 我们如何将Log4j集成到Struts2应用程序中? (How can we integrate log4j in Struts2 application?)

    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集成中签出示例项目。

  21. 有什么不同的Struts2标签? 我们如何使用它们? (What are different Struts2 tags? How can we use them?)

    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标签上阅读有关它们的更多信息。

  22. Struts2中的自定义类型转换器是什么? (What is Custom Type Converter in Struts2?)

    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示例教程 。

  23. 我们如何编写自己的拦截器并将其映射为行动? (How can we write our own interceptor and map it for action?)

    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中阅读有关会话的更多信息。

  24. 拦截器的生命周期是多少? (What is life cycle of an interceptor?)

    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()是每次客户端请求通过拦截器时调用的方法。

  25. 什么是拦截器堆栈? (What is an interceptor stack?)

    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。 我们可以在包的开头创建自己的拦截器堆栈,然后配置操作类以使用它。

  26. 什么是struts-default软件包,它有什么好处? (What is struts-default package and what are it’s benefits?)

    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拦截器和常用的拦截器堆栈。 建议在配置我们的应用程序包时扩展此程序包,以避免再次配置拦截器。 通过消除在我们的应用程序中配置拦截器和结果页面的繁琐任务,可以帮助开发人员。

  27. Struts2操作URI的默认后缀是什么,我们如何更改它? (What is the default suffix for Struts2 action URI and how can we change it?)

    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常量值来更改此后缀:

  28. 结果页的默认位置是什么,我们如何更改它? (What is the default location of result pages and how can we change it?)

    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批注提供结果页面的位置。

  29. 我们如何在Struts2应用程序中上传文件? (How can we upload files in Struts2 application?)

    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拦截器的更多信息。

  30. 开发Struts2应用程序时应遵循的最佳实践是什么? (What are best practices to follow while developing Struts2 application?)

    Some of the best practices while developing Struts2 application are:

    1. Always try to extend struts-default package while creating your package to avoid code redundancy in configuring interceptors.
    2. For common tasks across the application, such as logging request params, try to use interceptors.
    3. Always keep action classes java bean properties in a separate bean for code reuse and implement ModelDriven interface.
    4. If you have custom interceptor that you will use in multiple actions, create interceptor stack for that and then use it.
    5. Try to divide your application in different modules with namespace configuration based on functional areas.
    6. Try to use Struts2 tags in result pages for code clarify, if needed create your own type converters.
    7. Use development mode for faster development, however make sure production code doesn’t run in dev mode.
    8. Use Struts2 i18n support for resource bundles and to support localization.
    9. Struts2 provides a lot of places where you can have resource bundles but try to keep one global resource bundle and one for action class to avoid confusion.
    10. struts-default package configures all the interceptors and creates different interceptor stacks. Try to use only what is needed, for example if you don’t have localization requirement, you can avoid i18n interceptor.

    开发Struts2应用程序时的一些最佳实践是:

    1. 创建软件包时,请始终尝试扩展struts-default软件包,以避免配置拦截器时的代码冗余。
    2. 对于整个应用程序中的常见任务,例如记录请求参数,请尝试使用拦截器。
    3. 始终将动作类的Java bean属性保留在单独的bean中,以供代码重用并实现ModelDriven接口。
    4. 如果您有要在多个操作中使用的自定义拦截器,请为此创建拦截器堆栈,然后使用它。
    5. 尝试根据功能区域使用名称空间配置将应用程序划分为不同的模块。
    6. 尝试在结果页面中使用Struts2标记以澄清代码,如果需要,请创建自己的类型转换器。
    7. 使用开发模式可以加快开发速度,但是请确保生产代码不在开发模式下运行。
    8. 将Struts2 i18n支持用于资源包并支持本地化。
    9. Struts2提供了许多地方,您可以在其中拥有资源包,但尝试保留一个全局资源包和一个动作类资源包,以避免混淆。
    10. struts-default软件包配置所有拦截器并创建不同的拦截器堆栈。 尝试仅使用所需的内容,例如,如果您没有本地化要求,则可以避免使用i18n拦截器。
  31. 我们如何处理Struts2中应用程序引发的异常? (How can we handle exceptions thrown by application in 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面试问题

你可能感兴趣的:(struts2面试问题_Struts2面试问答)