Applying MVC design model through struts

Source: Internet
Author: User
Tags format define object html form variables version web services apache tomcat
The design 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 <taglib> tag in the Web.xml file.

The Small Print

The Jakarta project allows you to download the binary version of the structs or compile it directly from the source code. Binary plate work well, but if you have an uncommon software platform, or want to learn how to compile it, it is not very difficult. Regardless of your choice, structs needs the following software:

. Java Development Kit (JDK). You need to download and install a Java 2 JDK implementation for your operating system.

. Servlet container. You will also need to download and install a servlet container that is compliant with the Servlet API specification (version 2.2), the JSP specification (above 1.1). A good option is to download Apache Tomcat, which supports Web services and can run servlet and JSP.

. XML parser. Struts requires an XML parser that is compatible with the Java API for XML parsing (JAXP) specification (version 1.1 or above).

. Servlet API class. To compile structs or use structs applications, you will need a Servlet.jar file that contains servlet and JSP API classes. Most servlet containers contain this jar file.

. JDBC 2.0 Optional Package class. Structs supports an optional implementation of Javax.sql.DataSource, so it needs these API classes to compile. You can download these package classes at this URL:

To use structs to build an application, you need to follow these steps. First, the Lib/commons-*.jar and Lib/struts.jar files in the structs are copied to the Web-inf/lib directory in the application. Then copy the entire tag library description file (LIB/STRUTS-*.TLD) in the structs to the Web-inf directory in your Web application.

After copying these files, You can modify the Web.xml file in the Web application so that it contains an <servlet> element to define the controller servlet and a <servlet-mapping> element to indicate which request URI is mapped to this servlet. If you use the default directory for a standard installation, you can use the Web.xml file in the struts example application as a reference to see how it is set up, and then you can modify the Web.xml file in the Web application to specify the appropriate tag library declaration. Again, you can learn how to set up by structs's example application.

After modifying the Web.xml file, create a file called Struts-config.xml in the Web-inf directory, where you define the action map and other features that are relevant to your application. Finally, at the top of each JSP page that will use struts to customize the label, add the Struts custom label declaration.

The name struts comes from the supporting metal racks used in buildings and old airplanes. It is designed to help you reduce the time you use to develop Web applications using the MVC design model. You still need to learn and apply the architecture, but it will be able to do some of the heavy work. If you want to mix the advantages of servlets and JSP to build scalable applications, struct can help you do some of these things.

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.