Analysis of simple struts examples

Source: Internet
Author: User
Tags xslt xslt processor tld
Analysis of simple struts examples

<! -- The following is personal experience for your reference. For more information about deficiencies or errors, see -->
<! -- Xu Jianming -->

First, you need to configure your environment

Configure Tomcat Environment
1. To my machine (IP Address:, user: Guest, password not available) on the "share" packages file.
2nd, double-click j2sdk-1_4_2_02-windows-i586-p.exe to install JDK. It is best to install JDK in a root directory. Assume that the installation is under D: \ j2sdk1.4.2 _ 02.
3. install Tomcat. Install tomcat and directly decompress to the hard disk. For example, decompress the package to D: \ tomcat.
4. Configure environment variables. Right-click "my computer", select "advanced", and click "environment variable. Create the following environment variables in "system variables:
Name key value description
Java_home D: \ j2sdk1.4.2 _ 02 is the installation directory of your JDK.
Classpath.; % java_home % \ Lib
5、decompress to any directory, copy the jakarta-struts-1.1 files under the \ struts-example.war \ webapp directory to the \ Tomcat \ webapps under Tomcat
6. Go to the \ Tomcat \ bin directory and click STARTUP. BAT to start Tomcat. (After Tomcat is started, Click Shutdown. BAT to close it)
7. Run tomcat in the background, open IE, enter http: // localhost: 8080/in the address bar to go To the Tomcat homepage, and enter http: // localhost: 8080/Struts-Example: enter an example of struts.

We wish you a smooth installation :)

Next, let's take a look at the two most important files in the struts example, both of which are configuration files,

Struts-config.xml (I commented on the specific configuration)

<? XML version = "1.0" encoding = "ISO-8859-1"?>

<! Doctype Struts-config public
"-// Apache Software Foundation // DTD struts configuration 1.1 // en"

<! --
This is the struts configuration file for the example application,
Using the proposed new syntax.


<! -- =========== Action mapping definitions ============================== ==== -->
<! -- Requests suffixed with. Do in Web. XML are forwarded here for processing. This is equivalent to the struts model part, and the model part is more flexible in struts. -->
<! -- To process showinput. Do requests, the formbean used is getparameterform, which is a beans. getparameterform class. If an error occurs during the processing, index. jsp is returned. -->
<Action Path = "/showinput" type = "Beans. showaction" name = "getparameterform" Scope = "request" input = "/index. jsp"/>
<Action Path = "/success" Forward = "/success. jsp"/>
<Action Path = "/fail" Forward = "/error. jsp"/>


Web. XML (I commented on the specific configuration)

<! -- Action Servlet Mapping -->
<! -- The servlet of the system can be mapped to a file with the. Do suffix, And the suffix can be changed to any name. -->
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. DO </url-pattern>

<! -- The welcome file list -->
<! -- The default homepage of the system is index. jsp, which can be multiple. The system can be searched in order, similar to IIS -->
<Welcome-File> index. jsp </welcome-File>

<! -- Application tag library descriptor -->
<! -- Define the tag library -->
<! --
<Taglib-Uri>/WEB-INF/APP. TLD </taglib-Uri>
<Taglib-location>/WEB-INF/APP. TLD </taglib-location>


These two files are the basic configuration of JDK + Tomcat + Struts. Note: After this configuration is changed, restart tomcat!

Store your. JSP files. You can use HTML, frongpage, and other tools to compile them. This is simple for everyone. I will not write any books here,CodeAs follows:

Index. jsp

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. util. *, java. SQL. *, java. Text. *, java. Io. *" %>

<Form name = "form1" method = "Post" Action = "showinput. Do">
Enter success to return to the "success" page; otherwise, return to the "fail" page <br>
Input: <input type = "text" name = "valu"> <input type = "Submit" value = "Submit">
<A href = "success. Do"> success </a> <br>
<A href = "fail. Do"> fail </a>

Success. jsp

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. util. *, java. SQL. *, java. Text. *, java. Io. *" %>


Error. jsp

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. util. *, java. SQL. *, java. Text. *, java. Io. *" %>

Error Page!

Then in \ your folder \ WEB-INF \ Classes \ beans we write the class that needs to process the message request. The Code is as follows:

Getparameterform. Java

Package beans;
Import org. Apache. Struts. Action. actionform;
Public class getparameterform extends actionform
Private string valu = "null ";

Public getparameterform (){

Public void setvalu (string s ){
Valu = s;
Public String getvalu (){
Return valu;

Showaction. Java

package beans;
Import Java. lang. reflect. invocationtargetexception;
Import Java. util. locale;
Import javax. servlet. servletexception;
Import javax. servlet. HTTP. httpservletrequest;
Import javax. servlet. HTTP. httpsession;
Import javax. servlet. HTTP. httpservletresponse;
Import Org. apache. struts. action. *;
Import Org. apache. struts. util. *;

Public final class showaction extends action
Public actionforward execute (actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws exception {
Locale locale = getlocale (request );
Messageresources messages = getresources (request );
Httpsession session = request. getsession ();
Getparameterform userform = (getparameterform) form;
If (userform. getvalu (). Equals ("success ")){
Return (mapping. findforward ("success "));
Else {
System. Out. println (userform. getvalu ());
Return (mapping. findforward ("fail "));

Finally, you can run it.

Enter http: // localhost: 8080/your folder/In the Tomcat address/


My hotle California: gazhou travel Hall

Post: 29
Points: 49
Status: offline
Registration: 2004-10-14
Posted on: Floor 2,-11-7 11:42:00

Re: Analysis of struts simple example

Ii. Install struts

1. install the software required by struts

Java Development Kit -- we need JDK of download and install 1.2 (or later.



Servlet Container-the general choice is to download Tomcat (at least version 3.1, preferably version 4.0 or later ).



Ant build system -- if we use the struts source code release package for installation, We must download ant build system of Version 1.1 or later. Develop our own Web applications through strutsProgramYes. Ant is recommended to build our application.



Servlet API classes-to compile structs itself or use struts for applications, we need a servlet. jar package that contains Servlet and jsp api objects. Most servlet container (such as Tomcat) already comes with this file. Otherwise, we must download:

XML Parser -- structs requires an XML processor compatible with the Java API for XML parsing (JAXP) specification. xerces can be used.



Xalan XSLT Processor -- if we construct our structs system through structs source code release, we must download and install the xalan XSLT processor of 1_2_d01 or later versions (xerces already comes with xalan ). This processor is used to convert XML-based structs documents into HTML documents.

2. Construct structs through source code

1) download the structs source code release package.

2) set the ant_home environment variable to point to our ant directory.

3) set the java_home environment variable to point to our JDK directory.

4) set the servletapi_home environment variable to point to our servlet API directory (if our classpath already contains servlet. jar, you do not need to specify this directory)

5) release the structs source code package.

6) enter the structs directory and run the following command:

./Build. Sh Dist

This command will create the struts binary release package in ../Dist/structs (relative to our compilation directory ).

3. Install structs through the struts binary release package

1) download the binary release version of struts.

2) Unpack the binary release version of struts. (If we construct struts through the struts source code, the build result is already the struts of the unwrapped package ). Struts after unpacking contains the following content:

LIB/struts. jar -- This file contains all Java objects of struts. We need to copy it to the WEB-INF/lib directory of our web application.

LIB/structs. TLD -- this is a "tag library descriptor" file, which describes the free tag of the struts library. We need to copy it to the WEB-INF directory of our web application.

Webapps/struts-documentation.war -- this is a "Web application Archive" file that contains all struts documents. We can install it in servlet iner that supports servlet API 2.2 or later (Tomcat is recommended.

Webapps/struts-example.war-this is a Web application instance that demonstrates a wide range of struts features. We can install it in a servlet container compatible with servlet2.2 or later versions and jsp1.1 or later version specifications (Tomcat is recommended ).
Webapps/struts-test.war-this web application contains many tag test pages supported by struts, which can be used as an example of using struts tag.

4. Basic struts component package

There are about 15 packages of installed struts, nearly 200 classes, and the number is still being expanded. Here, we cannot describe them one by one. We can only list several major brief introductions. The following table describes the basic components of struts APIs, including
Action, actions, config, util, taglib, validator. See table 1 below:

Org. Apache. Struts. Action
Basically, the core classes and components that control the running of the entire Struts framework are in this package, such as the controller actionservlet we mentioned above. Action, actionform, actionmapping, and so on. Struts1.1 has more than 1.0 dynaactionform classes. Added the formbean generation function for Dynamic Scaling.
Org. Apache. Struts. Actions
This package is mainly used to provide the specific adapter conversion function between the client's HTTP request and business logic processing, while some of the 1.0 versions dynamically add and delete frombean classes, also replaced by the dynaactionform component of the action package in struts1.1
Org. Apache. Struts. config
Provides a ing of configuration file struts-config.xml elements. This is also a new feature in sturts1.1.
Org. Apache. Struts. util
To better support web application applications, strtuts integrates the support of some common services, such as connection pool and message source. For more information, see
Org. Apache. Struts. taglib
This is not a package, but a collection of customer label classes. The following describes the tag classes for building user interfaces, including bean tags, HTML tags, logic tags, nested tags, and template tags.
Org. Apache. Struts. validator
The validator framework is added to struts1.1 framework for dynamic configuration from form verification. For more information, see /~ Dwinterfeldt/

Iii. Struts configuration and component Introduction

The struts component configuration struts-config.xml file is described aboveArticleAs described in, I will not talk about it here. The following describes the components and configurations in the framework.

The following table lists the core classes of struts, that is, the responsibilities of the classic MVC component.

Table 2 core struts class and MVC

Class description
Actionforward user orientation or View Selection
Actionform status change data
Actionmapping status change event
Actionservlet controller, accepting user requests and status changes, and issuing View Selection
Part of the Action controller. It is used for model interaction, execution status change or status query, and informs actionservlet of the next selected view.

In addition to these core classes, Struts uses some configuration files and view helpers to communicate with controllers and models. The following table lists the struts configuration files and their roles in the architecture.

Table 3 strtuts configuration file

Applicationresources. properties stores localized information and labels so that applications can be internationalized.
The default configuration of the struts-config.xml storage controller object, including user point supported by the model, state change, status query

To expose struts configuration data to the view, the Framework provides a large number of helper classes in the form of JSP labels, such as table 4 strtus view assistant.

Mark library descriptor Purpose
JSP tag for struts-html.tld extension HTML form
Struts-bean.tld extensions process JSP tags for JavaBean
JSP tag for struts-logic.tld extended test property value

Controller component configuration

The Controller component in the framework is implemented by the org. Apache. Struts. Action. actionservlet class, which is an extension of the javax. servlet. http. httpservlet class.

Actionservlet Configuration:

We need to declare actionservlet in Web. xml and configure it for loading at startup. The following are configurable initialization parameters:

Default parameter description
Application null class of the application's resource set
Buffersize 4096 buffer size for File Upload
Config/WEB-INF/struts-config.xml profile location and name
Default content type of content text/html
Debug 0 program debugging level
Level of detailed program debugging in detail 0
Factory Null message resource factory,? Driving? Pay-as-you-go? Trial? /TD>
Formbean org. Apache. Struts. Action. actionformbean name of the class that encapsulates actionform bean Information
Forward org. Apache. Struts. Action. actionforward name of the class that encapsulates the information of the actionforward object
If locale is true, a local object is stored in the user session.
Mapping org. Apache. Struts. Action. actionforward class name that encapsulates actionmapping Information
Maxfilesize 250 m maximum size of the uploaded file
Multipartclass org. Apache. Struts. Action. actionforward name of the class for processing multiple requests
Nocache false whether the HTTP header must be set to Disabled
Null true is set to true. If the keyword is invalid, null is returned.
Tempdir is a temporary working directory provided to the program as a servlet parameter.
Validate true whether to use the new format configuration file
Vallidating true: whether to analyze the validity of the configuration file

In most cases, standard servlets can meet user needs.

When the URI of a specific request is received for the first time, actionservlet instantiates the appropriate action class and stores a reference in the action class instance with the servlet variable name. After being instantiated, the action class will be saved for future use.

Error Handling

Struts provides two classes to handle errors: actionerrors and actionerror. They both extend org. Apache. Struts. Action. Actionerrors stores a set of actionerror objects, each of which represents an independent error message. Each actionerror contains a keyword that can be mapped to the error information stored in the resource file, which is specified in the actionservlet initialization parameter.

Iv. Struts operation

Follow these steps to use struts in our own application:

1. Copy the LIB/struts. Jar directory of struts to the WEB-INF/lib directory of the Web application.

2. Copy the LIB/struts *. TLD of the struts directory to the WEB-INF directory of the Web application.

3. Modify the WEB-INF/web of Web application directory. add a <servlet> element to define the Controller servlet, and add a <servlet-mapping> element to establish the correspondence between the URI request and the servlet. See the WEB-INF/Web. xml file in the struts example for detailed syntax requirements.

4. Modify the WEB-INF/Web. xml file of the Web application directory to include the following tag library definition:

<Taglib-Uri>/WEB-INF/struts. TLD </taglib-Uri>
<Taglib-location>/WEB-INF/struts. TLD </taglib-location>
<Taglib-Uri>/WEB-INF/struts-bean.tld </taglib-Uri>
<Taglib-location>/WEB-INF/struts-bean.tld </taglib-location>
<Taglib-Uri>/WEB-INF/struts-logic.tld </taglib-Uri>
<Taglib-location>/WEB-INF/struts-logic.tld </taglib-location>

5. Create a WEB-INF/action. xml file to define the action ing relationship of our web application. For detailed syntax requirements, see the action. xml file in the struts example.

6. Add the following tag library definitions to the JSP page using the struts tag Library:

<@ Taglib uri = "/WEB-INF/struts. TLD" prefix = "struts" %>
<@ Taglib uri = "/WEB-INF/struts-bean.tld" prefix = "Bean" %>
<@ Taglib uri = "/WEB-INF/struts-logic.tld" prefix = "logic" %>

7. At last, do not forget to include the Struts. jar file in classpath when compiling the Java program of our web application.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

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.