Java: getting started with Struts

Source: Internet
Author: User
Tags tld
1. How to install struts:

First, download struts at http://jakarta.apache.org/struts. we recommend that you use the release version. 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 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.

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.

Project establishment:

Before formal development, you need to create this project in tocmat (my tomcat is installed in C: omcat. A faster way to create a directory test in C: omcatwebapps, and then copy the WEB-INF directory under C: omcatwebworkflow Struts-example to the test directory, 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. String 7

During development, place the JSP file under the test directory, the original Java file under testweb-infsrc, And the compiled class file under 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">

String 8

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>

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.
Struts-config.xml:
String 3

<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"/>

String 1

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

Formbean: reguserform
String 6

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;
}
} String 6

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

String 3

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.
*/
}
}

String 6

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.

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 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. String 8

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 good J2EE MVC implementation method. If your system is ready to adopt the J2EE MVC 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. 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 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.

String 1

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.

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.