Getting started with basic configuration in Structs

Source: Internet
Author: User
Tags html file upload tld

 

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 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 Specify the location of the configuration file for the sub-Application in relative paths. Generally, the sub-application is rarely used and is not described here.
Debug Set 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>



Note:

Can contain multiple Child element, first find the first File.

Because Servlet ing cannot be configured in the element, so the Structs Action cannot be directly used as the welcome file. You can use a work ing method 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 and 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 Child element used 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

To use the custom tag library, configure them:

 

      <taglib>       <tag-uri>/WEB-INF/structs-html.tld    <tag-uri>       <taglib-location>    /WEB-INF/structs-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. 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, The 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 configuration must follow the sequence specified by the above DTD. If the position in the configuration file of these elements is reversed, an error is generated when the Structs application is started.

Element

The element contains zero, one, or more Child element, Used to configure a specific data source. It can contain multiple Child element, 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. structs. 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) Element

Element is used to configure multiple ActionForm beans, Contains multiple Child element, each Contains multiple attributes, ClassName, name, type:

 

      <form-beans>        <form-bean name="HelloForm"   type="hello.HelloForm"/></form-beans>



Note: Dynamic ActionForm Bean must also be configured. Element 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) Element

Used to configure Exception Handling, The element can contain zero or multiple Element.

Element is used to set the ing between java exceptions and Exception handling classes org. apache. structs. action and Exception Hander.

(4) Element

The element is used to declare the Global Forwarding relationship, Zero or multiple Element. Element is used to map a logic to a specific URL

 

Attribute Description
ClassName And Configuration class corresponding to the element. The default value is org. apache. structs. 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)Element

Element contains zero or multipleElement.The element describes the ing 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 Resource Bundle, and 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. 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 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 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. Helper Method

There are many implementation methods.

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 the ability to control which parameter is acceptable. ActionForm must not contain the attribute method that looks like a JavaBean attribute, but cannot be set from the HTTP 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:

 

       



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 valid for the Action objects called in this ActionMapping. From the perspective of the internal Action object, forward is usually selected as follows:

 

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

Related Article

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.