Migrate Struts applications to Struts 2 (2)

Source: Internet
Author: User

 

 

In the previous article, we have explained the structure of the entire framework, the basis of the request process, and the differences between the configuration method and Struts2 and struts1. After learning about this, migrating from a Struts application to Struts 2 is no longer difficult.

In this article, we will describe in more detail how to convert the Struts action into the action of Struts 2.

 

Example of an application

This example selects weblog, which everyone is familiar with. It briefly introduces the functional requirements of this example:

• Add a new log

• View a log

• Modify a log

• Delete a log

• List all days

Addition, deletion, and modification (CRUD) are the most common applications in projects.

Business logic classes can be shared in Struts and Struts2 applications. For example:

Public class BlogService ...{

Private static List <Blog> blogs = new ArrayList <Blog & gt ;();

Public List <Blog> list ()...{...}

Public Blog create (Blog blog )...{...}

Public void update (Blog blog )...{...}

Public void delete (int id )...{...}

Public Blog findById (int id )...{...}

}

 

BlogService is just a simple business logic class and is not an interface. The Struts and Struts2 actions can be called by their instances. Although this design will lead to unnecessary coupling in actual projects, our examples only focus on the web layer, so it is irrelevant.

 

QUOTE:

 

Toolbox: in the first article, we talked about the dependency Injection Interface injection method in Struts2 actions. This is the main injection method for servlet-related classes (HttpServletRequest, HttpServletResponse, PrincipalProxy, etc.), but this is not the only method.

When Struts2 can use the Spring framework as the default container, the setter method of dependency injection is available. By adding the setter Method to the action (as shown below), the Struts2 framework can obtain the correct information from the Spring framework and load it into the action through setter.

Public void setBlogService (BlogService service )...{

This. blogService = service;

}

Similar to the interface injection method, we need an interceptor to help us complete the task. This is the ActionAutowiringInterceptor interceptor. In this way, our business logic class is automatically injected into Struts2's action before the action is called through Spring framework management. There are a variety of configuration parameters (such as by name, by type or automatically) to choose from, so that the injection method matching the object and setter can be determined according to your needs.

Code in Struts Application

 

Let's start with Struts. In Struts, the most common practice is that each requirement case (such as save, update, remove, list) has a corresponding action class, there will also be corresponding action form classes. This method may not be the best implementation method in our applications (other solutions include using dynamic form or using request to distribute actions ), however, the practice in our example is the most familiar form for all Struts developers. With this simple implementation method, you have the ability to use better methods when migrating to Struts2.

In the first article, we talked about the difference between action in Struts and Struts2. Now let's look at their differences again from UML. In general, the form in Struts action is: image1.jpg

This action form will be used in multiple actions. Let's take a look at it:

Public class BlogForm extends ActionForm ...{

Private String id;

Private String title;

Private String entry;

Private String created;

// Public setters and getters for all properties

}

 

As shown in UML, one restriction is that the ActionForm class must be inherited, And the other restriction is that all attributes in form must be of the String type, therefore, all getter and setter must only accept the String parameter and return the String result.

 

Then let's take a look at the action. The actions in this example include view, create, and update actions.

The View Action:

The Create Action:

Public class ViewBlogAction extends Action ...{

Public ActionForward execute (ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

Throws Exception ...{

BlogService service = new BlogService ();

String id = request. getParameter ("id ");

Request. setAttribute ("blog", service. findById (Integer. parseInt (id )));

Return (mapping. findForward ("success "));

}

}

Public class SaveBlogEntryAction extends Action ...{

Public ActionForward execute (ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

Throws Exception ...{

BlogService service = new BlogService ();

BlogForm blogForm = (BlogForm) form;

Blog blog = new Blog ();

BeanUtils. copyProperties (blog, blogForm );

Service. create (blog );

Return (mapping. findForward ("success "));

}

}

Public class UpdateBlogEntryAction extends Action ...{

Public ActionForward execute (ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

Throws Exception ...{

BlogService service = new BlogService ();

BlogForm blogForm = (BlogForm) form;

Blog blog = service. findById (Integer. parseInt (blogForm. getId ()));

BeanUtils. copyProperties (blog, blogForm );

Service. update (blog );

Request. setAttribute ("blog", blog );

Return (mapping. findForward ("success "));

}

}

 

All three actions follow the same pattern:

Generate a new business logic object instance-as mentioned above, we use the business logic object in action in the most direct way, this indicates that a new business logic object instance is generated in each action.

 

Get data from the request-one of the two forms. In view action, "id" is obtained directly from the HttpServletRequest object. In create and update actions, the value is taken from ActionForm. The call method of ActionForm and HttpServletRequest is very similar. The only difference between ActionForm and HttpServletRequest is the value of bean from field.

Call business logic-now we start to generate the parameters required to call the business logic and call the logic. If the parameter (In view action) is a simple object type, the converted value is automatically converted to the correct type (for example, from String to Integer ). If the parameter is a complex object type, the ActionForm needs to use BeanUtil to convert it into a corresponding object.

 

Set the returned data-if you need to return the data to the user, you need to set the data to be returned in the attribute of HttpServletRequest. Return an ActionForward-All Struts actions must be found at the end and return the corresponding ActionForward object.

 

There are only a few differences between the last two actions, remove and list actions. The remove action is shown below, without the BlogForm class. You can call the business logic to complete the required work by obtaining the "id" (similar to the view action) from the attribute of the request. When we introduce the configuration below, you can see that it does not return any data, because its "success" returned result is actually executed after removing and then executed list action to return information.

Public class RemoveBlogEntryAction extends Action ...{

Public ActionForward execute (ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

Throws Exception ...{

BlogService service = new BlogService ();

String id = request. getParameter ("id ");

Service. delete (Integer. parseInt (id ));

Return (mapping. findForward ("success "));

}

}

 

 

List action does not require any user input. It simply calls the no-argument method of the business logic and returns all the Blog objects.

Public class ListBlogsAction extends Action ...{

Public ActionForward execute (ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

Throws Exception ...{

BlogService service = new BlogService ();

Request. setAttribute ("bloglist", service. list ());

Return (mapping. findForward ("success "));

}

}

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.