Create a web application and configuration files for the Struts framework

xiaoxiao2021-03-04  49

For Struts applications, its configuration file Web.xml should configure the ActionServlet class. In addition, the Struts tag library used by the web application. This example uses three label libraries: struts bean, struts HTML and STRUTS Logic tag library. Routines 2-7 is the source code of web.xml.

Routines 2-7 Web.xml

PUBLIC "- // Sun microsystems, Inc.//dtd Web Application 2.2 // en"

"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

HelloApp Struts Application

Action

org.apache.struts.Action.ActionServlet

config

/web-inf/struts-config.xml

2

Action

*. do

Hello.jsp

/web-inf/struts-bean.tld

/web-inf/struts-bean.tld

/web-inf/struts-html.tld

/web-inf/struts-html.tld

/web-inf/struts-logic.tld

/Web-inf/struts-logic.tld

Create a configuration file for the Struts framework

As mentioned earlier, the Struts framework allows applications to be divided into multiple components and increase development speed. The configuration files for the Struts framework Struts-config.xml can assemble these components and determine how to use them. Routines 2-8 are the source code for the struts-config.xml file for the HelloApp application.

Routines 2-8 Struts-Config.xml

"- // Apache Software Foundation // DTD Struts Configuration 1.1 // en"

"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

This is the struts configuration file for the "Hello!" Sample Application

->

TYPE = "Hello.HelloAction"

Name = "Helloform"

Scope = "request"

Validate = "True"

INPUT = "/Hello.jsp"

>

The above code is configured to HelloApp's Helloform, HelloAction, and message resource files, first pass

element is configured with an actionform bean, named Helloform, which corresponds to Hello.Helloform:

Then pass through

The element is configured with an Action component:

TYPE = "Hello.HelloAction"

Name = "Helloform"

Scope = "request"

Validate = "True"

INPUT = "/Hello.jsp"

>

Element PATH attribute Specifies the path to the Action, the Type property specifies the full class name of the action, the Name property specifies the actionform bean that needs to be passed to the action, the scope property specifies the range of the ActionForm Bean, the Validate property specifies whether to perform form validation The Input property specifies the forwarding path when the form is verified. The element also includes a sub-element that defines a request forwarding path.

In this example

The element is configured with a HelloAction component. The corresponding class is Hello.HelloAction, requesting the access path to "HelloWorld", when the Action class is called, the Struts framework should pass the Helloform Bean that already contains form data to it. Helloform Beans are stored in the Request range and table verification should be performed before calling the Action class. If the form verification fails, the request will be forwarded to the page hello.jsp receiving the user entered, allowing the user to correct errors.

The struts-config.xml file finally passed

The element defines a resource bundle:

转载请注明原文地址:https://www.9cbs.com/read-31640.html

New Post(0)