用 WebSphere Studio V5 編寫 Struts 應用程序

Struts 是 Apache 軟件基金會(Apache Software Foundation)資助的一個開放源代碼框架。您可以用它來維護和擴展 Web 應用程序。IBM® WebSphere® Studio Application Developer 版本 5.0(以下稱 WebSphere Studio)對 Struts 有內建支持,支持 Struts 1.02 和 1.1(beta 2)。WebSphere Studio 中的 Struts Configuration 編輯器使您可以很容易地修改 struts-config.xml 文件。這篇文章描述了怎樣用 WebSphere Studio 中的內建支持創建一個 Struts 示例。

Struts

Struts 框架完全是用 Java 寫的,寫時用了標準 J2EE API。另外,它還採用了幾種著名的 J2EE 設計模式,比如模型-視圖-控制器(Model-view-controller)和 FrontController。

模型-視圖-控制器(MVC)

模型-視圖-控制器(MVC)是一種設計模式,它在定義下面三個應用層時作了明確的區分。

  • 模型(model)是應用程序的數據和業務規則集合。通常被稱作應用程序的業務邏輯。
  • 視圖(view)是應用程序的用戶界面。
  • 控制器(controller)定義了應用程序與用戶輸入及模型進行交互的方式。它被稱作應用程序邏輯。

通過明確區分各個層,MVC 允許組成每個層的各個組件間鬆散地耦合。這使它更加靈活,並且可以重用代碼。例如,如果您爲一個應用程序開發了幾個用戶界面,那麼就需要開發視圖組件,因爲各應用層間是鬆散耦合的。

Struts 框架是 MVC 的視圖和控制器組件。下面展示了 Struts 怎樣映射爲 MVC 框架。Struts 有三個主要組成部分:

  • Action bean
  • ActionServlet
  • ActionForm bean 和定製的標記。

圖 1. MVC 和 Struts 框架
MVC 和 Struts 框架

Action bean 和 ActionServlet
Struts 提供了一個單獨的 ActionServletorg.apache.struts.action.ActionServlet)來處理所有的瀏覽器請求。這種類型的框架我們稱之爲 FrontController 模式。每個瀏覽器請求都由 Struts 的 Action 子類(org.apache.struts.action.Action 的子類)來處理。每個瀏覽器請求都被映射爲 struts-config.xml 文件內的一個 Action 子類。ActionServlet 在初始化期間加載這種映射。要配置 Web 項目使其把所有的瀏覽器請求都傳遞給 ActionServlet,請把所有以 .do 結尾的 URI(例如 *.do)都映射爲 Web 部署描述符中的 ActionServlet。然後您可以在 Struts 配置文件中爲各個請求 URI(比如 /submit.do)提供實際的 Action 子類映射。

ActionForm bean
瀏覽器請求可帶參數。當用戶提交 HTML 表單後,Struts 框架就把參數放在一個 org.apache.struts.action.ActionForm bean 中。您也可以用 ActionForm bean 把缺省值預先植入一個表單,這些缺省值可從數據庫或其他的後端系統獲得。如果用戶在表單中輸入了不正確的值,ActionForm 可以進行驗證。您可以用以前的輸入重新顯示錶單。

定製的標記
Struts 提供了許多支持 ActionForm bean 的 JSP 定製標記。這些定製的標記支持:

  • 把從 ActionForm 子類中獲得的值預先植入 HTML 表單。
  • 國際化,比如提供由用戶語言環境決定的文本。
  • 邏輯,比如根據人們對頁面的使用方式爲其顯示不同的標題。

Struts 是一個通用的框架,您可以很容易地把它和 WebSphere Studio 一起使用。下面我們來開始第一個 Struts 示例。

編寫一個簡單的 Struts 應用程序

先決條件:

啓動 WebSphere Studio 版本 5.0:

  1. 轉到 Window Start 菜單。
  2. 選擇 Programs => IBM WebSphere Studio => Application Developer 5.0。

第 1 步:開始一個新的 Struts Web 項目

創建一個 1.3 EAR 項目(不包含 EJB / Client 或 Web 項目):

  1. 選擇 New => Projects => Enterprise Application Project
  2. 選擇 Create 1.3 J2EE Enterprise Application Project。按 Next
  3. 取消對全部三個子項目(Application Client Project、EJB Project 和 Web Project)的選擇。
  4. 輸入 StrutsEAR 作爲項目名。
  5. 單擊 Finish

創建一個 Web 項目並添加 Struts 支持:

  1. 選擇 New => Projects => Web => Web Project
  2. 輸入 StrutsSampleWeb 作爲 Web 項目。
  3. 選擇 Add Struts support 複選框。單擊 Next
  4. 選擇 Existing Enterprise Application Project。
  5. 瀏覽查找新建的 EAR 項目。兩次單擊 Next
  6. 在 Struts Setting 頁面中選擇 Override default settings,並在下拉框中選擇 1.1 (beta 2) ,如圖 2 所示。前面已經提到過,表單被提交時,HTML 表單數據被自動植入 Struts ActionForm。Struts 1.0.1 只支持簡單的 Java 類型。而 Struts 1.1(beta 2)還支持 java.util.HashMap 或其他的 Collection 類型。這一點我們將在本文的後面部分討論。
  7. 單擊 Finish

圖 2 .覆蓋 Web 項目創建嚮導中的缺省設置
覆蓋 Web 項目創建嚮導中的缺省設置

檢查圖 3 中的 Struts 文件結構。

圖 3 .支持 Struts 的 web 項目
支持 Struts 的 WEB 項目

您將稍後修改下面的文件:

  • ApplicationResources.properties 是 Struts 應用程序的資源綁定。語言環境的詳細信息和錯誤消息都放在這個屬性文件中。
  • struts-config.xml 是 Struts 的 xml 配置文件。WebSphere Studio 爲這個文件提供了一個 GUI 編輯器。

現在,請執行下列操作:

  1. 檢查 web.xml 文件。
  2. 展開 StrutsSampleWeb 項目並雙擊 Web Deployment Descriptor 來打開編輯器。
  3. 轉到 Servlets 頁面。請注意下面兩點:
    • 名爲 action 的 Struts org.apache.struts.action.ActionServlet 的定義。
    • 到這個 servlet 的 URL 映射,*.do

請注意,在 Initialization 部分,validate 被設爲 true。ActionServlet 用 XML 解析器來驗證和處理配置文件。它與表單驗證無關,稍後在本文中您將看到這一點。

*.do 怎樣獲得正確的 Action 類?

前面已經提到過,ActionServletAction 類是 MVC 模型中控制器層的核心。該控制器負責處理用戶的請求,把請求路由到業務邏輯,並選擇視圖來響應用戶(請參閱 Struts 用戶指南,第 4.4 節)。表單提交給 submit.do 後,Struts ActionServlet 會根據 struts-config.xml 文件中的 <action-mapping> 選擇正確的 Action 類來用。

Struts Action 子類負責處理用戶數據。在這個示例中,創建一個名爲 SubmitAction 的 Struts Action 子類。它由諸如讀和處理表單數據之類的操作組成。每一個表單都和該 Struts ActionForm 子類的一個實例關聯在一起。請創建這個繼承 ActionForm 的表單類。SubmitForm 是 ActionForm 的一個子類,它是用域的 getter 和 setter 方法創建的。getter 和 setter 方法在 ActionForm 子類中都是必須有的。

SubmitForm 怎樣發揮作用?

每一個 Struts Action 類都必須和一個 Struts ActionForm 類關聯在一起。您可以在 WebSphere Studio 中的 struts-config.xml 編輯器的 FormBean 頁面中定義 SubmitForm 類。然後可以把它與 struts-config.xml 文件中的 SubmitAction 映射關聯在一起。一個請求提交後,ActionServlet 把從 Web 瀏覽器上的實際表單中得到的數據自動植入 SubmitForm。在 SubmitAction 類中,用 SubmitForm f = (SubmitForm) form 來訪問表單數據。

第 2 步:用 Struts taglib 構建一個 JSP 表單

Struts 爲輸入域提供了許多 HTML 標記併爲 JSP 表單提供了許多超鏈接。下面列出了常用的幾個:

  • 複選框 - <html:checkbox property="name"/>
  • 隱藏域 - <html:hidden property="name"/>
  • 密碼輸入域 - <html:password property="name"/>
  • 單選按鈕 - <html:radio property="name"/>
  • 重設按鈕 - <html:reset/>
  • 選項(下拉框) <html:select property="name"/>
    <html:option value="a"/>choice1</html:option>
    </html:select>
  • 提交按鈕 - <html:submit/>
  • 文本輸入域 - <html:text property="name"/>
  • 文本區輸入域 - <html:textarea property="name"/>

大多數 HTML 標記都支持 Javascript 事件,如 onmouseclick、onmouseover 等事件。關於更多信息,請參閱 HTML Bean API

下面我們來爲這個示例創建一張 JSP 頁面。在 Web Perspective 中,用 Struts 模型創建一張 JSP 頁面:

  1. 展開 StrutsSampleWeb 項目直到看到 /Web Content 文件夾。右鍵單擊 /Web Content
  2. 選擇 New => JSP File
  3. Name 中輸入 submitpage.jsp
  4. 從下拉框中選擇 Struts JSP 作爲 Model
  5. 單擊 Next,注意只添加了 HTML 和 Bean 的 taglib。如果您想使用其他標記庫中的 taglib,如 Logic taglib,請選擇 Add Tag Libraries,然後選擇 /WEB-INF/struts-logic.tld
  6. 單擊 Finish

圖 4.JSP 創建嚮導 - Struts 標記庫
JSP 創建嚮導 - Struts 標記庫

用源代碼編輯器中的下面這些代碼修改 submitpage.jsp 頁並保存:

	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
	<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
	<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
	
	<html:html locale="true">
	
	<HEAD>
	<%@ page
	language="java"
	contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"
	%>
	<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<META name="GENERATOR" content="IBM WebSphere Studio">
	<META http-equiv="Content-Style-Type" content="text/css">
	<LINK href="theme/Master.css" rel="stylesheet"
		type="text/css">
		
		
	<TITLE>Pizza Order Page</TITLE>
	</HEAD>
	
	<BODY>
		<P><h1>Pizza Order Page </h1></P>
		
			<html:form action="/submit.do">
		
			Name: <html:text property="customer.name"/><br>
			Address: <html:text property="customer.address"/><br>
			
			Size: <html:radio property ="size" value="S"/>Small
			<html:radio property ="size" value="M"/>Medium
			<html:radio property ="size" value="L"/>Large
			
			Toppings: <br>
			Pepperoni<html:checkbox property="topping(Pepperoni)"/><br>
			Onion<html:checkbox property="topping(Onion)"/><br>
			Mushroom<html:checkbox property="topping(Mushroom)"/><br>
			Hot Pepper<html:checkbox property="topping(Hot Pepper)"/><br>
			Bacon<html:checkbox property="topping(Bacon)"/><br>
			
			<html:select property ="type">
			<html:option value="a">Delivery</html:option>
			<html:option value="b">Pickup</html:option>
			</html:select>
			
				<html:submit/>
				<html:reset/>
			</html:form>
	</BODY>
	</html:html>

忽略任務列表中關於 submit.do 不存在的警告消息。爲 Struts Action 類創建一張 /confirm.jsp 頁面用來進行轉發。

在 Web Perspective 中,創建 confirm.jsp 頁面:

  1. 右鍵單擊 /Web Content
  2. 選擇 New => JSP File
  3. Name 域中輸入 confirm.jsp
  4. 單擊 Finish

用下面的代碼修改 JSP 文件:

	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
	<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
	<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
	<html:html>
	<HEAD>
	<%@ page 
	language="java"
	contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"
	%>
	<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<META name="GENERATOR" content="IBM WebSphere Studio">
	<META http-equiv="Content-Style-Type" content="text/css">
	<LINK href="theme/Master.css" rel="stylesheet"
		type="text/css">
	<TITLE></TITLE>
	</HEAD>
	
	<BODY>
	<P>Thank you <%=request.getAttribute("name")%></P>
	</BODY>
	</html:html>

在 submitpage.jsp 中,customer.name 屬性引用對象內的一個域。topping(Pepperoni) 屬性是 java.util.HashMap 的一個鍵/值對。Struts 1.1(beta 2)HTML taglib 支持嵌套的屬性。

第 3 步:創建一個 Struts ActionForm SubmitForm 類

您必須定義一個繼承 Struts ActionForm 類的子類。它必須有用於所有表單域的 setter 和 getter。前面已經提到過,提交表單後,ActionForm 類被預先植入表單數據。ActionForm 類有驗證方法和重設方法(可選)分別用來驗證表單輸入和重新設置表單。稍後將在本文中討論它們。

如果您的應用程序有一個跨多個 Web 頁面的表單,那麼就可以只用一個 ActionForm。定義一個包含所有域的屬性的 ActionForm bean,不管域實際顯示在哪一張頁面上。同樣,可以把同一個表單的各個頁面提交給同一個 Action 類。使用這種設計,Web 站點設計者不必改動業務邏輯就可以重新安排域。

創建一個 Java 類,它繼承 Web 項目下 /Java Source 文件夾中的 org.apache.struts.action.ActionForm

  1. 在 Web Perspective 中,右鍵單擊 StrutsSampleWeb 項目下的 /Java Source 文件夾。
  2. 選擇 New => Other => Web。展開 Web => Struts。選擇 ActionForm Class 並單擊 Next

    圖 5.啓動 Struts ActionForm 類嚮導
    啓動 Struts ActionForm 類嚮導

  3. 輸入 com.ibm.sample.struts 作爲 Java Package 的名稱。
  4. 輸入 SubmitForm 作爲類的 Name。單擊 Next
  5. 這張頁面讓您選擇想爲 setter 和 getter 選擇的域。像圖 6 那樣展開目錄樹。選擇這些項:customer.name、size、topping(Pepperoni) 和 type

    圖 6. 爲 HTML 表單選擇新的訪問程序(setter 和 getter)
    選擇新的訪問程序

  6. 單擊 Next
  7. customer.name 改爲 customer(不用引號)並把它的類型改爲 Customer
  8. topping(Pepperoni) 改爲 toppings(注意複數形式),並把其類型改爲 java.util.HashMap
  9. String[] 改爲 String 類型。
  10. 添加 name,類型爲 String
  11. 添加 address,類型爲 String

    圖 7. 修改訪問程序
    修改訪問程序

  12. 單擊 NextActionServlet 用 struts-config.xml 文件來決定把控制權轉交給哪個 Action 類及與之相關聯的 ActionForm 類。這張頁面自動在 Struts 配置 xml 文件中定義 submitForm ActionForm bean。

    圖 8. 在 struts-config.xml 中爲 ActionForm 配置映射。
    配置映射

  13. 單擊 Finish

ActionForm 包含表單中所有的域,並且每個域必須有一個 getter 和一個 setter。您需要手工更改 Customer 對象以及 toppings HashMap 的 getter 和 setter 的實現。請照下面這樣修改您剛創建的 SubmitForm 中的黑體代碼:

	package com.ibm.sample.struts;
	
	import java.util.HashMap;
	
	import javax.servlet.http.HttpServletRequest;
	
	import org.apache.struts.action.ActionError;
	import org.apache.struts.action.ActionErrors;
	import org.apache.struts.action.ActionForm;
	import org.apache.struts.action.ActionMapping;
	
	public class SubmitForm extends ActionForm {
		private Customer customer = new Customer();
		private String size = null;
		private java.util.HashMap toppings = new java.util.HashMap();
		private String type = null;
		private String name = null;
		private String address = null;
	
		public String getSize() {
			return size;
		}
		public HashMap getToppings() {
			return toppings;
		}
		public void setSize(String size) {
			this.size = size;
		}
		public void setToppings(HashMap toppings) {
			this.toppings = toppings;
		}
		public String getType() {
			return type;
		}
		public void setType(String type) {
			this.type = type;
		}
		public Customer getCustomer() {
			return customer;
		}
		public void setCustomer(Customer customer) {
			this.customer = customer;
		}
		public String getAddress() {
			return customer.getAddress();
		}
		public String getName() {
			return customer.getName();
		}
		public void setAddress(String address) {
			customer.setAddress(address);
		}
		public void setName(String name) {
			customer.setName(name);
		}
		public void setTopping(String key, Object value) {  //*new* method
			toppings.put(key, value);
		}
		public Object getTopping(String key) {  //*new* method
			return toppings.get(key);
		}
		public void reset(ActionMapping mapping, HttpServletRequest request) {
			this.customer = new Customer();
			name = new String();
			address = new String();
			size = new String();
			toppings.clear();
			type = new String();
		}
	}

因爲 SubmitForm 要用 Customer bean,請在 com.ibm.sample.struts 包下創建一個名爲 Customer 的 java 類:

  1. 在 Web Perspective 中,右鍵單擊 Web 項目下的 /Java Source 文件夾。
  2. 選擇 New => Class
  3. 輸入或瀏覽選擇 com.ibm.sample.struts 作爲 Package 的名稱。
  4. 輸入 Customer 作爲類的 Name
  5. 單擊 Finish
  6. 爲該類輸入下面的代碼:
	package com.ibm.sample.struts;

	public class Customer {
		
		private String name;
		private String address;
		
		public String getAddress() {
			return address;
		}
		public String getName() {
			return name;
		}
		public void setAddress(String address) {
			this.address = address;
		}
		public void setName(String name) {
			this.name = name;
		}
	}

要把表單中的 input 文本域映射爲 customer 對象的 name 域,請使用 HTML 標記中的 <html:text property="customer.name"/>。要訪問 HashMap 的鍵/值對或任何 Collections,請使用 HTML 標記中的 property="toppings(key)",其中 toppings 是示例中 HashMap 的名稱。注意:只有 Struts 1.1(beta 2)支持使用 HashMap 或其他 Collections 屬性。

按下 reset 按鈕後,就調用了 reset() 方法。

第 4 步:創建一個 Action SubmitAction 類

Struts Action 類是應用程序邏輯。它進行 JDBC 調用、調用其他的業務 bean 並調用 EJB 等等。我們建議把業務邏輯與其他的 bean 分開,而不要把它嵌入這個 Action 類。這個類調用有業務邏輯的 bean。您應該實現 execute() 方法。它返回一個標識下一張頁面(比如一張 JSP 頁面)的 ActionForward 對象。

public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response)

execute() 方法能夠訪問您可以使用的表單數據。因爲所有的請求都被路由到 Action 類的一個實例,所以請確保您的代碼可以在多線程環境中正確執行。當返回一個標識 JSP 頁面的 ActionForward 時,在 struts-config.xml 編輯器中爲該 JSP 頁面定義一個邏輯名。例如,爲 /confirm.jsp 定義邏輯名 success。在 execute() 方法中,return (mapping.findForward("success")); 這行代碼把控制權轉交給 /confirm.jsp 頁面。

在 /Java Source 文件夾下創建一個名爲 SubmitActionAction 類:

  1. 在 Web Perspective 中,右鍵單擊 Web 項目下的 /Java Source 文件夾。
  2. 選擇 New => Other => Web => Struts => Action Class
  3. 輸入 com.ibm.sample.struts 作爲 Package 的名稱。
  4. 輸入 SubmitAction 作爲類的 Name。按下 Next
  5. 在 Forwards 部分,按下 Add 來添加轉交(forwards),這次轉交的 Namesuccess,Path 爲 /confirm.jsp
  6. 選擇 submitForm 作爲 Form Bean Name 域的值。
  7. 選擇 session 作爲 Form Bean Scope 域的值。
  8. 單擊 Finish

圖 9. 創建 Action 類嚮導。
創建 Action 類嚮導

在 SubmitAction 類中輸入下列代碼:

	package com.ibm.sample.struts;

	import javax.servlet.http.HttpServletRequest;
	import javax.servlet.http.HttpServletResponse;
	import org.apache.struts.action.Action;
	import org.apache.struts.action.ActionError;
	import org.apache.struts.action.ActionErrors;
	import org.apache.struts.action.ActionForm;
	import org.apache.struts.action.ActionForward;
	import org.apache.struts.action.ActionMapping;
	
	/**
	 * @version 	1.0
	 * @author
	 */
	public class SubmitAction extends Action {
	
		/**
		* Constructor
		*/
		public SubmitAction() {
	
			super();
	
		}
		public ActionForward execute(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response)
			throws Exception {
	
			ActionErrors errors = new ActionErrors();
			ActionForward forward = new ActionForward();
			// return value
			SubmitForm submitForm = (SubmitForm) form;
	
			try {
	
				// Getting the value from the form
				<B>String name = submitForm.getCustomer().getName();
				System.out.println ("The name is: " + name);
				request.setAttribute("name",name);</B>
	
			} catch (Exception e) {
	
				// Report the error using the appropriate name and ID.
				errors.add("name", new ActionError("id"));
	
			}
	
			// If a message is required, save the specified key(s)
			// into the request for use by the <struts:errors> tag.
	
			if (!errors.empty()) {
				saveErrors(request, errors);
			}
			// Write logic determining how the user should be forwarded.
			forward = mapping.findForward("success");
	
			// Finish with
			return (forward);
	
		}
	}

第 5 步:運行樣本

至此,您已經完成了這個示例,現在可以運行它了。要在 WebSphere Test Environment V5 中運行 submitpage.jsp:

  1. 右鍵單擊 submitpage.jsp => Run on Server
  2. 確保選中了 WebSphere Test Environment v5 Server。單擊 OK

第 6 步:驗證

您可以在 Struts ActionForm 中進行驗證。一個表單被提交後,SubmitForm 中的 validate() 方法被實現。請把下面的 validate() 方法添加到 SubmitForm 中:

	public ActionErrors validate(
		ActionMapping mapping,
		HttpServletRequest request) {
			
		ActionErrors errors = new ActionErrors();

		if ((customer.getName()==null)|| (customer.getName().equals(""))) {
			// if the name is empty --> errors
				errors.add("Name", new ActionError ("error.customer.name"));
							System.out.println("errors....");
		}
		return errors;
	}
	

保存了 SubmitForm 類後,請注意 ActionError 編譯錯誤並沒有解決。右鍵單擊 ActionError => Source => Organize imports。

error.customer.name 屬性是從資源綁定中獲得的。在一個編輯器中從 /Java Source 文件夾打開 ApplicationResources.properties。把下面的代碼行添加到這個屬性文件中:

	# Optional header and footer for <errors/> tag.
	title=Pizza Store
	errors.header=<hr><h3>Errors</h3><ul>
	errors.footer=</ul><hr>
	error.customer.name=<li>Name is empty

當錯誤在 JSP 頁面中顯示出來時,是用 <hr><h3>Errors</h3><ul></ul> 括起來的。您必須用 errors.header 和 errors.footer 爲錯誤消息定義頭(header)和腳(footer)。錯誤被檢測出後,它們被返回表單,同時輸入值被保存。

您需要在 JSP 文件中添加代碼行 <html:errors/> ,這樣錯誤才能顯示出來。在 submitpage.jsp 文件的開頭處(就在<html:form> 之前),輸入 <html:errors/>

在 struts-config.xml 編輯器中修改 /input 域:

  1. 在編輯器中打開 struts-config.xml 文件。這個文件在 /Web Content 文件夾中。
  2. ActionActionForm bean 由 Struts 創建嚮導來配置,但 Actions 頁面中的 input 域除外。爲使驗證能正常進行,這個域是必需的。
  3. 在 struts-config.xml 編輯器的 Actions 頁面下,在 Input 域中選擇 /submit 並輸入 /submitpage.jsp。保存所做的修改。

您可以把錯誤消息和其他消息放在 ApplicationResources.properties 文件中。例如,您可以用 <bean:message key="title"/> 在 JSP 中顯示標題,內容可以從資源綁定中獲得。

資源綁定還支持國際化(i18n - 在 in 之間有 18 個字符)。您可以爲 ISO 語言代碼爲 xx 的其他語言創建 ApplicationResources_xx.properties。

再次運行樣本並進行驗證

既然我們已經修改了 Web 項目,那麼就來重新啓動一下服務器並再次嘗試運行該樣本。要重新啓動服務器,請轉到服務器視圖,右鍵單擊並選擇 Restart

如果提交後名稱爲空,將返回到表單頁面,屬性文件中列出錯誤消息。

圖 10. 運行樣本並進行驗證
運行樣本並進行驗證

Struts 應用程序的圖形表示

WebSphere Studio 允許您創建 web 圖表來用圖形方式表示 Struts 應用程序。

  1. 在 Web Perspective 中,右鍵單擊 Web 項目下的 /Java Source 文件夾。
  2. 選擇 File => New => Other => Web => Struts => Web Diagram
  3. 輸入 submitDiagram 作爲 File name
  4. 按下 Finish。這樣就在 /Java Source 文件夾下創建了一個名爲 submitDiagram.gpg 的文件。
  5. 轉到 Web Structure 視圖。這個視圖和 Outline 視圖位於同一堆棧上。
  6. 展開 <default module> => Actions => /submit
  7. 把帶 /submit 的圖標拖到圖表上。
  8. 右鍵單擊圖表上的圖標 => Draw => Draw All From
  9. 雙擊圖標進入編輯器。例如,雙擊 /submit 圖標可以進入 struts-config.xml 編輯器。

圖 11. Struts Web 圖表
Struts Web 圖表

結束語

Struts 是一個功能強大的框架,它非常靈活,並且它的三個應用層鬆散地耦合在一起。在應用程序中使用 Struts 可以簡化將來的維護工作,並使 Web 站點易於擴展。WebSphere Studio Version 5.0 提供了一個易於使用的 Struts 插件來幫助您維護和擴展您的 Web 站點。

相關信息

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章