1. How to install struts:
FirstHttp://jakarta.apache.org/StrutsDownload struts. We recommend that you use release. The current maximum version is 1.2.6. There are multiple OS versions (Windows, Linus ...), after downloading and decompress the 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: tomcatwebapps, and restart tomcat. Open your browser and enter:Http: // localhost: 8080/Struts-example/index. jspIf you can see the dark blue icon of "powered by struts", it means the operation 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 also provides a system practical object: XML processing, throughJavaReflection APIs Automatic ProcessingJavaBeans attributes, International prompts and messages, etc.
2. exercise as an example:
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 formal development, you need to create this project in tocmat (my tomcat is installed in C: omcat. A quick method is to create a directory test under C: omcatwebapps, and then copy
Copy the WEB-INF directory to the test directory, and then clear the SRC and classes directories under testweb-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 developmentJSPPut the file in the test directory,JavaThe original file is stored in testweb-infsrc, And the compiled class file is stored in testweb-infclasses.
Registration page: reguser. jsp
<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" %>
<% @ Taglib uri = "/WEB-INF/Struts-bean.tld" prefix = "Bean" %>
<% @ Taglib uri = "/WEB-INF/Struts-html.tld" prefix = "html" %>
<HTML: HTML locale = "true">
<Head>
<Title> reguser </title>
<HTML: base/>
</Head>
<Body bgcolor = "white">
<HTML: errors/>
<HTML: Form Action = "/reguseraction" Focus = "LOGNAME">
<Table border = "0" width = "100%">
<Tr>
<TH align = "right">
LOGNAME:
</Th>
<TD align = "Left">
<HTML: Text property = "LOGNAME" size = "20" maxlength = "20"/>
</TD>
</Tr>
<Tr>
<TH align = "right">
Password:
</Th>
<TD align = "Left">
<HTML: Password property = "password" size = "20" maxlength = "20"/>
</TD>
</Tr>
<Tr>
<TH align = "right">
E-mail:
</Th>
<TD align = "Left">
<HTML: Password property = "email" size = "30" maxlength = "50"/>
</TD>
</Tr>
<Tr>
<TD align = "right">
<HTML: Submit property = "Submit" value = "Submit"/>
</TD>
<TD align = "Left">
<HTML: reset/>
</TD>
</Tr>
</Table>
</Html: Form>
</Body>
</Html: HTML>
ThisJSPThe page is different from the commonJSPPages, because it uses a lot of taglib, these taglib may be difficult for beginners, but this is one of the essence of struts. Flexible use will greatly improve the development efficiency.
Struts-config.xml:
<Struts-config>
<Form-beans>
<Form-bean name = "reguserform"
Type = "org. cjea. Struts. example. reguserform"/>
</Form-beans>
<Action-mappings>
<Action Path = "/reguseraction"
Type = "org. cjea. Struts. example. reguseraction"
Attribute = "reguserform"
Scope = "request"
Validate = "false">
<Forward name = "failure" Path = "/messagefailure. jsp"/>
<Forward name = "success" Path = "/messagesuccess. jsp"/>
</Action>
</Action-mappings>
</Struts-config>
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.
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;
}
}
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 conventionalJSP, Servlet,JavaIn the layer-3 bean structure,JSPImplement the view function. servlet implements the controller function,JavaBean implementation 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.
3. Struts advantages and disadvantages
Advantages:
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 additionJSPIn additionJSPIn addition to the built-in common tags, you rarely develop your own tags. Maybe struts is 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.
Disadvantages:
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 goodJ2EEMVC implementation method. If your system is ready to useJ2EEMVC Architecture, consider struts.
4. Struts implementation experience:
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. Struts requires a process and a skillfulJSPIt may take about half a month for programmers to learn by themselves. If titls is used, it takes a longer time.
3) If you use taglib extensively on the webpage, your artist will make part of the sacrifice. 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 the several tags related to form, let's keep the other marks. Later, study actionservlet and Struts-config.xml first, and you will feel a sense of accomplishment.
5) is struts only suitable for large projects? No! Struts is suitable for projects of various sizes. Of course, for large projects, its advantages are more obvious.