Create a configuration file for the Web application and the Struts framework

Source: Internet
Author: User
Tags config contains resource version tld java web access
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 2-7 is the source code for the Web.xml.

      
       
Routine 2-7 web.xml<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Web-app Public "-//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-cl Ass>org.apache.struts.action.actionservlet</servlet-class> <init-param> <param-name>config& lt;/param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <l Oad-on-startup>2</load-on-startup> </servlet> <!--Standard Action servlet Mapping--> <servle t-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </ Servlet-mapping> <!--the usual Welcome File List--> <welcome-file-list> &Lt;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-8 is the source code for the Struts-config.xml file that HelloApp applies to.

       
        
Routine 2-8 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://j  Akarta.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 ================================ = = =--> <form-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.js"P "/> </action> </action-mappings> <!--========== message definitions ================== =========--> <message-resources parameter= "Hello.application"/></struts-config>
       


The above code configures the Helloform, helloaction, and message resource files for HelloApp applications, first by <form-bean>The element is configured with a actionform Bean named Helloform, and its corresponding class is hello. Helloform:
<form-bean name= "Helloform" type= "Hello." Helloform "/>

Then through element is configured with an action component:

     
           
            
      <action    Path      = "/helloworld"          type      = "Hello." Helloaction "          name     =" Helloform "          scope     =" Request "          Validate   =" true "             input     = "/hello.jsp" >        <forward name= "SayHello" path= "/hello.jsp"/></action>
     
           


The Path property of the <action> element specifies the paths of the requested access action, the Type property specifies the full class name of the action, and the Name property specifies the Actionform Bean that needs to be passed to the action. The scope property specifies the scope of the Actionform bean, the Validate property specifies whether to perform form validation, and the input property specifies the forwarding path when form validation fails. The <action> element also contains a <forward> child element that defines a request forwarding path.

In this case, The <action> element is 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 with the element: <message-resources parameter= "hello.application"/>

     < The parameter property of the message-resources> element specifies the message resource file that resource bundle uses. 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.



This article is selected from the Freescale book "Mastering struts: Java Web design and development based on MVC"

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.