Getting Started with beginners: Getting started with basic configuration in struts

Source: Internet
Author: User
Tags button type error handling exception handling html form tld

1. Apply configuration Web.xml file for struts

First Step: Configure Actionservlet



Note: Regardless of how many applications are included in the application, you only need to configure a actionservlet, because the Actionservlet supports multithreading, and the current struts framework allows only one actionservlet to be configured in the application.

Step two: Initialize the parameters to initialize the configuration of the servlet's running environment.



Initialization parameters used in Struts1.1


Indicates the configuration file location of the struts application in a relative way. If not set, the default value is/web-inf/struts-config.xml.

CONFIG/SUB1 indicates the location of the child application's configuration file in a relative path, which is rarely used in a child application, and is not described in this way.

Debug sets the debug level of the servlet to control the verbosity of the logging. The default is 0, which records relatively minimal log information.


Set the debug level for Digester, Digester is a framework used by the struts framework to parse XML configuration files, which allows you to view the parse logs for different levels of detail. The default is 0, which records relatively minimal log information.

Step Three: Configure the Welcome list

When a client accesses a Web application, if only the root URL of the Web application is given, without specifying a specific filename, the Web container automatically invokes the welcome file for the Web application.


Description: You can include multiple child elements, first to find the first file.

Because the servlet map cannot be configured in an element, you cannot directly use the Action of struts as a welcome file, and you can use a workaround to invoke the struts action in the welcome file. First, create a global forwarding entry for the invoked action in the struts configuration file, for example:


Path= ""/>

To create a welcome.jsp file

Finally, the welcome.jsp file in the Web.xml file is configured as a welcome file with the following code:


Fourth Step: Configure error handling


You can also configure the Java exception that is captured by the Web container, and you need to set child elements that specify the Java exception class. The Web container might catch the following exceptions:

RuntimeException or Error

Servletexception or its subclasses

IOException or its subclasses


Fifth step: Configure the Struts Tag library

Use the customized tag libraries to configure them


Used to specify the relative or absolute URI address of a tag library to which the Web application accesses the tag library.

Specifies the physical location of the label description file in the file resource system.

2.Struts configuration file

The struts framework reads its configuration file at startup, creating and configuring various struts components based on it.

1 elements

element is the root element of the struts configuration file, and the element has 8 child elements. Its DTD is defined as follows:

<! ELEMENT struts-config (Data-sources?,formbean?,
message-resources*,plug-in*) >

In the struts configuration, each child element of the element must be configured in the order specified in the preceding DTD, and if the position in the configuration file of these elements is reversed, an error is generated when the struts application starts.

1. Element

The element contains 0, one, or more child elements that are used to configure a specific data source that can contain multiple child elements to set various properties of the data source.

Once you have configured the data source, you can access the data source in the action class, and the Getdatasource (Httprequrst) method is defined in the Org.apache.struts.action.Action class, which is used to get a reference to the data source object.

DataSource DataSource;
Connection Connection;
DataSource =getdatasource (Request);
Connection=datasource.getconnection ();

You can also declare multiple data sources in a configuration file, at which point you need to assign a unique key value to each data source, and then change the value to identify the specific data source.

2. Element

Elements are used to configure multiple actionform beans, including multiple child elements, each containing multiple attributes, Classname,name,type

<form-bean name= "Helloform"
Type= "Hello. Helloform "/>

Note: Configure the dynamic Actionform Bean, and you must also configure elements of the element.


Type= "Hello. Helloform "/>

Type= "Org.apache.struts.action.DynaAction" >

Type= "Java.lang.String"/>

Type= "Java.lang.String" >

Type= "java. Integer "Initial=" >

3. Element

For configuring exception handling, an element can contain 0 or more elements.

element is used to set the mapping between the Java exception and the exception handling class Org.apache.struts.action,exception hander.

4. Element

element is used to declare a global forwarding relationship, consisting of 0 or more elements. element is used to map a logic to a specific URL


The configuration class that corresponds to the element, default is Org.apache.struts.action.ActionForWard


The logical name of the forwarding path, which is required when the


The URI specified or redirected. This item must be preceded by a "/"


This is true to indicate that a redirection is performed, and when this entry is false, a request forwarding is indicated.


Path= "/"/>

Path= "/"/>

If jsp1.jsp sends the request to Action1, you can use the following code:


<logic:forward name= "Forward1" >

If the Action1 execute () method sends the request to jsp2.jsp, you can use the code:

Return (Mapping.findforward ("Forward2"))

5. Element

An element contains 0 or more elements. The element describes the mapping from a specific request path to the corresponding action class.

6. Element

element is used to configure Actionservlet.

7.< message-resources> Element

element is used to configure the Resource Bundle,resource Budle for storing localized message files.


Wrapping the base element of HTML


INPUT element that wraps the button type of HTML


Wrapping the HTML Cancel button


Wrapping an input field for an HTML checkbox type


Conditionally display some error messages, display actionerrors information


Wrapping an HTML file on a transport field


Defining an HTML FORM element


Wrapping HTML Frame elements


Wrapping HTML Hidden input fields


Wrapping HTML elements in HTML


Wrapping an input field of type "image"


Wrapping an IMG element of HTML


Wrapper provides JavaScript validation scripts based on the checksum rules provided by Validatorplugin


Wrapping hyperlinks


Conditionally display some hint information, display actionmessages information


Packaging multiple-selection input boxes


Wrap a Select input box


Packing Batch selection input box


Packing Batch Select input box set


Packing ciphertext input Box


Packing a radio input box


button to wrap the reset feature


Wrapping a URL


Wrap a Select input box


Wrapping a Submit button


Wrapping a text input box


Wrap a memo input box


Actionform's requirements

Creating a actionform is not difficult, but your class must meet some requirements:

Actionform must be extended from Org.apache.struts.ActionForm. The base class Actionform is not instantiated. Actionform must define a public property for each HTML control that should be collected from the request.

(Struts 1.0 requires that each attribute be mutator and accessor.) Struts 1.1 is not so strict)

Actionform may also have to meet some of the optional requirements:

If you ask Actionform to check them before passing the attributes to the action, you must implement the Validate


If you want to initialize the attribute before assembly, you must implement reset, which is invoked before the Actionform assembly.

Dynamic Actionform

Actionform as a type converter

One force point for actionform is that you should use string and Boolean attributes. In fact, this means that attributes must be converted from one type to another. Most applications also require attributes, such as phone numbers or numbers, to appear in a formatted manner. The core Java package provides some tools to do this, but it is still a challenge to clearly integrate them into the application.

Struts developers often include helper methods in Actionform for type conversions. Helper methods can be implemented in a number of ways, which we will describe later.

5.2.6 Actionform as a firewall

When a request is submitted, Actionservlet uses an automated assembly mechanism to set the requested, Actionform property from the request parameter. This allows you to control which request parameter by controlling which Actionform attribute is exposed. is acceptable. This also means that if you are actionform careless design you may lose the ability to control which parameters are acceptable. Actionform must not contain a property method that looks like a JavaBean property, but cannot be set from an HTTP request.

5.3.4 Actionform can nest other beans

Because the struts tag extension and the automatic assembly mechanism all support dot syntax to access other beans from actionform. This is a convenient way to assemble the existing bean through actionform. In the JSP page you can refer to a nested bean like this:

Propety= "Values.telephonetext"
Size= "14"
Maxlength= "/>"

Distribution (Dispatch)

Struts developers typically use the same action to process related actions. A common practice is to select actions that use hidden attributes in an HTML form. BaseForm's dispatch properties can also be used for this purpose:

public void Setdispatch (String dispatch);
Public String Getdispatch ();

The Struts configuration provides a actionforward of nested levels:

Global Actionforward is effective for all action objects in the whole application;

Local actionforward are defined in the actionmapping element. Only for those in this

The Action object invoked in Actionmapping is valid.

From the inside of the action object, forward usually chooses like this:

Mapping.findforward ("continue");

Go from:

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.

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.