web| Create
Create a configuration file for a Web application
For struts applications, its configuration file web.xml should be configured for the Actionservlet class, and the Struts tag library used by Web applications should be declared, in this case, three tag libraries are declared: Struts Bean, struts HTML and Struts logic tag library. Routine 1 is the source code for the Web.xml.
Routine 1 Web.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE web-apppublic "-//sun Microsystems, INC.//DTD Web Application 2.2//en" "Http://java.sun.com/j2ee/dtds/web-app_ 2_2.dtd "><web-app><display-name>helloapp Struts application</display-name><!--Standard Action Servlet Configuration--><servlet><servlet-name>action</servlet-name>< Servlet-class>org.apache.struts.action.actionservlet</servlet-class><init-param><param-name >config</param-name><param-value>/web-inf/struts-config.xml</param-value></init-param ><load-on-startup>2</load-on-startup></servlet><!--Standard Action servlet Mapping--> <servlet-mapping><servlet-name>action</servlet-name><url-pattern>*.do</url-pattern ></servlet-mapping><!--the usual Welcome File List--><welcome-file-list><welcome-file> hello.jsp</welcome-file></welcome-file-list><!--Struts Tag Library Descriptors--><taglib><taglib-uri>/web-inf/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location></taglib><taglib>< taglib-uri>/web-inf/struts-html.tld</taglib-uri><taglib-location>/web-inf/struts-html.tld</ Taglib-location></taglib><taglib><taglib-uri>/web-inf/struts-logic.tld</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location></taglib></web-app>
|
Create a configuration file for the Struts framework
As mentioned earlier, the struts framework allows applications to be divided into multiple components to improve development speed. The struts framework's configuration file Struts-config.xml can assemble these components and decide how to use them. Routine 2 is the source code for the Struts-config.xml file that HelloApp applies to.
Routine 2 Struts-config.xml
<?xml version= "1.0" encoding= "iso-8859-1"? ><! DOCTYPE struts-config Public "-//apache Software foundation//dtd struts Configuration 1.1//en" "http:// Jakarta.apache.org/struts/dtds/struts-config_1_1.dtd "><!--this are the struts configuration file for the" hello! " Sample application--><struts-config><!--======== Form Bean definitions ====================--> <fo rm-beans> <form-bean name= "helloform" type= "Hello." Helloform "/> </form-beans><!--========== Action Mapping definitions ===================--> <action -mappings> <!--Say hello! --> <action Path = "/helloworld" type = "Hello." Helloaction "name =" Helloform "scope =" request "Validate =" true " input = "/hello.jsp" > <forward name= "SayHello" path= "/hello.jsp"/> </action> ; </action-mappings> <!--========== message Resources Definitions ================--> <message-resources parameter= "Hello.application"/></ Struts-config>
|
The above code configures the Helloform, helloaction, and message resource files for the HelloApp application, first with an element configured with a actionform Bean named Helloform, whose corresponding class is Hello. Helloform:
An action component is then configured with the element:
<action Path = "/helloworld" type = "Hello." Helloaction " name =" Helloform " scope =" Request " Validate =" true " input = "/hello.jsp" ><forward name= "SayHello" path= "/hello.jsp"/></action>
|
Element's Path property specifies the path where the action is requested, the Type property specifies the full class name of the action, and the Name property specifies the Actionform Bean,scope property that needs to be passed to the action to specify the Actionform Bean's storage scope. The Validate property specifies whether form validation is performed, and the input property specifies the forwarding path when form validation fails. Element also contains a child element that defines a request forwarding path.
The elements in this example are configured with the Helloaction component, and the corresponding class is hello. Helloaction, the request access path is "HelloWorld", and when the action class is invoked, the struts framework should pass the Helloform bean that already contains the form data to it. The Helloform bean is stored in the request scope and should be validated before calling the action class. If form validation fails, the request is forwarded to the page hello.jsp that the user entered, allowing the user to correct the error.
The Struts-config.xml file finally defines a resource Bundle: The parameter property of the element specifies the message resource file used by resource Bundle. In this case, the parameter property is "Hello.application", indicating that the message resource file is named "Application.properties", and its location is web-inf/classes/hello/ Application.properties.