Getting started with basic configuration in structs

Source: Internet
Author: User
Tags html file upload tld
Chapter 1 basic configuration of structs
1. Configure the Web. xml file for the structs Application
Step 1: 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: No matter how many sub-applications are included in the application, you only need to configure one actionservlet. Because actionservlet supports multithreading, the current structs framework only allows configuring 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. 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
Parameter Name Description/Default Value
Config specifies 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 sets the debug level of digester. digester is a framework used by the Struts framework to parse the xml configuration file. 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>
Note: <welcome-file-List> contains multiple <welcome-File> sub-elements. First, find the first <welcome-File> file.
Servlet ing cannot be configured in the <welcome-file-List> element, so structs action cannot be used as the welcome file directly, you can use a work und to call structs action in the welcome file. First, create a Global Forwarding item for the called action in the structs 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 <error-page> for Java exceptions captured by web containers. You need to set the <exception-type> sub-element to specify the Java exception class. 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 structs tag Library
Use the custom tag library to configure them
<Taglib>
<Tag-Uri>/WEB-INF/structs-html.tld <tag-Uri>
<Taglib-location>/WEB-INF/structs-html.tld </taglib-location>
</Taglib>
<Tag-Uri> specifies the relative or absolute URI address of the tag library. Web applications use this URI to access the tag library.
<Taglib-location> specify the physical location of the tag 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 <structs-config> element
The <structs-config> element is the root element of the structs configuration file, and the <structs-cofig> element has eight 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 struts configuration, the sub-elements of the <structs-config> element must be configured in the order specified by the DTD above. If the positions in the configuration files of these elements are reversed, an error is generated when the structs application is started.
<Data-sources> element

The <data-sources> element contains zero, one, or more <data-sources> child elements. <data-sources> is used to configure a specific data source, it can contain multiple <set-property> sub-elements to set various attributes of the data source.
...
After configuring the data source, you can access the data source in the action class. apache. structs. action. the getdatasource (httprequrst) method is defined in the action class, which is used to obtain references to data source objects.
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 <form-beans> Elements
The <form-beans> element is used to configure multiple actionform beans. <form-beans> contains multiple <forom-bean> sub-elements ,, each <from-bean> contains multiple attributes, including classname, name, and type.
<Form-beans>
<Form-bean name = "helloform" type = "Hello. helloform"/>
</Form-beans>
Note: To configure dynamic actionform bean, you must also configure the <form-property> element of the <form-bean> element.
<Form-beans>
<Form-bean name = "helloform" type = "Hello. helloform"/>
<Form-bean name = "userform" type = "org. Apache. structs. 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 <global-exception> element
<Global-exception> is used to handle configuration exceptions. The <global-exception> element can contain zero or multiple <exception> elements.
<Exception> the element is used to set mappings between Java exceptions and exception handling classes org. Apache. structs. Action and exception hander.
4 <gloable-forwards> Elements
The <global-forwards> element is used to declare a Global Forwarding relationship. <global-forwards> consists of zero or more <forward> elements. <Forward> elements are used to map a logic to a specific URL.
Attribute description
The configuration class corresponding to the classname and <forward> elements. The default value is org. Apache. structs. Action. actionforward.
Name: the logical name of the forwarding path, which must be
The URI specified by path or redirected. Required. It must start "/".
If redirect is set to true, the operation is redirected. 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:
<HTML: link forward = "forward1">
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 <action-mapping> Elements
The <action-mapping> element contains zero or more <action> elements. <Action> the element describes the ing from a specific request path to the corresponding action class.
6 <controller> Elements
The <controller> element is used to configure actionservlet.
7 <message-resources> Elements
The <message-resources> element is used to configure resource bundle. Resource budle is used to store localized message files.
Structs tag
HTML Tag
Tag Name Description
Base package HTML base elements
The button wraps the input elements of the HTML button type.
Cancel wrap HTML cancel button
Checkbox packaging HTML checkbox input fields
Errors displays some error messages conditionally and actionerrors Information
File packaging HTML file upload input field
Form defines HTML form elements
Frame-wrapped HTML Frame Elements
Hidden packaging HTML hidden input field
HTML packaging HTML elements
Input fields of the image packaging "image" Type
IMG wraps IMG elements in HTML
Javascript packaging the JavaScript Validation script provided according to the validation rules provided by validatorplugin
Link packaging hyperlink
Messages displays some prompts with conditions and actionmessages information.
Multibox packaging multiple selection input box
Option package a selection input box
Options package a batch of selection input boxes
Optionscollection package a batch of selection input box sets
Password-encapsulated ciphertext input box
Radio package radio input box
Reset package "reset" button
Rewrite package a URL
Select package a selection input box
Submit Package A submit button
Wrap a text input box in text
Textarea wraps 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 implemented.
Sample.
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;
If you want to initialize attributes before assembly, you must implement reset, which is called before the actionform assembly;
Dynamic actionform
Actionform as a type converter
A mandatory point of actionform should use the string and Boolean attributes. In fact, this means that the attribute
It must be converted from one type to another. Most applications also need some attributes, such as phone numbers or quantities
A format. The core Java package provides some tools to do this, but you must set
It is still a challenge to become an application.
Struts developers often include the Helper method in actionform for type conversion. Helper Method
There are many implementation methods, which we will describe in the 5.6.
5.2.6 use actionform as a firewall
When a request is submitted, actionservlet uses an automatic assembly mechanism to set the requested
Actionform attributes. This allows you to control which actionform attribute is exposed to control which request parameter
Is acceptable. This also means that if your actionform design is careless, you may lose control of which parameter can be connected
Capability. Actionform must not contain a property that looks like a JavaBean, but it cannot be requested from HTTP
Evaluate the set property method.
5.3.4 Other beans can be nested in actionform
Because struts tag extension and Automatic Assembly support the dot syntax to access other
Bean. 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:
<HTML: Text
Propety = "values. telephonetext"
Size = "14"
Maxlength = "14"/>
5.7.2 distribution (dispatch)
Struts developers usually use the same action to process related operations. A common practice is to select an operation in
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 ");
 

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.