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

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
Org.appache.struts.ActionServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<uri-pattern>*.do</url-pattern>
</servlet-mapping>

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.

<servlet>
<servlet-name>
Action
</servlet-name>


</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/web-inf/struts-config.xml
</param-value>
</init-param>
</servlet>

Initialization parameters used in Struts1.1

Config

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.

Detail

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.

<welcome-file-list>
<welcome-file>
welcome.jsp
</welcome-file>
<welcome-file>
index.jsp
</welcome-file>
</welcome-fiel-list>

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:

<global-forwards>

Path= "Helloword.do"/>
</global-forwords>

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:

<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>

Fourth Step: Configure error handling

<error-page>
<error-code>404</error-code>
<location>/commom/404.jsp</location>
</error-page>
<error-page>
<error-code>505</error-code>
<location>/commom/505.jsp</location>
</error-page>

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

<error-page>
<exception-type>
Java.io.IOException
</exception-type>
<location>
/common/system_ioerror.jsp
</location>
</error-page>

Fifth step: Configure the Struts Tag library

Use the customized tag libraries to configure them

<taglib>
<tag-uri>
/web-inf/struts-html.tld
<tag-uri>
<taglib-location>
/web-inf/struts-html.tld
</taglib-location>
</taglib>

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?,
Global-exeception?,global-forward?,
Action-mapping,controller?,
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-beans>
<form-bean name= "Helloform"
Type= "Hello. Helloform "/>
</form-beans>

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

<form-beans>

Type= "Hello. Helloform "/>

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

Type= "Java.lang.String"/>

Type= "Java.lang.String" >

Type= "java. Integer "Initial=" >
</form-beans>

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

ClassName

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

Name

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

Path

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

redirect

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

<global-forwards>

Path= "/action1.do"/>

Path= "/action2.do"/>
</global-forwards>

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

Or

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

Base

Wrapping the base element of HTML

button

INPUT element that wraps the button type of HTML

Cancel

Wrapping the HTML Cancel button

CheckBox

Wrapping an input field for an HTML checkbox type

Errors

Conditionally display some error messages, display actionerrors information

File

Wrapping an HTML file on a transport field

Form

Defining an HTML FORM element

Frame

Wrapping HTML Frame elements

Hidden

Wrapping HTML Hidden input fields

Html

Wrapping HTML elements in HTML

Image

Wrapping an input field of type "image"

Img

Wrapping an IMG element of HTML

Javascript

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

Link

Wrapping hyperlinks

Messages

Conditionally display some hint information, display actionmessages information

Multibox

Packaging multiple-selection input boxes

Option

Wrap a Select input box

Options

Packing Batch selection input box

Optionscollection

Packing Batch Select input box set

Password

Packing ciphertext input Box

Radio

Packing a radio input box

Reset

button to wrap the reset feature

Rewrite

Wrapping a URL

Select

Wrap a Select input box

Submit

Wrapping a Submit button

Text

Wrapping a text input box

TextArea

Wrap a memo input box

Actionform

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

Method

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: http://java.ccidnet.com/art/3737/20051212/391299_5.html

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.