Sorting and sharing of interview questions related to struts 2

preface

As we all know, struts 2 is a very excellent open source framework. We can use the struts 2 framework for development and quickly build a struts 2 framework. However, whether we can express the working principle of the struts 2 framework clearly in language. You don't need to say how the bottom layer is implemented. I guess most people don't understand it. When the interviewer asks us how struts 2 works, how should we answer this question? What can you say to make yourself different? First look at what other friends have summarized

1、 Working principle

The processing in the struts 2 framework is roughly divided into the following steps

The client initializes a request to a servlet container (such as Tomcat)

2 the request passes through a series of filters (among these filters, there is an optional filter called actioncontextcleanup, which is very helpful for the integration of struts 2 and other frameworks, such as SiteMesh plugin)

3. Then the filterdispatcher is called. The filterdispatcher asks actionmapper to decide whether to call an action

4 if actionmapper decides to call an action, the filterdispatcher hands over the processing of the request to actionproxy

5. Actionproxy queries the configuration file of the framework through the configuration manager to find the action class to be called

6. Actionproxy creates an instance of actioninvocation.

7. The actioninvocation instance is called using the naming mode. Before and after the process of calling action, it involves the call of related interceptors.

8. Once the action is executed, actioninvocation is responsible for executing the action according to struts The configuration in XML finds the corresponding return result. The returned result is usually (but not always, or possibly another action chain) a JSP or FreeMarker template that needs to be represented. The tags inherited from the struts 2 framework can be used in the presentation process. Actionmapper needs to be involved in this process

II. Workflow

1. The client browser sends an HTTP request

2. According to web XML configuration, which is received by filterdispatcher

3. According to struts XML configuration, find the action class and method to be called, and inject the value into aciton through IOC

4. Action calls the business logic component to process the business logic. This step includes form verification.

5. After the action is executed, according to struts The configuration in XML finds the corresponding returned result and jumps to the corresponding page

6. Return HTTP response to client browser

3、 The difference between interceptors and filters

1. Interceptors are based on java reflection mechanism, while filters are based on function callbacks.

2. The filter depends on the servlet container, while the interceptor does not depend on the servlet container.

3. Interceptors can only work on action requests, while filters can work on almost all requests.

4. The interceptor can access the object in the action context and value stack, but the filter cannot.

5. In the life cycle of the action, the interceptor can be called multiple times, while the filter can only be called once when the container is initialized.

4、 What do you want to use struts 2

Struts 2 is a very powerful Java Web open source framework and an MVC web framework based on action of POJO. Based on the webwork and xwork framework of that year, it inherits its advantages and makes considerable improvements at the same time.

1. Struts 2 is based on MVC architecture. The framework structure is clear and the development process is clear at a glance. Developers can well control the development process.

2. Parameter passing using ognl.

Ognl provides a simple way to access data in various scopes in struts 2. You can easily obtain data in request, attribute, application, session and parameters. It greatly simplifies the amount of code developers use to obtain these data.

3. Powerful interceptor

The interceptor of struts 2 is an action level AOP. Many features in struts 2 are implemented through interceptors, such as exception handling, file upload, verification, etc. The interceptor is configurable and reusable. Some common functions such as login verification and permission verification can be placed in the interceptor to complete some common functions in Java Web projects. In a web project I implemented, I used the interceptor of struts 2 to complete the permission verification function in the system.

4. Easy to test

The actions of struts 2 are simple POJOs, which makes it easy to write test cases for the actions of struts 2 and greatly facilitates the testing of 5 Java Web projects.

Easy to extend plug-in mechanism adding extensions in struts 2 is a pleasant and easy thing. You only need to put the required jar package in the WEB-INF / lib folder in struts The extension can be realized by making some simple settings in XML.

6. Modular management

Struts 2 has taken modularization as the basic idea in the architecture. There are three ways to modularize the application: split the configuration information into multiple files, create self-contained application modules as plug-ins, create new framework features, that is, organize new functions unrelated to specific applications into plug-ins to add to multiple applications.

7. Global results and declarative exceptions

Add a global result for the application and handle the exception in the configuration file, so that when a specified exception occurs during processing, you can jump to a specific page.

So many of its advantages are favored by many people. It is combined with spring and hibernate to form a more popular SSH framework. Of course, each company needs its own framework, which is also a product of SSH variation.

5、 Seeing the netizens' summary of the principle of struts 2, I also summarized it myself for the subsequent interview. Here are my questions

1. The customer service side initiates a request and points to the Tomcat container through the HTTP protocol. What did Tomcat do when she got the request?

2. We web XML configuration

We can see org. Org from the web configuration file apache. struts2. dispatcher. ng. filter. Struts prepareandexecutefilter, what is the use of this filter class?

This / * intercepts all requests. What does it do when it intercepts requests< url-pattern>/*

3. We use struts XML configuration

Who is responsible according to struts XML configuration, find the action class and method to call?

4. Finally, according to struts The configuration in XML finds the corresponding return result result, and returns an HTTP response to the client browser

How struts 2 works

Struts interceptor, common interceptor

Advantages of struts 2

Struts 2 core controller

Struts 2 configuration file override order

How struts 2 modifies constants

Action

Three ways to implement action

Access servlet API

Results view

What is the role of the default struts default

How does struts 2 validate the specified method

valuestack

What is the difference between actioncontext, ServletContext and pagecontext?

Interceptor

custom interceptor

File upload

I think it is necessary to clarify the principles of struts, not only memorize the principles written on the data, but also summarize these problems.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>