Struts principles, development and project implementation

Source: Internet
Author: User
Struts principles, development and project implementation
Holen 2002-9-12

1. Summary
2. Keywords
3. Framework
4. Origin of struts
5. Working Principle of struts
6. Install struts
7. One instance
8. Struts advantages and disadvantages
9. Struts project implementation experience
10. Summary
11. References

1. Summary
This article describes the working principle, installation, and configuration of struts (taking struts1.0.2 as an example. Then, we will discuss in detail the application of struts in the actual development process based on an example. Finally, we will provide some experience accumulated in the development process for your reference.

2. Keywords
Struts, MVC, J2EE, tiles, framework

3. Framework
Framework is the architecture (framework). Here it refers to the software architecture method. Why should we mention the framework? Let me start with J2EE.
The J2EE system includes JSP, Servlet, EJB, Web service, and many other technologies. The emergence of these technologies provides a very competitive choice for Web application development in the e-commerce era. How to combine these technologies to form a stable architecture to meet the needs of the project is a very important step in the project development process.
This step is generally completed by the architect. Based on the project requirements, the designer will screen and select various technologies in the J2EE system, and take into account the division of roles in the development process and subsequent operation and maintenance, and system scalability.
A successful software requires a successful architecture, but the establishment of the software architecture is a complex and continuous improvement process. It is impossible for software developers to build different architectures for different projects, however, we always try to reuse the previous architecture as much as possible or develop architecture solutions that are as common as possible. Struts is one of them, and Struts is a popular J2EE-based architecture solution, other commonly used J2EE-based architecture solutions include turbine and realmothods.

4. Origin of struts
Struts was first launched as part of the Apache Jakarta project. The project creator hopes to improve and improve the Java Server Pages (JSPs), Servlet, tag library, and object-oriented technical standards through research on this project. The current maximum release version is struts1.0.2, which can be downloaded at
The struts name comes from the supported metal racks used in buildings and old-style aircraft. It aims to help you reduce the time needed to develop Web applications using MVC design models. You still need to learn and apply the architecture, but it will do some heavy work. Struts is a good choice if you want to use the advantages of servlets and JSP together to build scalable applications.

5. Working Principle of struts
MVC, short for Model-View-Controller, is a common design pattern. MVC weakens the coupling between the business logic interface and the data interface, and makes the view layer more changeable. The working principle of MVC is shown in 1:

Struts is an implementation of MVC. It uses Servlet and JSP tags (belonging to the J2EE specification) as part of the implementation. Struts inherits various MVC features and makes corresponding changes and extensions based on J2EE features. The working principle of Struts is shown in Figure 2:

Control: Through Figure 2 we can see that there is an XML file Struts-config.xml, associated with the Controller, in struts, bear the Controller role in MVC is a servlet, called actionservlet. Actionservlet is a common control component. This control component provides an entry point for processing all HTTP requests sent to struts. It intercepts and distributes these requests to the corresponding response class (these response classes are subclasses of the action class ). In addition, the control component is also responsible for filling the action from (usually called frombean) with the corresponding request parameters and passing them to the action class (usually called actionbean ). The logging class implements the core business logic. It can access Java Beans or call ejbs. Finally, the controls class passes control to the subsequent JSP file, which generates a view. All of these control logics are configured using Struts-config.xml files.
View: The view is mainly generated by JSP pages. Struts provides a wide range of JSP tag libraries, such as HTML, Bean, logic, and template. This facilitates separate presentation of logic and program logic.
Model: The model exists in the form of one or more Java Beans. These beans are divided into three types: Action Form, action, JavaBean or EJB. Action form is generally called formbean, which encapsulates user request information from the client, such as form information. Action is usually called actionbean. It gets the formbean from actionsevlet, retrieves the relevant information in formbean, and makes relevant processing. Generally, it calls Java Bean or EJB.
Process: In struts, user requests are generally set *. do is used as the request service name, all *. do requests are directed to actionsevlet, which encapsulates user requests into a formbean with a specified name based on the configuration information in the Struts-config.xml and transmits this formbean to the actionbean with the specified name, actionbean performs business operations, such as file operations and database operations. Each *. Do has a corresponding formbean name and actionbean name, which are configured in the Struts-config.xml.

Core: the Core of Struts is actionsevlet, the core of actionsevlet is Struts-config.xml. This will be discussed in detail later.

6. Install struts
The installation of Struts is relatively simple. The following describes the installation process using Tomcat 4.0.4 as an example.
First, go to Region.
After unzipping the zip package, you can see that Lib, webapps, and webapps have some war files.
Assume that your Tomcat is installed in C:/tomcat, copy the war files to C:/tomcat/webapps, and restart tomcat.
Open your browser and enter http: // localhost: 8080/Struts-example/index in the address bar. JSP. If you can see the dark blue icon of "powered by struts", it means it is successful. This is an example of Struts. It is accompanied by detailed instructions and can be used as a beginner's tutorial.
In addition, Struts provides a system practical object: XML processing, automatic processing of JavaBeans attributes through Java reflection APIs, and International prompts and messages.

7. One instance

In a user registration system, the user enters the relevant information through the webpage: registration ID, password, email. If the registration is successful, a message indicating success is returned. Otherwise, a message indicating registration failure is displayed.
The following is the core code of the relevant file.

Project establishment:
Before official development, you need to create this project in tocmat (my tomcat is installed in C:/tomcat.
One of the faster creation methods is:
Create a directory test under C:/tomcat/webapps, and then run the following command under C:/tomcat/webapps/Struts-Example:
Copy the WEB-INF directory to the test directory, and then clear the SRC and classes directories under the test/WEB-INF, and the contents in the struts-config.xml file. In this way, all the struts class packages and related configuration files are required.
During development, put the JSP file in the test directory, the original Java file in the test/WEB-INF/src, the compiled class file in the test/WEB-INF/classes.

Registration page: reguser. jsp


This JSP page is different from a common JSP page, because it uses a lot of taglib, which may be difficult for beginners to master, but this is one of the essence of struts. Flexible use will greatly improve the development efficiency.


type="org.cjea.Struts.example. RegUserForm "/>

type=" org.cjea.Struts.example.RegUserAction "
attribute=" regUserForm "

The core of Struts is controller, that is, actionservlet, and the core of actionservlet is Struts-config.xml, Struts-config.xml concentration of all the page navigation definition. For large web projects, you can quickly grasp the context through this configuration file, which is of great benefit for both early development and later maintenance or upgrade. Mastering Struts-config.xml is the key to mastering struts.


package org.cjea.Struts.example;

import javax.Servlet.http.HttpServletRequest;
import org.apache.Struts.action.ActionForm;
import org.apache.Struts.action.ActionMapping;

public final class RegUserForm extends ActionForm{

private String logname;
private String password;
private String email;

public RegUserForm(){
logname = null;
password = null;
email = null;

public String getLogName() {
return this.logname;
public void setLogName(String logname) {
this.logname = logname;
public void setPassWord(String password) {
this.password = password;
public String getPassWord() {
return this.password;
public void setEmail(String email) { = email;
public String getEmail() {

public void reset(ActionMapping mapping, HttpServletRequest request)
logname = null;
password = null;
email = null;

Each formbean must inherit the actionform class. formbean encapsulates page requests. The HTTP request is encapsulated in an object. It must be noted that multiple HTTP requests can share one formbean for easy maintenance and reuse.

Actionbean: reguseraction

Package org. cjea. Struts. example;

Import javax. servlet. http .*;
Import org. Apache. Struts. Action .*;

Public final class reguseraction extends action

Public actionforward perform (actionmapping mapping,
Actionform form, httpservletrequest req,
Httpservletresponse res)
String title = Req. getparameter ("title ");
String Password = Req. getparameter ("password ");
String email = Req. getparameter ("email ");
Obtain user requests and perform database operations.

Formbean is generated to provide data to actionbean. In actionbean, data encapsulated in formbean can be obtained. After corresponding logic processing, the business method is called to fulfill the corresponding business requirements.

Servlet evolution: in the conventional three-layer structure of JSP, Servlet, and JavaBean, JSP implements the view function, Servlet implements the controller function, and JavaBean implements the model implementation.
In struts, the servlet is split into three parts: actionservlet, formbean, and actionbean. Actionservlet with Struts-config.xml, dedicated to complete the page navigation, instead of responsible for specific data acquisition and the corresponding logic, these two functions are completed by formbean and actionbean.

8. Struts advantages and disadvantages
Like many Apache projects such as Tomcat and turbine, Struts is an open-source software, which has a major advantage. This allows developers to gain a deeper understanding of their internal implementation mechanisms.
In addition, the advantages of struts are mainly embodied in two aspects: taglib and page navigation. Taglib is a struts tag library, which can be used flexibly to greatly improve development efficiency. In addition to common JSP tags, JSP developers in China rarely develop their own tags. Struts may be a good starting point.
As for page navigation, I think that will be a future direction. In fact, this will make the context of the system clearer. With a configuration file, you can grasp the relationship between all parts of the system, which is of great benefit for later maintenance. This advantage is especially evident when another group of developers take over the project.
Taglib is a major advantage of struts, but for beginners, it requires a continuous learning process and even disrupt the habit of writing web pages. However, when you get used to it, you will think it is really great.
Struts divides the controller of MVC into three parts. The structure is clearer while the complexity of the system is also increased.
Less than half a year has passed since struts was created, but it has been gradually applied to commercial software. Although it still has many shortcomings, it is a very good J2EE MVC implementation method. If your system is ready to adopt the J2EE MVC Architecture, consider struts.

9. Struts project implementation experience
Some time ago, we developed a web application based on the struts architecture (combined with tiles. The following are some of our experiences and lessons learned during the project. I hope you can help me.
1. For Struts-based project development, a good overall plan is required first. Which modules are included in the system, and how many formbeans and actionbeans are required for each module, and it is best to have a dedicated person responsible for the management of Struts-config.xml. The development of Struts-based project is the difficulty of configuration management, especially the management of Struts-config.xml.
2. If your project is very tight and there are no experienced struts developers in the project team, we recommend that you do not use struts. It takes about half a month for a skilled JSP programmer to learn struts. If titls is used, it takes a longer time.
3. If you use taglib extensively on the webpage, your artist will make some sacrifices. This sacrifice is especially evident when you combine tiles to enhance features. Of course, your choice of functionality and beauty is up to you.
4. taglib is a good thing, but flexible use of it requires a process. If you don't want to spend too much time on taglib, you only need to understand several form-related tags, let's keep the other marks. Later, study actionservlet and Struts-config.xml first, and you will feel a sense of accomplishment.
5. Although struts was not born for a long time, there are more and more tools related to it. If you use JBuilder as a development tool, I can recommend several excellent open tools for you, greatly improve development efficiency.
6. Is struts only applicable to large projects? No! Struts is suitable for projects of various sizes. Of course, for large projects, its advantages are more obvious.

10. Summary
Struts is an excellent J2EE MVC Architecture. It uses taglib to obtain reusable code and abstract Java code, and uses actionservlet with Struts-config.xml to realize navigation of the entire system. It enhances developers' overall grasp of the system and enhances the maintainability and scalability of the system.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

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.