web|建立
建立Web應用的設定檔
對於Struts應用,它的設定檔web.xml應該對ActionServlet類進行配置,此外,還應該聲明Web應用所使用的Struts標籤庫,本例中聲明使用了三個標籤庫: Struts Bean、Struts HTML和Struts Logic標籤庫。常式1為web.xml的原始碼。
常式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> |
建立Struts架構的設定檔
正如前面提及的,Struts架構允許把應用劃分成多個組件,提高開發速度。而Struts架構的設定檔struts-config.xml可以把這些組件組裝起來,決定如何使用它們。常式2是helloapp應用的struts-config.xml檔案的原始碼。
常式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 is 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.jsp" /> </action> </action-mappings> <!-- ========== Message Resources Definitions ================ --> <message-resources parameter="hello.application"/></struts-config> |
以上代碼對helloapp應用的HelloForm、HelloAction和訊息資源檔進行了配置,首先通過元素配置了一個ActionForm Bean,名叫HelloForm,它對應的類為hello.HelloForm:
接著通過元素配置了一個Action組件:
<action path = "/HelloWorld" type = "hello.HelloAction" name = "HelloForm" scope = "request" validate = "true" input = "/hello.jsp"><forward name="SayHello" path="/hello.jsp" /></action> |
元素的path屬性指定請求訪問Action的路徑,type屬性指定Action的完整類名,name屬性指定需要傳遞給Action的ActionForm Bean,scope屬性指定ActionForm Bean的存放範圍,validate屬性指定是否執行表單驗證,input屬性指定當表單驗證失敗時的轉寄路徑。元素還包含一個子項目,它定義了一個請求轉寄路徑。
本例中的 元素配置了HelloAction組件,對應的類為hello.HelloAction,請求訪問路徑為"HelloWorld",當Action類被調用時,Struts架構應該把已經包含表單資料的HelloForm Bean傳給它。HelloForm Bean存放在request範圍內,並且在調用Action類之前,應該進行表單驗證。如果表單驗證失敗,請求將被轉寄到接收使用者輸入的網頁hello.jsp,讓使用者糾正錯誤。
struts-config.xml檔案最後通過元素定義了一個Resource Bundle:元素的parameter屬性指定Resource Bundle使用的訊息資源檔。本例中parameter屬性為"hello.application",表明訊息資源檔名為"application.properties",它的存放路徑為WEB-INF/classes/hello/application.properties。