This section describes the basic principles of the Struts 1.1 framework, as well as the installation of Struts 1.1 and a simple example.

Source: Internet
Author: User
Tags tld
This article describes the basic principles of the Struts 1.1 framework, as well as the installation of Struts 1.1 and a simple example.
1. Origin of struts
Struts is part of the Apache Jakarta project. The goal of this project is to provide an open-source framework for the establishment of Java Web applications. Currently, the general version is 1.1, but 1.2 also appears. The Struts framework can be used to improve and improve the application of Java Server Pages (JSPs), Servlet, label library, and object-oriented technology in Web applications. Using the Struts framework can reduce the development time when you apply the Model-View-controller design pattern to improve development efficiency. Applying struts to the J2EE client should be a good choice.

2. Working Principle of struts
When talking about struts, we have to mention the MVC design pattern. MVC is short for Model-View-controller. It is a common design pattern in Web applications. MVC weakens the coupling between the business logic interface and the data interface, and makes the view layer more changeable.
Struts is an implementation of MVC. It combines JSP, Java Servlet, Java Bean, taglib, and other technologies. Let's take a look at the working principle of the Struts framework:

Control: In struts, actionservlet acts as a controller. 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 Form (usually called frombean) with the corresponding request parameters and passing it to the action class (usually called actionbean ). The logging class implements the core business logic. It can access Java Beans or call ejbs. All of these control logics are configured using Struts-config.xml files.

View: JSP is used to control page output. It receives data from the action form and displays data using HTML, taglib, Bean, logic, and so on.

Model: In struts, there are three types of beans: Action, actionform, EJB, or Java Bean. Actionform is used to encapsulate the customer request information. Action obtains the data in actionform and is then processed by EJB or Java Bean.

3 struts advantages and disadvantages
Advantages: 1. Using the taglib provided by struts can greatly save development time. 2. Representation and logic separation. 3. Easy Maintenance and expansion. 4. easy team development
Disadvantage: a large number of tags are used, which is difficult for beginners.

4. Install Struts 1.1
Installing Struts 1.1 is relatively simple. Suppose we use Tomcat 5.0 and install it under D:/tomcat. First, go to the http://jakarta.apache.org/struts/ to download Struts 1.1, unzip, you can see the jakarta-struts-1.1 directory, under this directory, You can see such a directory: webapps. this directory contains multiple *. war, we copy the struts-blank.war file to D:/tomcat/webapps, restart tomcat, under Tomcat webapps will generate a Struts-blank struts development environment. In addition, struts-example.war in webapps under the directory you decompress is a good struts learning tutorial, if you want to learn more, please visit the http://jakarta.apache.org/struts or download the struts in Action E version.

5. A simple example
5.1 logon. jsp
<% @ Taglib uri = "/WEB-INF/struts-html.tld" prefix = "html" %>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<%
/**
* This page shows logon interface.
* Title: logon System
* Description: logon page
* Copyright: Copyright (c) 2004
* Company: www.ewe.com.cn
* @ Author bluesunny * @ Version 1.0
*/
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
%>
<HTML>
<Head>
<Title> logon </title>
</Head>
<Body>
<HTML: errors/>
<HTML: Form Action = "/logon" Focus = "username">
Username: <HTML: Text property = "username" size = "25"/>
Password: <HTML: Password property = "password" size = "25"/>
<HTML: Submit property = "Submit"/>
</Html: Form>
</Body>
</Html>
The page is the HTML Tag applied to struts on the user login page.
5.2 logonform. Java
Package com. logon. app;
Import javax. servlet. http .*;
Import org. Apache. Struts. Action .*;
/**
* This page shows logonform. Java
* Title: logon System
* Description: logonform page
* Copyright: Copyright (c) 2004
* Company: www.ewe.com.cn
* @ Author bluesunny
* @ Version 1.0
*/
Public class logonform extends actionform {
Private string username;
Private string password;
Public loginform (){
Username = NULL;
Password = NULL;
}
Public void setusername (string username ){
This. Username = username;
}
Public String GetUserName (){
Return this. Username;
}
Public void setpassword (string password ){
This. Password = password;
}
Public String GetPassword (){
Return this. Password;
}
Public void reset (actionmapping mapping, httpservletrequest request)
{
Username = NULL;
Password = NULL;
}
}
This page is actionform bean, which is mainly used to encapsulate the user name and password of the client.
5.3 logonaction. Java
Package com. logon. app;
Import java. Io .*;
Import java. SQL .*;
Import org. Apache. Struts. action. Action;
Import org. Apache. Struts. Action. actionforward;
Import org. Apache. Struts. Action. actionmapping;
Import org. Apache. Struts. Action. actionform;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import org. Apache. Struts. Action .*;
/**
* This page shows logonaction. Java
* Title: logon System
* Description: logonaction page
* Copyright: Copyright (c) 2004
* Company: www.ewe.com.cn
* @ Author bluesunny
* @ Version 1.0
*/
Public class logonaction extends action {
Public actionforward execute (actionmapping, actionform, httpservletrequest request, httpservletresponse response) throws exception {
Logonform form = (logonform) actionform;
Actionerrors errors = new actionerrors ();
String username = form. GetUserName ();
String Password = form. GetPassword ();
If (username. Equals ("bluesunny") & password. Equals ("bluesunny ")){
Return actionmapping. findforward ("success ");
} Else {
Errors. Add ("login", new actionerror ("Logon. Failed "));
Saveerrors (request, errors );
Return actionmapping. findforward ("failed ");
}
}
}
Obtain the data in actionform for processing, and return the processing result to the JSP page.
5.4 web. xml
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/j2ee/dtds/web-app_2_3.dtd>
<Web-app>
<Display-Name> struts logon application </display-Name>
<! -- Standard action servlet configuration (with debugging) -->
<Servlet>
<Servlet-Name> action </servlet-Name>
<Servlet-class> org. Apache. Struts. Action. actionservlet </servlet-class>
<Init-param>
<Param-Name> config </param-Name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> 2 </param-value>
</Init-param>
<Init-param>
<Param-Name> detail </param-Name>
<Param-value> 2 </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup>
</Servlet>
<! -- Standard action Servlet Mapping -->
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>
<! -- The welcome file list -->
<Welcome-file-List>
<Welcome-File> logon. jsp </welcome-File>
</Welcome-file-List>
<! -- Struts tag library descriptors -->
<Taglib>
<Taglib-Uri>/tags/Struts-bean </taglib-Uri>
<Taglib-location>/WEB-INF/struts-bean.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-Uri>/tags/Struts-HTML </taglib-Uri>
<Taglib-location>/WEB-INF/struts-html.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-Uri>/tags/Struts-logic </taglib-Uri>
<Taglib-location>/WEB-INF/struts-logic.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-Uri>/tags/Struts-nested </taglib-Uri>
<Taglib-location>/WEB-INF/struts-nested.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-Uri>/tags/Struts-tiles </taglib-Uri>
<Taglib-location>/WEB-INF/struts-tiles.tld </taglib-location>
</Taglib>
</Web-app>
5.5 struts-config.xml
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype Struts-config public
"-// Apache Software Foundation // DTD struts configuration 1.1 // en"
Http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd>
<Struts-config>
<Form-beans>
<Form-bean name = "logonform" type = "com. logon. App. loginform"/>
</Form-beans>
<Action-mappings>
<Action
Path = "/logon" type = "com. logon. App. logonaction" name = "logonform" Scope = "request" input = "Logon. jsp">
<Forward name = "success" Path = "/success. jsp"/>
<Forward name = "failure" Path = "/logon. jsp"/>
</Action>
<Message-resources parameter = "resources. Application"/>
</Struts-config>
5.6 application. Properties
Logon. Failed = logon failed! Please logon in again!
5.7 deploy logon Application
Create a success. jsp and enter: "successflly! ".
Compile the source file and deploy the application in Tomcat. Assume that the application is deployed in the app directory. We can start the browser to access: http: // localhost: 8080/APP /.
5.8 directory structure
/WEB-INF:
/WEB-INF/lib: struts. Jar
/WEB-INF/classes:
/WEB-INF/classes/COM/logon/APP: *. Class
/WEB-INF/classes/resources: *. Properties
6. Summary
This article describes the working principle and Installation Method of struts in detail, and provides a login example as a Struts framework application. In addition, this article also describes the directory structure of struts, which greatly reduces the learning burden for beginners and can be used as an entry to struts.

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.