The development step of Struts 2 application __struts2

Source: Internet
Author: User

Using STRUTS2 to make a simple login example, the following is a summary of the process of developing the Struts 2 application.

The development steps for Struts 2 applications:

(0) Copy the Struts core jar file to the project's Lib folder.

(1) Define the core filter in the Web.xml file to intercept the user request.

Since Web applications are applications based on the request/Response architecture, the framework's core servlet or filter must be configured in Web.xml to allow the framework to intervene in Web applications, regardless of the MVC web framework.

For example, the 1th step in developing the Struts 2 application is to add the following configuration fragment to the Web.xml file:

<!--defines the core filter--> of Struts 2

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<!--let struts 2 's core filter intercept all requests-->

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

(2) If you need to submit the request as a post, define the JSP page that contains the form data. If you simply send the request in get, you do not have to go through this step.

(3) Define the action class that handles user requests.

This step is also necessary in all MVC frameworks, because this action is the C in MVC, the controller, which is responsible for invoking methods in model to process requests.

The underlying mechanism of the MVC framework is that when a core servlet or filter receives a user request, it typically simply preprocess the user request, such as parsing, encapsulating parameters, and then creating an action instance through reflection and calling the action's specified method (Struts 1 is usually execute,struts 2 can be any method to handle user requests. Here's another question: How does a servlet or filter intercept a user request, and how does it know which action instance to create? There are two kinds of solutions:

² Using configuration files: For example, you can configure Login.action to use the Loginaction class. This allows the MVC framework to know which action instance to create.

² Use convention: This usage may be inspired by the rails framework, for example, you can contract xxx.action always correspond to xxxaction classes. If the core controller receives the Login.action request, the Logoinaction class is invoked to process the user request.

In the MVC framework, the controller is actually composed of 2 parts, which intercepts all user requests, and the common code that handles the request is completed by the core controller, while the actual business controls (such as Call model, return processing results, and so on) are handled by the action.

(4) Configure action. For most of the MVC frameworks in the Java domain, it is very much like using XML files to configure management. Configuring the action is to specify which request will process which action to apply, so that the core Controller creates the appropriate action instance based on that configuration and invokes the business control method of the action. For example, you typically use the following code fragment to configure the action.

<action name= "Login" class= "Org.crazyit.app.action.LoginAction" >

...

</action>

The above configuration fragment specifies that if the user requests that the URL be login, the org.crazyit.app.action.LoginAction is used to process it.

Now Struts 2 's convention plug-in draws on the advantages of the rails framework and begins to support the idea of "Convention over Configuration", which is to specify the correspondence between the user's request address and the action by means of a convention.

(5) Configure the corresponding relationship between the processing result and the physical view resource.

When the action processing user request is finished, it usually returns a processing result (usually with a simple string), which can be considered to be a logical view name that needs to be associated with the specified physical view resource to be valuable. Therefore, you also need to configure the corresponding relationship between the processing results.

For example, the following code fragment is used to configure the mapping relationship between the processing result and the physical view.

<action name= "Login" class= "Org.crazyit.app.action.LoginAction" >

<!--defines the mapping between 3 logical views and physical resources-->

<result name= "Input" >/login.jsp</result>

<result name= "Error" >/error.jsp</result>

<result name= "Success" >/welcome.jsp</result>

</action>

The bold-text code above specifies the mapping between 3 processing results and 3 physical views, and the configuration fragment is specified as Lee. Loginaction return to input, the actual will enter the/login.jsp page, when the error returns, the actual will enter the/error.jsp page, when the success return, the actual will enter the/welcome.jsp page.

(6) Writing view resources. If the action needs to pass some data to the view resource, you can use the OGNL expression.

With the above 6 steps, you can basically complete the development of a struts 2 process, that is, you can perform a complete request/response process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.