[struts]Write web.xml

来源:互联网 发布:理财收益计算器 软件 编辑:程序博客网 时间:2024/04/19 14:28

  Because a Struts application is a Web application, it has to follow the same rules. that any Web application has to follow: Every Web application must have a web.xml configuration file. The web.xml file should define the ActionServlet,  which functions as a controller from the standpoint of the MVC(Model - View - Controller)framework. In other words, every request to a Struts application has to go through the ActionServlet. the ActionServlet is provided by the Struts frameword.

  The example web.xml file as shown below, has three different categories of configuration information:

  • ActionServlet configuraton
  • ActionServlet mapping
  • Struts tag library descriptors

  Under ActionServlet configuration section, note that several initialization parameters are specified as following through <init-param> elements:

  • <param-name>application</param-name>
    defines message resource file.  it is set as following:
        <init-param>
                <param-name>application</param-name>
                <param-value>ApplicationResources</param-value>
        </init-param>
  • <param-name>config</param-name>
    defines the path of the Struts configuration file. For example, the path of the Struts configuration file set to ./WEB-INF/struts-config.xml.
      <init-param>
                <param-name>config</param-name>
                <param-value>/WEB-INF/struts-config.xml<param-value>
      </init-param>

  Under ActionServlet mapping configuration section shown below, it is set in the way that every request that has *.do URL pattern will be handled by the ActionServlet.

  • <servlet-mapping>
               <servlet-name>action</servlet-name>
               <url-pattern>*.do</url-pattern>
    </servlet-mapping>

  Under Struts tag Library Descriptors configurations section, note three Struts tag libraries are configured.

 A complete web.xml example:

 

<?xml version="1.0" encoding="ISO-8859-1"?>

<!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>Advanced J2EE Programming Class Sample App</display-name>
  
  
<!-- Standard Action Servlet Configuration (with debugging) -->
  
<servlet>
    
<servlet-name>action</servlet-name>
    
<servlet-class>
      org.apache.struts.action.ActionServlet
    
</servlet-class>
    
<init-param>
      
<param-name>application</param-name>
      
<param-value>ApplicationResources</param-value>
    
</init-param>
    
<init-param>
      
<param-name>config</param-name>
      
<param-value>/WEB-INF/struts-config.xml</param-value>
    
</init-param>
    
<init-param>
      
<param-name>debug</param-name>
      
<param-value>2</param-value>
    
</init-param>
    
<init-param>
      
<param-name>detail</param-name>
      
<param-value>2</param-value>
    
</init-param>
    
<init-param>
      
<param-name>validate</param-name>
      
<param-value>true</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>

 
  
<!-- 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>
原创粉丝点击