Apply MVC design model via Struts

xiaoxiao2021-03-06  23

Transfer from www.yesky.com

Model - View-Controller (MVC) system highlights the advantages of servlets and JSP, while minimizing their disadvantages. In implementations, the user request is sent to a controller servlet, the servlet determines the requested nature, and transmits the appropriate processor based on the type of request. Each processor is related to a special model, which is packaged in business logic to perform some special functions. After the processing is completed, the result will send back the processor, and the processor selects the appropriate view to display it.

Struts was earlier in May 2002 as a part of the Jakarta project, and the Jakata project was made by Apache Foundation. The founders of the project hopes that the project has improved and improves Java Server Pages (JSPS), servlet, label libraries, and object-oriented technical standards. Struts provides an architecture for writing applications for using the MVC system. Struts uses "ActionMApping" that allows servlet to convert users to application behavior. ActionMApping typically specifies a request to operate the object type of the request, and the attributes specifying other needs.

The Action object is used as a part of ActionMapping, which is responsible for processing a request, and sending a suitable view (usually a web browser), or transmits a request to a suitable model.

The bridge between the model and the view is a Form Bean, which can be obtained by inheriting org.apache.struts.action.actionform. The Form bean is used to save the user data before processing, or display the model data returned to the user. Struts contains custom labels that can automatically combine fields in the created Form Bean.

In practice, the user requests the JSP page, the controller servlet receives the request and determines which application handles it. The Action object transmits the request to the JavaBean containing the receiving business logic. Commercial logic bean will connect to the database and query, receive the result, and then return the result to the action object. The Action object stores the result as part of the request to a Form Bean. Once all data is collected, the results can be formatted and displayed. Finally, JavaServer Page is displayed in the form of an HTML form.

model,

Controller and view

The basic components of the controller in Struts are the servlet defined in the ActionServlet class, which is defined by ActionMAppings. The ActionMapping class is represented by 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 transmits the request to the Action. Struts's ActionMapping class may also contain additional information, which may just be related to your application, such as local variables, environment variables, or other URIs.

The key to Struts is the activities around the controller. The Struts Controller Servlet mapping event is class (an event is usually an HTTP POST, GET, or a similar request). The ActionServlet is the commander of the MVC design model and is also the core of the STRUCTS architecture. ActionServlet creates and uses Action, an ActionForm, and ActionForward. Struts-config.xml is used to set an action. During the creation of a web application, use extended action and actionform to resolve the issue of how to respond to user requests. Struts-config.xml is to tell the ActionServlet how to use the extension class. The ActionServlet can also be extended to make the Structs application with some custom features.

This method has several advantages. First, the entire logic stream of the application is in a grade grade text file. This allows you to easily view, understand applications, especially large applications. Second, the person who writes a web page does not need to traverse the Java code when modifying the web page, and Java developers do not need to recompile the code when changing the process. ActionForm maintains the SESSION status for a web application. ActionForm is an abstract class that can be extended by each input table model. It represents the general concept of data, can be set or updated by the HTML table. For example, your application may have a useerActionform that is set by the HTML table. The Struts architecture will check if the useerActionform exists. If there is no existence, an instance of this class will be created. Structs will use the corresponding field of HTTPSERVLETREQUEST to set the status of the UseractionForm. Before transferring UseractionForm to the useerAction of the commercial package layer, the Struct architecture first updates the status of the UseractionForm.

The STRUCTS model contains a systematic state and its acceptable operation. A variety of structures can be used to represent models (such as other servlets or JSPs), but you will use JavaBeans when most of them. JavaBean's properties, or data obtained by external data sources in Enterprise JavaBeans (EJBS), and the method is that the method is to represent the operations performed above. The operation does not need to be defined by the JavaBean method; in a simple case, the operation can be placed into the Action object, although this will make the difference between the processing and the rations be blurred.

The view of the Struts MVC application is typically constructed by JSP, which can use HTML or XML static formatting pages, and a method provides a method that can be dynamically inserted to respond to user requests. One of Structs is its custom label library, which provides a method that can easily use JSP to create a user interface.

The STRUCTS architecture contains custom label libraries that can be used in a variety of different ways. Although these architectures are used, these libraries are not necessarily, but they contain the labels in your application. Some tag libraries for Structs include:

Structs-HTML tag library. This library can be used as a creation of a dynamic HTML user interface or form.

Structs-bean tag library. Put the JavaBean code to the JSP page, the library provides a strengthening way, through

Label implementation.

Struts-logic tag library. This library can manage conditions for outputting text, traversing object collections to repeatedly generate output text, and application flow control.

Struts-template tag library. The library contains the labels that can be used as a page created a dynamic JSP template that has a common appearance or the format of the same.

Use these label libraries and any other library. Because the library is already written, what you have to do is telling the servlet engine to use it. In Tomcat, you can specify the "Taglib> tab in the web.xml file to specify the label library's URI, and the location of the label library description file on the web server system.

SMALL Print

Jakarta project allows you to download Structs binary versions, or compile it directly from the source code. The binch version is working very well, but if you have an uncommon software platform, or if you want to learn how to compile it, it is not very difficult. No matter what your choice, Structs requires the following software:

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

. Servlet container. You also need to download and install a servlet container that is compatible with the Servlet API specification (above 2.2), JSP specification (above). A good choice is to download Apache's Tomcat, which can support web services and run Servlet and JSP. . XML parser. Struts requires an XML parser compatible with Java API for XML Parsing (JAXP) specification (version 1.1 or more).

. 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 packages in this URL: http://java.sun.com/products/jdbc/download.html

To create an app with StruCTS, you need to follow the steps below. First, copy the lib / commus - *. Jar and lib / struts.jar files in Structs to the web-inf / lib directory in the app. Then copy all the label library files (lib / struts - *. TLD) in Structs to the web-INF directory in your web application.

After copying these files, you can modify the web.xml file in the web app, which contains a element to define the controller servlet, and a element to indicate which request URI is Map to this servlet. If you use the default directory for a standard installation, you can use the web.xml file in the Struts example to use the web.xml file as a reference, see how it is set, then you can modify the web.xml file in the web application. To specify the appropriate label declaration. Again this again, you can learn how to set up by StruCTS.

After modifying the Web.xml file, create a file called Struts-Config.xml in the web-inflicment, define an operation mapping and other features related to your application. Finally, the declaration of the Struts custom label is added at each of the JSP pages of the Struts custom label.

Struts This name is from the support metal rack used in the building and the old aircraft. Its purpose is to help you reduce the time to develop web applications using the MVC design model. You still need to learn and apply this architecture, but it will be able to complete some of them. If you want to mix the advantages of servlets and JSP to build scalable applications, Struct can help you complete some of them.

转载请注明原文地址:https://www.9cbs.com/read-81044.html

New Post(0)