首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

建立 Web 應用與 Struts 框架配置檔案完整範例

     編輯:Java 程式設計入門

建立 Web 應用配置檔 (web.xml)

Struts 應用的 web.xml 應配置 ActionServlet 類別,並宣告所用 Struts 標籤庫。本例宣告三個標籤庫:Struts Bean、Struts HTML、Struts Logic。範例 1 為 web.xml 原始碼。

範例 1 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
  "https://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>

建立 Struts 框架配置檔 (struts-config.xml)

struts-config.xml 將應用組件組裝起來,決定如何使用。範例 2 為 helloapp 應用的配置檔原始碼。

範例 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"
  "https://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<!-- 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.jsp"/>
    </action>
  </action-mappings>

  <!-- Message Resources Definitions -->
  <message-resources parameter="hello.application"/>
</struts-config>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved