Design and implementation of Web layer application based on struts technology

Source: Internet
Author: User
Tags definition config functions http request implement
web| Design

In recent years, the Struts framework based on MVC (Model-view-controller) model is more and more popular with web system developers. Struts is based on the current dynamic web design in the field of JSP, Servlet, XML and other related development technologies on the basis of a mainstream development architecture, with modular, flexible and reusable advantages, while greatly simplifying the development of Web applications. It reasonably separates the user representation logic, the business logic and the control logic, makes the development process concise and clear, and improves the maintainability and reusability of the software.

Struts Framework

1. Struts Architecture

Struts is an application framework of open source code based on MVC Classic design pattern, and it is a more mature framework in web development at present. Provides a highly configurable MVC development model for Web development by consolidating servlet, JSP, JavaBean, custom tags, and information resources into a unified framework.

The struts architecture implements the concept of the MVC design pattern, which maps model, view, and controller to components in a Web application. Model is built on behalf of the system State and business logic action, view is implemented by the custom tags provided by JSP and struts, and Controller is responsible for controlling the flow, which is done by Actionservlet and actionmapping.

2. Introduction of main components

Below we will struts-config from the view, control, model, and struts configuration files separately. XML to describe the architecture of struts.

(1) View: Built primarily by JSP, struts itself includes a set of extensible custom Tag libraries (TAG1 IB) that simplify the process of creating a user interface.

(2) Model: The model mainly represents a system's state and business logic. In struts, the state of the system is primarily represented by the actiomform Bean, and the business logic is typically implemented by JavaBean or EJB components.

(3) Controller: Controller mainly by the Actionservlet class and action class to implement, Actionservlet class is the struts framework of the core components, mainly responsible for receiving HTTP request information. Struts-config according to configuration file. XML configuration information that forwards the request to the appropriate action object. The action class is responsible for invoking the model's methods, updating the state of the model, and helping to control the application's process.

(4) configuration file struts-config. XML: When Actionservlet receives HTTP request information, how do you decide which action object to forward the user request to? This requires some configuration information that describes the user request path and action mapping relationship. In struts. These configuration mapping information is stored in a specific XML file struts-config. XML, in which the mapping information for each action is configured with one (action) element. These configuration information is read into memory when the system starts up and is used by struts during run time, in which each (action) element corresponds to an instance of the Actionmapping class.

implementation of the BBS system based on struts framework

BBS Forum is a common function of E-commerce website, it provides a free discussion area for Internet users. Users can be based on the need to publish articles on the forum, ask questions and express their views, commonly known as post or irrigation. At the same time, Internet users can also browse other people's posts in the Forum, and can reply to the article. Therefore a typical BBS system should generally provide the following functions: ① member Management, ② discussion area articles, ③ published new articles, ④ background management.

Using the struts framework to realize the BBS, the application is decomposed into model, view and controller, which separates the business logic from the presentation layer, improves the software scalability and good logic, and changes the traditional Web development design pattern. Then use the Struts-config.xml configuration file to assemble the three, coordinate them to complete the corresponding functions. The following will be the user login in BBS as an example according to the MVC architecture to detail the implementation process.

1. Create VIEW Component

There are two main components in the view: the user login page bbslogin.jsp, is the input display page of struts; Login failed page wrong.jsp; Is the page that is displayed after the user has failed to log in. They are all saved under the Web application directory BBS.

2. Create Model components

The model component file Userinfo.java is the JavaBean that handles the business logic, encapsulates all the information related to the forum user, and defines all the methods associated with the forum user information processing. For example, the method of checking the user's identity and finding, deleting, editing, storing user information, etc. Subclasses that derive from the action class loginaction access Userinfo.java implement the processing of the business logic. If the business of model build processing is more complex, it can be implemented using EJB or other Java EE components. Note that the package for the class that corresponds to the business logic layer should be deployed under bbs/web-inf/classes.

3. Create Controller components

The controller component acts as the control center for the entire struts architecture, including actionservlet, Action beans, actionform beans, and related configuration file Web.xml, Struts-config.xml, and resource bundles. The Actionservlet class is the framework's own, the control hub of the entire struts framework, and usually does not need to be extended.

⑴ Declaration and definition Loginactionform.java

This javabean is used to collect form data submitted by the user login page. Note the attribute name in Actionformbean must correspond to the name of the form element and the name of the hyperlink parameter in the User presentation Layer JSP page.

⑵ Declaration and definition Loginaction.java

Loginaction is responsible for processing the client submission request, its main function is according to receive the different request, through the Loginactionform obtains the corresponding submission form data, invokes the business logic layer corresponding Userinfo.java to carry on the processing. Finally, the results are passed to the user presentation Layer JSP page, which is displayed in front of the user.

⑶ configuration file definition

Web.xml primarily implements the configuration of Actionservlet, and must also declare the Struts tag library used in the Web presentation layer. For this application struts-config.xml configuration is as follows:

       
        
        
          
           
          
          
          
           
          
          
           
         
       
        

Concluding remarks

In a sense, struts technology only implements the Web Layer application framework, but it is a good extensibility and continuous improvement of the framework, combined with the Java Core Design model, can build a fairly stable based on MVC application framework, the use of such an application framework can not only shorten the development time, It can also realize the reuse of framework and code, and it is the mainstream technology of web application development in the future.



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.