Beginner 1, configure Web.xml files for structs applications
First Step: Configure Actionservlet
<servlet> <servlet-name>action</servlet-name> <servlet-class>org.appache. Structs. 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 Actionservlet supports multithreading, and the current structs 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> org.appache. Structs. Actionservlet </servlet-class> <init-param><param-name>config</param-name>< Param-value>/web-inf/structs-config.xml</param-value></init-param> </servlet>
|
Table 2-1. Initialization parameters used in Struts1.1
Name of parameter |
Meaning/default value |
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 |
A relative path indicates the configuration file location of the child application, which is rarely used in the case of a child application, and is not described in many ways. |
Debug |
Set 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
Can contain multiple A child element that first finds the first File.
Because You cannot configure a servlet map in an element, so you cannot directly use the structs action as a welcome file, and you can use a workaround to invoke the structs action in the welcome file.
First, create a global forwarding entry for the invoked action in the structs configuration file, for example:
<global-forwards> <forward name= "Welcome" path= "Helloword.do"/></global-forwords>
|
Create a welcome.jsp file, and finally configure the welcome.jsp file in the Web.xml file 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>
|
Java exception Configuration that can also be captured for web containers , you need to set A child element that specifies the Java exception class. The Web container might catch the following exception: 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 structs Tag Library
Use the customized tag libraries to configure them:
<taglib> <tag-uri>/web-inf/structs-html.tld <tag-uri> <taglib-location> /web-inf/structs-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, structs configuration file
The structs framework reads its configuration file at startup and creates and configures various structs components based on it.
(1) element
The element is the root element of the structs configuration file, and the element has 8 child elements. Its DTD is defined as follows:
<! ELEMENT structs-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 location in the configuration file of these elements is reversed, an error is generated when the structs application starts.
Elements
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.structs.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> <form-bean name= "helloform" type= "Hello." Helloform "/> <form-bean name= UserForm" type= "Org.apache.structs.action.DynaAction" > <from-prope Rty name= "FirstName" type= "java.lang.String"/> <form-property name= "secondname" type= "java.lang.String" > <form-property name= "Age" type= "java. Integer "initial=" > </form-beans>
|
The
(3) element
is used to configure exception handling, and the element can contain 0 or more elements. The
element is used to set the mapping between the Java exception and the exception-handling class Org.apache.structs.action,exception hander. The
(4) element
element is used to declare a global turn , has 0 or more elements. The element is used to map a logic to a specific URL
Property |
Description |
ClassName |
the configuration class that corresponds to the element, default to org.apache.structs. 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> <forward name= "forward1" path= "/action1.do"/><forward name= "Forward1" 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) Elements
element contains 0 or more Elements. The element describes the mapping from a specific request path to the corresponding action class.
(6) Elements
element is used to configure Actionservlet.
(7) < message-resources> elements
element is used to configure the Resource Bundle,resource Budle for storing localized message files.
Structs Label
HTML tags
Label name Description
base element for base wrapper HTML
button wrapper HTML INPUT element of button type
Cancel Wrapper HTML Cancel button
CheckBox wrapper input field for HTML checkbox type
Errors to display some error messages conditionally, display actionerrors information
Transport into domain on file wrapper HTML files
form defines an HTML form element
Frame wrapper HTML FRAME element
Hidden wrapper HTML hidden input field
HTML elements in HTML wrapper HTML
Image wrapper input fields of type "image"
IMG Wrapper HTML img Element
JavaScript wrappers provide JavaScript validation scripts based on the validation rules provided by Validatorplugin
Link Wrap Hyperlink
Messages conditionally display some hints to display actionmessages information
Multibox Packaging Multi-selection input box
Option wraps a Select input box
Options packaging Batch selection input box
Optionscollection Packing Batch Select input box set
Password Packing ciphertext input box
Radio Packaging Radio Input box
Reset button to wrap "reset" function
Rewrite wrap a URL
Select to wrap a selection input box
Submit Package A submission button
Text Wraps a TextBox
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. Base class Actionform is not real
of the case.
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 the attribute
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
There are many ways to implement it.
5.2.6 Actionform as a firewall
When the request is submitted, Actionservlet uses an automated assembly mechanism to set the requested
Actionform property. 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. On the JSP page, you can refer to a nested bean like this:
5.7.2 Distribution (Dispatch)
Struts developers typically use the same action to process related actions. A common practice to select operations is to
Hidden properties are used in HTML forms. BaseForm's dispatch properties can also be used for this purpose:
public void Setdispatch (String dispatch);p Ublic 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.
Valid only for action objects that are invoked in this actionmapping. From the inside of the action object, forward usually chooses like this:
Actionforward forward = Mapping.findforward ("continue");
|