Beginners: getting started with basic struts configurations

Source: Internet
Author: User
Tags button type html file upload tld

1. Configure the Web. xml file for the struts Application

Step 1: 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: No matter how many sub-applications are included in the application, you only need to configure one actionservlet. Because actionservlet supports multiple threads, the current Struts framework only allows you to configure one actionservlet in the application.

Step 2: Initialize parameters to initialize the servlet runtime environment.

<servlet>
  <servlet-name>
action
</servlet-name>
  <servlet-class>
org.appache.struts.ActionServlet
</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

Specify the location of the configuration file of the struts application in relative paths. If not set, the default value is/WEB-INF/struts-config.xml.

Config/sub1 specifies the location of the configuration file of the sub-application in a relative path. Generally, sub-applications are rarely used and are not described here.

Debug sets the debug level of the servlet to control the log record details. The default value is 0. The minimum log information is recorded.

Detail

Set the debug level of digester. digester is a framework used by the Struts framework to parse XML configuration files. With this setting, you can view resolution logs of different levels. The default value is 0. The minimum log information is recorded.

Step 3: configure the welcome list

When you access a web application, the Web Container automatically calls the welcome file of the Web application if only the root URL of the Web application is provided and no specific file name is specified.

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

Description: it can contain multiple child elements. First, find the first file.

Because servlet ing cannot be configured in the element, you cannot directly use the struts action as a welcome file. You can use a work ing method to call the struts action in the welcome file. First, create a Global Forwarding item for the called action in the struts configuration file, for example:

<global-forwards>
  <forward name=”welcome”
path=”HelloWord.do”/>
  </global-forwords>

Create a welcome. jsp file

Finally, configure the welcome. jsp file as a welcome file in the web. xml file. The Code is as follows:

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

Step 4: 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 Java exceptions captured by web containers. You need to set sub-elements to specify Java exception classes. The Web container may catch the following exceptions:

Runtimeexception or error

Servletexception or its subclass

Ioexception or its subclass

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

Step 5: configure the struts tag Library

Use the custom tag library to configure them

<taglib>
  <tag-uri>
/WEB-INF/struts-html.tld
<tag-uri>
  <taglib-location>
/WEB-INF/struts-html.tld
</taglib-location>
  </taglib>

Specifies the relative or absolute URI address of the tag library. Web applications access the tag library based on this URI.

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

2. Struts configuration file

When the Struts framework is started, it reads its configuration file and creates and configures various struts components based on it.

1 Element

The element is the root element of the struts configuration file and has eight 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 struts configuration, each child element of the element must be configured according to the sequence specified by the DTD above. If the position in the configuration file of these elements is reversed, an error is generated when the struts application is started.

1. Elements

An element contains zero, one, or more child elements. It is used to configure a specific data source. It can contain multiple child elements and is used to set various attributes of the data source.

After configuring the data source, you can access the data source in the action class. apache. struts. action. the getdatasource (httprequrst) method is defined in the action class, which is used to obtain references to data source objects.

DataSource dataSource;
Connection connection;
dataSource =getDataSource(request);
connection=dataSource.getConnection();

You can also declare multiple data sources in the configuration file. In this case, you need to assign a unique key value for each data source and change the value to identify the specific data source.

2. Elements

Element is 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: to configure the dynamic actionform bean, you must also configure the element.

<form-beans>
  <form-bean name="HelloForm"
type="hello.HelloForm"/>
  <form-bean name="userForm"
type="org.apache.struts.action.DynaAction">
  <from-property name="firstname"
type="java.lang.String"/>
  <form-property name="secondname"
type="java.lang.String">
  <form-property name="age"
type="java.Integer" initial="18">
  </form-beans>

3. Elements

Used to handle configuration exceptions. The element can contain zero or multiple elements.

Element is used to set the ing between Java exceptions and exception handling classes org. Apache. Struts. Action and exception hander.

4. Elements

An element is used to declare a Global Forwarding relationship. It consists of zero or multiple elements. Element is used to map a logic to a specific URL

Classname

Configuration class corresponding to the element. The default value is org. Apache. Struts. Action. actionforward.

Name

The logical name of the forwarding path, which must be

Path

The specified or redirected Uri. Required. It must start "/".

Redirect

If this parameter is set to true, the redirection operation is performed. If this parameter is set to false, the request is forwarded.

<global-forwards>
  <forward name="forward1"
path="/Action1.do"/>
  <forward name="forward1"
path="/Action2.do"/>
  </global-forwards>

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

Or

<logic:forward name=”forward1”>

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

return(mapping.findForward(“forward2”))

 

 


5. Elements

The element contains zero or multiple elements. The element describes the ing from a specific request path to the corresponding action class.

6. Elements

Element is used to configure actionservlet.

7. <message-resources> element

Element is used to configure resource bundle, and resource budle is used to store localized message files.

Base

Package HTML base elements

Button

Encapsulate the input elements of the HTML button type

Cancel

Wrap the HTML cancel button

Checkbox

Wrap HTML checkbox input fields

Errors

Conditional display of some error messages and actionerrors Information

File

Packaging HTML file upload input field

Form

Define HTML form elements

Frame

Wrap HTML Frame Elements

Hidden

Wrap HTML hidden input fields

Html

Wrap HTML elements in HTML

Image

Wrap input fields of the "image" Type

IMG

IMG elements for HTML Packaging

Javascript

Wrap the Javascript verification script provided according to the validation rules provided by validatorplugin

Link

Wrap hyperlink

Messages

Displays prompt information and actionmessages information conditionally.

Multibox

Packing multiple choice input box

Option

Package A selection input box

Options

Input box for packaging batch Selection

Optionscollection

Package a batch of selection input box sets

Password

Encapsulated ciphertext input box

Radio

Package single choice input box

Reset

Button for packaging the "reset" Function

Rewrite

Package a URL

Select

Package A selection input box

Submit

Wrap a submit button

Text

Wrap a text input box

Textarea

Wrap a remarks input box

Actionform

Requirements of actionform

Creating an actionform is not difficult, but your class must meet the following requirements:

Actionform must be extended from org. Apache. Struts. actionform. The base actionform class cannot be instantiated. Actionform must define a public attribute for each HTML control that should be collected from the request.

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

Actionform may also meet some optional requirements:

If you require the actionform to verify the attributes before passing them to the action, you must implement the validate

Method;

To initialize attributes before assembly, you must implement reset, which is called before actionform assembly.

Dynamic actionform

Actionform as a type converter

A mandatory point of actionform should be string
And Boolean attributes. In fact, this means that the property must be converted from one type to another. Most applications also need some 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 applications.

Struts developers often include the Helper method in actionform for type conversion. The Helper method can be implemented in many ways, which will be described later.

5.2.6 use actionform as a firewall

When a request is submitted, actionservlet
Use an automatic assembly mechanism to set the requested actionform attribute from the request parameters. This allows you to control which actionform
Attribute exposed to control which request parameter. Is acceptable. This also means that if your actionform
Careless design you may lose the ability to control which parameter is acceptable. Actionform must not contain a property that looks like a JavaBean, but it cannot contain
The property method of the request.

5.3.4 Other beans can be nested in actionform

Because struts tag extension and Automatic Assembly support the dot syntax to access other beans from actionform. This is a convenient method. You can use actionform to assemble the existing bean. On the JSP page, you can reference a nested bean as follows:

  propety="values.telephoneText"
  size="14"
  maxlength="14"/>

Distribution (dispatch)

Struts developers usually use the same action to process related operations. A common practice is to use hidden attributes in HTML forms. The dispatch attribute of baseform can also be used for this purpose:

public void setDispatch(String dispatch);
public String getDispatch();

Struts configuration provides multiple levels of nested actionforward:

Global (global) actionforward is valid for all action objects in the application;

Local (partial) actionforward is defined in the actionmapping element. Only

The action object called in actionmapping is valid.

From the perspective of the internal action object, forward is usually selected as follows:

ActionForward forward = 
mapping.findForward("continue");

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.