Research and application of Struts framework based on MVC pattern

Source: Internet
Author: User
Tags html tags implement interface query tld client
   Summary:The struts framework has the advantage of modularity, flexibility, and reusability of components, while simplifying the development of Web applications based on MVC. This paper discusses the principle and method of realizing MVC pattern in struts framework, and gives a concrete application example.

The Distributed enterprise application software has complex structure and involves many kinds of technology, which puts forward a high demand for the design and development personnel. In this case, it is necessary to design and develop the software using the design pattern-reusable design scheme. MVC pattern has been proved to be a successful software design pattern, this paper mainly discusses an application framework of MVC pattern ――struts, and demonstrates the method of implementing the MVC pattern with Struts framework through an example.

   MVC design Pattern

MVC (Modle-view-controller, model-View-Controller) mode is a software design pattern designed by Xerox PARC in the 1980s for the programming language Smalltalk-80, which has been widely used so far, It is recommended as the Sun Java Platform Design model, which provides an excellent design model for the development of interactive application systems, and is welcomed by more and more developers.

The MVC pattern consists primarily of 3 parts: models, views, and controllers, as shown in Figure 1.


Figure 1 MVC Pattern Framework
The model represents business logic and business rules, and has the most processing tasks in the three parts of MVC. It can use component technologies such as JavaBean and EJB to handle access to the database. A model can provide data for multiple views. Because the code applied to the model can be reused by multiple views only once, it reduces the duplication of the code.

The view is displayed on the screen. After the model has been manipulated, the result is displayed through the view. There is actually no real processing happening in the view, just as a way of outputting data and allowing the user to manipulate it.

The controller is used to manage user interaction with the view. Once the user wants to process the model, it cannot execute the model directly, but it is realized indirectly through the controller. The controller can take a value from the view, and then pass the corresponding value to the model for processing. The controller accepts input from the user and invokes the model and view to fulfill the user's needs.

From the above, the MVC pattern process is: first, the controller to receive the user's request, and decide which model should be called to process, and then the model with business logic to handle the user's request and return data, the last controller in the corresponding view returned data, and through the expression layer presented to the user.

MVC itself is a very complex system, so using MVC to implement Web applications, it is best to choose a ready-made MVC framework, under which to develop, so as to achieve a multiplier effect. There are a lot of MVC frameworks available now, because struts has a complete document and is relatively simple, so it's easier to develop an MVC system with it.

   Struts Framework

Struts is an open source project for the Apache organization. Struts is a better MVC framework that provides the underlying support for developing an MVC system, with the main technology being servlet,jsp and custom tag library. The basic composition is shown in Figure 2.


Fig. 2 Struts frame diagram
As you can see from Figure 2, the Controller feature in the Struts framework consists of actionservlet and Actionmapping objects, and the core is a servlet-type object Actionservlet that accepts client requests. Actionservlet includes a set of Actionmapping objects based on configuration, and each Actionmapping object implements a mapping between a request and the action processor object in a specific model section.

The model section is composed of action and Actionform objects. All the action processor objects are subclasses that the developer derives from the action class of struts. The action processor object encapsulates the specific processing logic, invokes the business logic module, and submits the response to the appropriate view component to produce a response. Struts provides Actionform component objects that can describe client form data by defining attributes. The developer can derive the subclass object from it, combine it with the custom tag library provided by struts to achieve good encapsulation and support of the client's form data, the action processor object can read and write directly to it, and no longer need to interact with request and response objects. Support for interaction between view and model is achieved through the Actionform Component object.

The view section is implemented through JSP technology. Struts provides a custom tag library that can be very well interacting with the model part of the system by using these custom tags to create a JSP form that can be mapped to the Actionform in the model section to encapsulate the user's data.

   The application of Struts framework based on MVC pattern

The following is a concrete example of how to apply the Struts framework to build an application. This example is a budget information module in a comprehensive budget management system, which includes four functions of data entry, data inquiry, data modification and data deletion, which is used to complete the input, query, modification and deletion of budget data.

In particular, when using the struts framework, the development of the corresponding sections mainly includes:

1, build the View

In the struts framework, the implementation of the view is mainly based on JSP technology, but compared with the traditional JSP there are two significant differences.

First, using the JSP development view under Struts, the page does not contain any scripts, but simply completes the data transfer and receives and displays the returned data. The processing of data and the jump of the page are implemented in the business layer. This enables a complete separation of business logic and presentation logic, improves the performance and scalability of an application, and facilitates the reuse of programs.

Second, the Struts framework provides a set of extensible custom tag libraries, mainly including bean tags, logic tags, HTML tags, and template tags. By using tags, you can simplify the process of creating a user interface and better implement the encapsulation of your data. By using these custom tags to create a JSP form, you can implement the mapping of Actionform in the business layer and interact very well with the business logic part of the system.

The following gives the section implementation code of the Data entry page:

<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ taglib uri= "/web-inf/struts-html.tld" prefix= "html"%>
<%@ taglib uri= "/web-inf/struts-bean.tld" prefix= "Bean"%>
<%@ taglib uri= "/web-inf/struts-logic.tld" prefix= "logic"%>
<title>
Pre-Preparation Data entry
</title>
<link rel= "stylesheet" type= text/css "href=". /images/mobile.css ">
<body bgcolor= "#ffffff" >
<table width= "border=" 1 "cellspacing=" 1 "cellpadding=" 2 "bordercolor=" #CBE7F8 "align=" center "
<TR bgcolor= "#F2F9FD" >
<TD width= "20%" > type: </td>
<td>
</td>
</tr>
......
</table>
</body>
The program first introduces the HTML, bean, and logic tag libraries, and then uses the action to indicate the absolute path of the invoked action component, marking
It can be seen that no business logic is handled in the view, and its primary role is to give the information and processing results that the client displays, as well as the request forwarding, which is the interface with which the user interacts.

2. Build the Model

The model uses JavaBean and EJB components to design and implement the business logic of the system. Derive the specific action object from the action based on different requests, complete the "what" task to invoke the business component made up of the bean, and create the encapsulation of the client form data by the Actionform derived class.

Here's how to budgetinfoaddform this actionform bean:

......
Public final class Budgetinfoaddform extends Actionform {
Private String InfoType;
Private String title;
Private String content;
Private Formfile Infofile;
This means that budgetinfoaddform inherits from Actionform, in which you define the parameters of the view in which you want to display the data information, including InfoType, data classification, title, data title, content, data contents, Infofile, Information files. The methods for setting and accessing these parameters are also provided in Budgetinfoaddform:

Public String Getinfotype ()
public void Setinfotype (String infotype)
Public String GetTitle ()
public void Settitle (String title)
Public String getcontent ()
public void SetContent (String content)
Public Formfile Getinfofile ()
public void Setinfofile (Formfile infofile)
public void Reset (actionmapping mapping, HttpServletRequest request) {
title = null;
content = null;
Infofile = null;
}
3. Building the Controller

In the Struts application framework, the central controller (Actionservlet) is provided by the struts framework itself, and developers generally do not need to develop the Actionservlet; The action class responsible for specific business processes is the focus of developer development, All of the business operations are performed in these action objects, and the actionservlet of struts is shifted to the JSP page to return the processing results to the client.

In the budget data module, four action:budgetinfoaddaction are implemented, used for data entry, budgetinfocontentaction, for data query, budgetinfoupdateaction for data modification Budgetinfodelaction, for data deletion.

4, establish the configuration file

The struts framework has two profiles Web.xml and struts-config.xml that are used to configure the interactions between the various modules in the struts system. By configuring the two configuration files, the various parts of MVC in the Struts framework are connected to achieve a true MVC system.

   Concluding remarks

Struts is an enterprise-level Web application Development framework based on MVC design pattern, which is designed to mitigate the burden of constructing enterprise Web applications as a whole. Its own tag library can greatly improve the development efficiency and improve the maintainability and extensibility of the system. Based on summarizing the struts frame technique and its working principle, this paper gives an application example, which provides a reference for the future development of Web application in Struts framework.

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.