在MVC/Model 2中,控制器是必要的核心,這在Struts中是由ActionServlet擔任,一個應用程式中使用一個ActionServlet物件,我們必須在web.xml中配置它:
<?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>Struts Blank Application</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>config</param-name>
      <param-value>/WEB-INF/conf/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>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>


附帶一提的是,如果您的Servlet容器支援Servlet 2.4,您可以改以下面的scheme標籤來取代dtd:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">


在ActionServlet的設定上,我們給了個config初始參數,用以指定struts-config.xml的位置,在<servlet-mapping>的設定上,我們將所有以*.do結尾的請求全部交給ActionServlet來處理。

為了要使用Struts自訂標籤,我們也在web.xml中加入以下的設定:
  <!-- Struts Tag Library Descriptors -->
  <taglib>
    <taglib-uri>/tags/struts-bean</taglib-uri>
    <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
  </taglib>
                                                                                
  <taglib>
    <taglib-uri>/tags/struts-html</taglib-uri>
    <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
  </taglib>
                                                                                
  <taglib>
    <taglib-uri>/tags/struts-logic</taglib-uri>
    <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
  </taglib>


接下來該組態struts-config.xml了,依照web.xml的內容,我們將之設定在WEB-INF/conf目錄中:
<?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">
                                                                                
<struts-config>
    <global-forwards>
        <forward
            name="welcome"
            path="/Welcome.do"/>
    </global-forwards>
                                                                                
    <action-mappings>
        <action
            path="/Welcome"
            type="org.apache.struts.actions.ForwardAction"
            parameter="/pages/Welcome.jsp"/>
    </action-mappings>
                                                                                
    <message-resources parameter="resources.application"/>
</struts-config>


<global-forward>提供一個全局可查找的forward對象,在Struts的組態中,我們可以為每一個<action>標籤指定其forward對象,如果在<action>中查找不到,就會至<global-forward>中查找,對於一些共同使用的forward對象,您可以集中於<global-forward>中管理。

在<action-mappings>中用於設定請求的URI與Action物件的對應,也就是什麼樣的路徑請求該使用哪一個Action物件來處理,每一個<action>標籤的設定對應於一個ActionMapping物件,path路徑設定為Welcome表示Welcome.do的請求將由type所設定的物件來處理,ForwardAction單純的將請求forward至另一個對象,forward的對象則是parameter所設定的對象,透過ForwardAction的轉發,我們可以避免直接請求一個資源的真正位址,而使得所有的請求都可以先經過控制器ActionServlet來進行調度。

<message-resources>用於設定我們的訊息資源檔,parameter中的設定表示我們將使用WEB-INF/classes/resources目錄中application為開頭的資源檔案,預設將讀取application.properties,根據區域設定的不同,可以讀取不同的區域化訊息資源檔案。

接下來我們準備index.jsp,如下:
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<logic:redirect forward="welcome"/>


<logic:redirect>標籤會查找struts-config.xml中的<global-forward>標籤中是否設定有"welcome"的forward對象,並使用redirect的方式重新導向至指定的路徑,我們之前已經在web.xml中設定了<welcome-file>為index.jsp,所以當有人請求根目錄時,將會以index.jsp的?>中的設定,將請求forward至pages/Welcome.jsp,由於使用forward的方式,所以動作是在容器內進行的,瀏覽器並不知道這件事,因而瀏覽器的網址列並不會改變,仍然顯示Welcome.do。

Welcome.jsp的內容如下:
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@page contentType="text/html; charset=Big5"%>
<html:html locale="true">
<head>
<title><bean:message key="welcome.title"/></title>
<html:base/>
</head>
<body bgcolor="white">
                                                                                
<H1><bean:message key="welcome.message"/></H1>
                                                                                
</body>
</html:html>


這邊使用了<html:html>標籤與<bean:message>標籤,標籤的使用方式是顯而易見、不難理解的,其中<bean:message>的key比對來源就是我們的訊息資源檔,也就是application.properties,如果您有設定中文訊息檔的話就是application_zh.properties,您可以使用http://localhost:8080/HelloStruts/來瀏覽,最後網址列會變成http://localhost:8080/HelloStruts/Welcome.do,而Welcome.jsp所產生的HTML原始碼如下:
<html lang="zh">
<head>
<title>哈囉!Struts!</title>
<base href="http://localhost:8080/HelloStruts/pages/Welcome.jsp">
</head>
<body bgcolor="white">

<H1>這是您的第一個Struts!</H1>

</body>
</html>
posted on 2005-04-22 11:19  Johnny  阅读(509)  评论(0)    收藏  举报