Applying MVC design model through struts

Source: Internet
Author: User
Tags config contains html form http post

The model-View-Controller (MODEL-VIEW-CONTROLLER,MVC) system highlights the advantages of Servlets and JSP, while minimizing their shortcomings. In the implementation, a user request is sent to a controller servlet that determines the nature of the request and transmits it to the appropriate processor according to the requested type. Each processor is associated with a particular model, which encapsulates a business logic to perform a special set of functions. After processing, the result is sent back to the processor, and the processor chooses the appropriate view to display it.

Struts was first introduced as part of the Jakarta Project in May 2002, and the Jakata project was shipped by Apache Foundation. The project's founders hope that the project's research will improve and improve the Java Server Pages (JSPs), servlet, Tag library, and object-oriented technical standards. Struts provides a framework for writing applications using the MVC system. Struts uses "actionmapping", which allows the servlet to turn a user's request into an application behavior. Actionmapping typically specifies the path of a request, the type of object that is acting on the request, and specifies other required attributes.

The action object is used as part of the actionmapping to process the request, and to send a response back to the appropriate view (usually a Web browser), or to transfer the request to the appropriate model.

The bridge between the model and the view is a form bean that can be obtained by inheriting Org.apache.struts.action.ActionForm. The form bean is used to hold user data before processing, or to display model data that is returned to the user. Struts contains a custom label that can automatically combine fields from the form bean that you create.

In practice, the user requests a JSP page, and the controller servlet receives the request and decides which application handles it. The Action object transmits the request to the JavaBean that contains the received business logic. The business logic bean connects the database and queries, receives the results, and returns the result to the action object. The Action object stores the result as part of the request into a form bean. Once you have collected all of the requested data, you can format the results and display them. Finally, JavaServer page displays the results as an HTML form.

Models, controllers, and views

The basic component of a controller in struts is the servlet defined in the Actionservlet class, which is defined by actionmappings. The Actionmapping class represents the name and location of the action object. When a request enters the controller, it maps the requested path to the location of the action and sends the request to the action. The Actionmapping class of struts may also contain other information that may be relevant to your application, such as local variables, environment variables, or other URIs.

The key to struts is the activity around the controller. The Struts controller servlet maps events to classes (an event is usually an HTTP POST, get, or similar request). Actionservlet is the headquarters of MVC Design model and the core of structs architecture. Actionservlet creates and uses action, a actionform and Actionforward. Struts-config.xml is used to set the action. During the creation of Web applications, extended action and actionform are used to address the issue of how to respond to user requests. Struts-config.xml tells Actionservlet how to use an extended class. You can also extend Actionservlet to make the structs application with some custom attributes.

This method has several advantages. First, the entire logical flow of the application is in a hierarchical text file. This makes it easier to view and understand applications, especially for large applications. Second, people who write Web pages do not need to traverse the Java code to understand the application process while modifying the page, and Java developers do not have to recompile the code when changing the process.

Actionform maintains session state for Web applications. Actionform is an abstract class that can be extended by each input table model. It represents the general concept of data that can be set or updated by an HTML table. For example, your application may have a useractionform that is set by an HTML table. The struts schema checks whether Useractionform exists, and if not, creates an instance of the class. Structs will use HttpServletRequest's corresponding fields to set the Useractionform state. Before transferring useractionform to the commercial packaging layer useraction, the struct schema updates the state of the Useractionform first.

The structs model contains the state of the system and the operations it can accept. You can use a variety of structures to represent models (such as other servlets or JSPs), but in most cases you will use JavaBeans. JavaBean properties, or data from an external data source in Enterprise JavaBeans (EJBs), represent States, and methods represent actions that can be performed on them. Operations do not need to be defined by the JavaBean method; In a simple case, the action can be placed in the Action object, although this will blur the distinction between processing and grading.

The view that Struts MVC applies is typically constructed by JSP, which can use HTML or XML to statically format pages, and also provides a way to dynamically insert custom content in response to a user's request. A key to structs is its custom tag library, which provides a way to easily create a user interface using a JSP.

The structs architecture contains custom tag libraries that can be used in a variety of ways. While it is not necessary to use the architecture, the tags they contain are useful in many parts of your application. Some of structs's tag libraries include:

. structs-html Tag Library. This library can be used to create dynamic HTML user interfaces or tables.

. Structs-bean Tag Library. For putting JavaBean code on a JSP page, the library provides an enhanced way to implement through the tag.

. Struts-logic Tag Library. The library can manage conditionally produce output text, traverse object collections to produce output text repeatedly, and apply flow control.

. Struts-template Tag Library. The library contains labels that can be used as pages to create dynamic JSP templates that have a public or common format.

Using these tag libraries is the same as any other library. Because the library is already written, all you have to do is tell the servlet engine to use it. In Tomcat, you can specify the URI of the tag library and the location of the tag library description file on the Web server system by using the tag in the Web.xml file.

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.