In the previous article about getting started with Struts, we mainly talked about how to install Struts and the registration page: reguser.jsp. This article will mainly introduce Struts application examples, advantages and disadvantages, and implementation experience.
Struts-config.xml:
request "
validate="false">
The core of Struts is Controller, which is ActionServlet, and the core of ActionServlet is Struts-config.xml. Struts-config.xml concentrates all The navigation definition of the page. For large-scale WEB projects, you can quickly grasp its context through this configuration file, which is very helpful for both early development and later maintenance or upgrades. Mastering Struts-config.xml is the key to mastering Struts.
FormBean: RegUserForm
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) {
this.email = email;
}
public String getEmail() {
return this.email;
}
public void reset(ActionMapping mapping, HttpServletRequest request)
{
logname = null;
password = null;
email = null;
}
}
Every FormBean must inherit the ActionForm class, FormBean is responsible for page requests Encapsulation. That is, the HTTP request is encapsulated in an object. One thing that needs to be explained is that multiple HTTP requests can share a FormBean, which is convenient for 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 Acti??μ???? ?? ;onForward 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 the user request and perform corresponding database operations, omitted
*/
}
}
FormBean is generated for Provide data to ActionBean, and the data encapsulated in FormBean can be obtained in ActionBean. After corresponding logical processing, the business method is called to complete the corresponding business requirements.
The evolution of Servlet: In the conventional three-layer structure of JSP, Servlet, and JavaBean, JSP implements the functions of View, Servlet implements the functions of Controller, and JavaBean implements Model.
In Struts, the Servlet under normal circumstances is split into three parts: ActionServlet, FormBean, and ActionBean. ActionServlet cooperates with Struts-config.xml to complete page navigation full-time, and is no longer responsible for specific data acquisition and corresponding logic. These two functions are completed by FormBean and ActionBean.
Struts advantages and disadvantages
Advantages:
Struts, like Tomcat, Turbine and many other Apache projects, is open source software, which is one of its major advantages. Allow developers to have a deeper understanding of its internal implementation mechanism.
In addition, the advantages of Struts are mainly reflected in two aspects: Taglib and page navigation. Taglib is the tag library of Struts. It can be used flexibly and can greatly improve development efficiency. In addition, as far as domestic JSP developers are concerned, in addition to using the common tags that come with JSP, they rarely develop their own tags. Perhaps Struts is a good starting point.
Regarding page navigation, I think that will be a development direction in the future. In fact, doing so will make the context of the system clearer. Through a configuration file, you can grasp the connection between various parts of the entire system, which is of great benefit for later maintenance. This advantage becomes even more obvious when another group of developers takes over the project.
Disadvantages:
Taglib is a major advantage of Struts, but for beginners, it requires a continuous learning process and may even disrupt your web page writing habits. However, when you get used to it, you I'll think it's really great.
Struts divides the MVC Controller into three, which not only makes the structure clearer, but also increases the complexity of the system.
Struts has been around for less than half a year, but it has gradually been used more and more in commercial software. Although it still has many shortcomings, it is a very excellent J2EE MVC implementation method. If your system is going to adopt the J2EE MVC architecture, then you might as well consider Struts.
Struts implementation experience:
1. Project development based on Struts architecture requires first of all a good overall plan, which modules are included in the entire system, and how many modules are needed for each module FormBean and ActionBean, etc., and it is best to have a dedicated person responsible for the management of Struts-config.xml. The difficulty in developing projects based on Struts lies in configuration management, especially the management of Struts-config. Struts. Mastering Struts requires a process. For a skilled JSP programmer, self-study will take about half a month. If combined with titls, it will take longer
3. If you use taglib extensively in web pages, your artist will make some sacrifices. This sacrifice is especially obvious when you combine it with Tiles to enhance functionality. Of course, your choice of functionality and aesthetics is up to you
4. Taglib is a good thing, but using it flexibly requires a process. If you don’t want to spend too much time on Taglib, then you only need to understand and use FORM Just leave the relevant tags and leave the other tags. We will look at them later. First, study ActionServlet and Struts-config.xml. You will feel a sense of accomplishment. 5. Is Struts only suitable for large-scale projects? No! Struts is suitable for projects of all sizes. Of course, for large projects, its advantages are more obvious.
The above is the content of Struts entry experience (2). For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!