STRUTS

1 STRUTS 2

這是一個 STRUTS 2 的範例程式,取自於 STRUTS 2 IN ACTION 一書的範例。 此網頁應用程式彙整每一個章節的範例。

目錄結構:

C:\apache-tomcat-7.0.25\webapps\Struts2InAction\
index.html
menu\Menu.jsp
chapterTwo\*.jsp
chapterThree\*.jsp
chapterFour\*.jsp
chapterFive\*.jsp
chapterSix\*.jsp
chapterSeven\*.jsp
chapterEight\*.jsp
chapterNine*.jsp
chapterTen\*.jsp
chapterEleven\*.jsp
WEB-INF\
        web.xml
        lib\
            *.jar
        classes\
                struts.xml
        classes\manning\chapterTwo\
        classes\manning\chapterTwo\
           chapterTwo.xml
           HelloWorld.java
           AnnotatedNameCollector.java
           AnnotatedHelloWorldAction.java
        classes\manning\chapterThree\
             chapterThree.xml
             ImageUpload.class
             Register.class
             Register.properties
             Register_es.properties
             modelDriven\
             objectBacked\
             utils\
        classes\manning\chapterFour\
            chapterFour.xml
            *.class
            *.properties
            utils\
        classes\manning\chapterFive\
            chapterFive.xml
            *.class
            *.properties
            utils\
        classes\manning\chapterSix\
            chapterSix.xml
            *.class
            *.properties
            utils\
        classes\manning\chapterSeven\
             chapterSeven.xml
             *.class
             *.properties
             utils\
        classes\manning\chapterEight\
             chapterEight.xml
             *.class
             *.properties
            utils\
        classes\manning\chapterNine\
            chapterNine.xml
            *.class
            *.properties
        classes\manning\chapterTen\
            chapterTen.xml
            *.class
            *.properties
        classes\manning\chapterEleven\
              chapterEleven.xml
              *.class
              *.properties


2 WEB.XML

WEB-INF\WEB.XML 是一般網頁應用程式的部署描述器,用來宣告網頁應用程式的伺服程式(SERVLET)和攔截器等,並由伺服器的容器執行之。 一支網頁應用程式只會有一個 WEB.XML 檔案,可以在這個檔案中宣告多支伺服程式和攔截器(FILTER)。

宣告此 XML 檔的版本規格,版本 XML 1.0,文字的編碼規格為 UTF-8。

   WEB-INF\WEB.XML
01 <?xml version="1.0" encoding="UTF-8"?>

宣告此 XML 檔為網路應用程式 web-app,版本 2.4。 XML 命名空間為 http://java.sun.com/xml/ns/j2ee。 內容類型為網路應用程式 Web Application 2.2,根據的文件類型定義檔為 web-app_2_2.dtd。 後面的

   WEB-INF\WEB.XML
01 <web-app version="2.4" 
01 xmlns="http://java.sun.com/xml/ns/j2ee" 
01 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
01 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

定義顯示的網路應用程式名稱。

   WEB-INF\WEB.XML
01 <display-name>S2 Example Application - Chapter 1 - Hello World</display-name>

宣告第一個攔截器,名稱為 struts2,執行的類別為 FilterDispatcher。 攔截器的用途是在網頁伺服器中,將屬於這個網頁應用程式的要求攔下,不理會其他要求,因此稱為攔截器(或過濾器)。 設定一個參數 actionPackages 值為 manning。

   WEB-INF\WEB.XML
01 <filter>
01     <filter-name>struts2</filter-name>
01     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
01     <init-param>
01  		<param-name>actionPackages</param-name>
01 		<param-value>manning</param-value>
01 	</init-param>  
01 </filter>

攔截器接受所有要求。 當攔截器識別 STRUTS 2 的要求,基本上就是字尾是 .action 的要求,交由 STRUTS 2 系統處理。

   WEB-INF\WEB.XML
01 <filter-mapping>
01    <filter-name>struts2</filter-name>
01    <url-pattern>/*</url-pattern>
01 </filter-mapping>

第二個攔截器,名稱為 SpringOpenEntityManagerInViewFilter,執行的類別為 OpenEntityManagerInViewFilter,攔截所有要求??。

   WEB-INF\WEB.XML
01 <filter>
01     <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
01     <filter-class>
01         org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
01     </filter-class>
01 </filter>
01 
01 <filter-mapping>
01     <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
01     <url-pattern>/*</url-pattern>
01 </filter-mapping>

設置監聽器,監聽類別為 ContextLoaderListener。這個監聽器是用來配合 SPRING,致能 SPRING 物件功能和自動繞線轉譯器。

   WEB-INF\WEB.XML
01 <listener>
01     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
01 </listener>

啟動一個伺服程式,伺服類別為 AnotherServlet,用來處理所有路徑為 /anotherServlet 的要求。

   WEB-INF\WEB.XML
01 <servlet>
01     <servlet-name>anotherServlet</servlet-name>
01     <servlet-class>manning.servlet.AnotherServlet</servlet-class>
01 </servlet>
01 <servlet-mapping>
01 	<servlet-name>anotherServlet</servlet-name>
01 	<url-pattern>/anotherServlet</url-pattern>
01 </servlet-mapping>

設定歡迎頁為應用程式根目錄的網頁檔 index.html。

   WEB-INF\web.xml
01 <welcome-file-list>
01     <welcome-file>index.html</welcome-file>
01 </welcome-file-list>


3 STRUTS.XML

STRUTS 2 的網頁應用程式組態檔是在 WEB-INF\classes\struts.xml。 不同於 WEB.XML 是被網頁伺服器讀取,STRUTS.XML 是由網頁應用程式的伺服程式讀取,用來設定網頁應用程式的內部執行參數。

宣告文件為 STRUTS 2.0 的組態檔。

   WEB-INF\classes\struts.xml
01 <?xml version="1.0" encoding="UTF-8" ?>
01 <!DOCTYPE struts PUBLIC
01     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
01     "http://struts.apache.org/dtds/struts-2.0.dtd">

啟用 devMode,這使得當要求所載入的資源出錯時,可以在錯誤頁上顯示相關資訊。

   WEB-INF\classes\struts.xml
01     <constant name="struts.devMode" value="true" />

定義動作映射,當執行 Menu.action 時,會轉送到 menu/Menu.jsp,執行另一個網頁檔。 將原始檔 WEB-INF\src\manning\chapterTwo.xml 包含到 struts.xml,成為同一個檔案。

   WEB-INF\classes\struts.xml
01 <package name="default" namespace="/" extends="struts-default"> 
01     <action name="Menu">
01     <result>/menu/Menu.jsp</result>
01     </action>
01 </package>

使用個別的 XML 檔描述每一章的網頁應用程式的動作映射,在這裡將所有動作映射包含進來,成為 STRUTS 組態的一部分。

   WEB-INF\classes\struts.xml
01 <include file="manning/chapterTwo/chapterTwo.xml"/>
01 <include file="manning/chapterThree/chapterThree.xml"/> 
01 <include file="manning/chapterThree/objectBacked/chapterThree.xml"/>
01 <include file="manning/chapterThree/modelDriven/chapterThree.xml"/>
01 <include file="manning/chapterFour/chapterFour.xml"/>
01 <include file="manning/chapterFive/chapterFive.xml"/>
01 <include file="manning/chapterSix/chapterSix.xml"/>
01 <include file="manning/chapterSeven/chapterSeven.xml"/>
01 <include file="manning/chapterEight/chapterEight.xml"/>
01 <include file="manning/chapterNine/chapterNine.xml"/>
01 <include file="manning/chapterTen/chapterTen.xml"/>
01 <include file="manning/chapterEleven/chapterEleven.xml"/>

舉例來說,chapterTwo.xml 描述二個動作映射,Name、HelloWorld。 動作 Name 的映射路徑是 chapterTwo/Name,功能是將網頁轉送到 chapterTwo/NameCollector.jsp。 動作 HelloWorld 的映射路徑是 chapterTwo/HelloWorld,功能是執行 HelloWorld 類別成功後,將網頁轉送到 chapterTwo/HelloWorld.jsp。

   WEB-INF\classes\manning\chapterTwo\chapterTwo.xml
01 <struts>
01     <package name="chapterTwo" namespace="/chapterTwo" extends="struts-default">
01 		<action name="Name">
01    			<result>/chapterTwo/NameCollector.jsp</result>
01 		</action>	
01 		<action name="HelloWorld" class="manning.chapterTwo.HelloWorld">
01 			<result name="SUCCESS">/chapterTwo/HelloWorld.jsp</result>
01      	</action>		
01     </package>
01 </struts>


4 INDEX.HTML

歡迎頁 INDEX.HTML 是使用者要求所觸發的第一個網頁檔。 這個網頁檔顯示等候訊息字串 One moment please,同時將網頁導向 Menu.action。

   INDEX.HTML
01 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
01 <html>
01 <head>
01   <META HTTP-EQUIV="Refresh" CONTENT="0;URL=Menu.action">
01 </head>
01 <body>
01   <h3>One moment please.</h3>
01 </body>
01 </html>


5 MENU.JSP

INDEX.HTML 執行動作 Menu.action,轉送到 /menu/Menu.jsp。 MENU.JSP 有一個超連結,會觸發動作 chapterTwo/Name,將網頁轉送到 /chapterTwo/NameCollector.jsp。 所有在 MENU.JSP 連結的動作都是由 STRUTS.XML 和其所包含其他 XML 檔所定義。

STRUTS2

   menu\Menu.jsp
01 <%@ page contentType="text/html; charset=UTF-8" %>
01 <%@ taglib prefix="s" uri="/struts-tags" %>
01 <html>
01 <head>
01 <title>Struts 2 in Action: Menu</title>
01 </head>
01 <body>
01   <hr>
01   <ul>
01      <li><a href="<s:url action='chapterTwo/Name'/>">HelloWorld</a></li>
01      <li><a href="<s:url action='chapterTwo/annotatedNameCollector'/>">AnnotatedHelloWorld</a></li>
01      <li><a href="<s:url action='chapterThree/PortfolioHomePage'/>">Struts 2 Portfolio (Chapter 3)</a></li>	
01      <li><a href="<s:url action='chapterFour/PortfolioHomePage'/>">Struts 2 Portfolio (Chapter 4)</a></li>
01      <li><a href="<s:url action='chapterFive/PortfolioHomePage'/>">Struts 2 Portfolio (Chapter 5)</a></li>
01      <li><a href="<s:url action='chapterSix/PortfolioHomePage'/>">Struts 2 Portfolio (Chapter 6)</a></li>
01      <li><a href="<s:url action='chapterSeven/PortfolioHomePage'/>">Struts 2 Portfolio (Chapter 7)</a></li>
01      <li><a href="<s:url action='chapterEight/PortfolioHomePage'/>">Struts 2 Portfolio (Chapter 8)</a></li>
01      <li><a href="<s:url action='chapterNine/PortfolioHomePage'/>">Struts 2 Portfolio (Chapter 9)</a></li>
01      <li><a href="<s:url action='chapterTen/PortfolioHomePage'/>">Struts 2 Portfolio (Chapter 10)</a></li>
01      <li><a href="<s:url action='chapterEleven/PortfolioHomePage'/>">Struts 2 Portfolio (Chapter 11)</a></li>
01   </ul>
01   <hr>   	
01 </body>
01 </html>


6 CHAPTERTWO

從 menu/Menu.jsp 點入 HelloWorld,觸發動作 chapterTwo/Name,網頁轉送到 chapterTwo/NameCollector.jsp。

動作 chapterTwo/Name 將網頁轉送到 chapterTwo/NameCollector.jsp。

   WEB-INF\classes\manning\chapterTwo\chapterTwo.xml
01 <package name="chapterTwo" namespace="/chapterTwo" extends="struts-default">
01     <action name="Name">
01         <result>/chapterTwo/NameCollector.jsp</result>
01     </action>	
01     ....
01 </package>

網頁檔 NameCollector.jsp 會產生一個表格,表格中有一個文字區域,用來填入名字。 名字填完後,按下 submit 按鈕,觸發動作 HelloWorld。

STRUTS2

   chapterTwo\NameCollector.jsp
01 <h4>Enter your name so that we can customize a greeting just for you!</h4> 	
01 <s:form action="HelloWorld">
01 <s:textfield name="name" label="Your name"/>
01 <s:submit/>
01 </s:form>

動作 HelloWorld 的動作類別是 manning.chapterTwo.HelloWorld。 這個動作類別會將輸入的名字字串加工成 Hello xxxx。 完成後,將網頁轉送到 chapterTwo/HelloWorld.jsp。

   WEB-INF\classes\manning\chapterTwo\chapterTwo.xml
01 		<action name="HelloWorld" class="manning.chapterTwo.HelloWorld">
01 			<result name="SUCCESS">/chapterTwo/HelloWorld.jsp</result>
01      	</action>		

動作類別 HelloWorld.class 的檔案位置是 WEB-INF\classes\manning\chapterTwo。
原始程式碼 HelloWorld.java 的檔案位置是 WEB-INF\src\manning\chapterTwo。
HelloWorld.java 提供三個方法 execute、getCustomGreeting、setCustomGreeting。
方法 execute 是給動作映射用的,方法 getCustomGreeting、setCustomGreeting 是 java bean 的 SETTER 和 GETTER 方法,可於 JSP 中設定資料與取得資料。

   WEB-INF\classes\manning\chapterTwo\HelloWorld.java
01 package manning.chapterTwo;
01 public class HelloWorld {
01 	private static final String GREETING = "Hello ";
01     public String execute()  {
01     	setCustomGreeting( GREETING + getName() );
01     	return "SUCCESS";
01     }
01     private String name;
01     public String getName() {
01         return name;
01     }
01     public void setName(String name) {
01         this.name = name;
01     }
01     private String customGreeting;
01     public String getCustomGreeting() {
01     	return customGreeting;
01     }
01     public void setCustomGreeting( String customGreeting ){
01     	this.customGreeting = customGreeting;
01     }
01 }

HelloWorld.jsp 使用 STRUTS 標籤,將儲存在動作類別 HelloWorld.class 的字串讀出,並顯示於網頁。

STRUTS2

   chapterTwo\HelloWorld.jsp
01 <%@ taglib prefix="s" uri="/struts-tags" %>
01 ....
01 <hr>
01     <h3>Custom Greeting Page</h3>      
01     <h4><s:property value="customGreeting"/></h4>
01 <hr>


7 CHAPTERTWO

這裡展示一種透過 JAVA 設定的動作映射技巧,動作映射的設定是在 JAVA 程式碼中,不是在 STRUTS.XML。 從 menu/Menu.jsp 點入 AnnotatedHelloWorld,觸發動作 chapterTwo/annotatedNameCollector,執行 WEB-INF\classes\manning\chapterTwo\AnnotatedHelloWorldAction.class。 動作成功後,網頁轉送到 chapterTwo/NameCollector.jsp。

   WEB-INF\classes\manning\chapterTwo\AnnotatedHelloWorldAction.java
01 @Result(name="SUCCESS", value="/chapterTwo/HelloWorld.jsp" )

AnnotatedHelloWorldAction.java 程式碼和 HelloWorld.java 幾乎一模一樣,僅有一行的差異,就是設定動作映射的 @Result(....) 。

   WEB-INF\classes\manning\chapterTwo\AnnotatedHelloWorldAction.java
01 package manning.chapterTwo;
01 import org.apache.struts2.config.Result;
01 import org.apache.struts2.dispatcher.ServletDispatcherResult;
01 @Result(name="SUCCESS", value="/chapterTwo/HelloWorld.jsp" )
01 public class AnnotatedHelloWorldAction {
01 	private static final String GREETING = "Hello ";
01     public String execute()  {
01     	setCustomGreeting( GREETING + getName() );
01     	return "SUCCESS";
01     }
01     private String name;
01     public String getName() {
01         return name;
01     }
01     public void setName(String name) {
01         this.name = name;
01     }
01     private String customGreeting;
01     public String getCustomGreeting(){
01     	return customGreeting;
01     }
01     public void setCustomGreeting( String customGreeting ){
01     	this.customGreeting = customGreeting;
01     }
01 }


8 CHAPTERTHREE

這裡講解第三章的範例。


8.1 動作群

第三章的範例的動作映射定義在 chapterThree.xml,該檔案是在 WEB-INF\classes\manning\chapterThree\chapterThree.xml,。 此檔案定義了八個動作, 在 chapterThree 路徑下有四個動作 PortfolioHomePage、ViewPortfolios、Registration、Register, 在 /chapterThree/secure 路徑下有四個動作 AdminPortfolio、AddImage、ImageUpload、RemoveImage。

第一個動作,動作名稱 PortfolioHomePage,動作路徑 chapterThree,動作全名 chapterThree/PortfolioHomePage。
動作的功能是將網頁轉送到 chapterThree/PortfolioHomePage.jsp。

   WEB-INF\classes\manning\chapterThree\chapterThree.xml
01 <package name="chapterThreePublic" namespace="/chapterThree" extends="struts-default">
01 	<action name="PortfolioHomePage">
01 		<result>/chapterThree/PortfolioHomePage.jsp</result>
01 	</action>
01 	....
01 </package>

第二個動作,動作名稱 ViewPortfolios,動作路徑 chapterThree,動作全名 chapterThree/ViewPortfolios。
動作的功能是將網頁轉送到 chapterThree/ViewPortfolios.jsp。

   WEB-INF\classes\manning\chapterThree\chapterThree.xml
01 <package name="chapterThreePublic" namespace="/chapterThree" extends="struts-default">
01 	....
01 	<action name="ViewPortfolios">
01 		<result>/chapterThree/ViewPortfolios.jsp</result>
01 	</action>
01 	....
01 </package>

第三個動作,動作名稱 Registration,動作路徑 chapterThree,動作全名 chapterThree/Registration。
動作的功能是將網頁轉送到 chapterThree/Registration.jsp。

   WEB-INF\classes\manning\chapterThree\chapterThree.xml
01 <package name="chapterThreePublic" namespace="/chapterThree" extends="struts-default">
01 	....
01 	<action name="Registration">
01 		<result>/chapterThree/Registration.jsp</result>
01 	</action>
01 	....
01 </package>

第四個動作,動作名稱 Register,動作路徑 chapterThree,動作全名 chapterThree/Register。
動作的功能是先執行類別 Register,成功後,將網頁轉送到 chapterThree/RegistrationSuccess.jsp。

   WEB-INF\classes\manning\chapterThree\chapterThree.xml
01 <package name="chapterThreePublic" namespace="/chapterThree" extends="struts-default">
01 	....
01 	<action name="Register" class="manning.chapterThree.Register">
01 		<result>/chapterThree/RegistrationSuccess.jsp</result>
01 		<result name="input">/chapterThree/Registration.jsp</result>
01 	</action>
01 </package>

第五個動作,動作名稱 AdminPortfolio,動作路徑 chapterThree/secure,動作全名 chapterThree/secure/AdminPortfolio。
動作的功能是將網頁轉送到 chapterThree/PortfolioHomePage.jsp。

   WEB-INF\classes\manning\chapterThree\chapterThree.xml
01 <package name="chapterThreeSecure" namespace="/chapterThree/secure" extends="struts-default">
01 	<action name="AdminPortfolio">
01 		<result>/chapterThree/AdminPortfolio.jsp</result>
01 	</action>
01 	....
01 </package>

第五個動作,動作名稱 AddImage,動作路徑 chapterThree/secure,動作全名 chapterThree/secure/AddImage。
動作的功能是將網頁轉送到 chapterThree/PortfolioHomePage.jsp。

   WEB-INF\classes\manning\chapterThree\chapterThree.xml
01 <package name="chapterThreeSecure" namespace="/chapterThree/secure" extends="struts-default">
01 	....
01 	<action name="AddImage">
01 		<result>/chapterThree/ImageUploadForm.jsp</result>
01 	</action>
01 	....
01 </package>

第五個動作,動作名稱 ImageUpload,動作路徑 chapterThree/secure,動作全名 chapterThree/secure/ImageUpload。
動作的功能是先執行類別 ImageUpload,成功後,將網頁轉送到 chapterThree/ImageAdded.jsp。

   WEB-INF\classes\manning\chapterThree\chapterThree.xml
01 <package name="chapterThreeSecure" namespace="/chapterThree/secure" extends="struts-default">
01 	....
01 	<action name="ImageUpload"
01 		class="manning.chapterThree.ImageUpload">
01 		<param name="fileSystemPath">./portfolioFS/</param>
01 		<result>/chapterThree/ImageAdded.jsp</result>
01 		<result name="input">
01 			/chapterThree/ImageUploadForm.jsp
01 		</result>
01 	</action>
01 	....
01 </package>

第八個動作,動作名稱 RemoveImage,動作路徑 chapterThree/secure,動作全名 chapterThree/secure/RemoveImage。
動作的功能是將網頁轉送到 chapterThree/ImageRemoved.jsp。

   WEB-INF\classes\manning\chapterThree\chapterThree.xml
01 <package name="chapterThreeSecure" namespace="/chapterThree/secure" extends="struts-default">
01 	....
01 	<action name="RemoveImage">
01 		<result>/chapterThree/ImageRemoved.jsp</result>
01 	</action>
01 </package>


8.2 動作流程

從 menu/Menu.jsp 點入 CHAPTER 3,觸發動作 chapterThree/PortfolioHomePage,網頁轉送到 chapterThree/PortfolioHomePage.jsp。

STRUTS2

01 chapterThree/PortfolioHomePage.jsp
01 <%@ page contentType="text/html; charset=UTF-8" %>
01 <%@ taglib prefix="s" uri="/struts-tags" %>
01 <html>
01 <head>
01 <title>PortfolioHomePage</title>
01 </head>
01 <body>
01 <h4>Welcome to the Struts 2 Portfolio!</h4> 	
01 <ul>
01 <li><a href="<s:url action='Registration'/>">Create an Account</a></li>
01 <li><a href="<s:url action='RegistrationOB'/>">Create an Account  ( Object Backed Version )</a></li>
01 <li><a href="<s:url action='RegistrationMD'/>">Create an Account  ( Model Driven Version )</a></li>	
01 </ul> 
01 </body>
01 </html>

在網頁 PortfolioHomePage.jsp,中點入 Create an Account,觸發動作 chapterThree/Registration,網頁轉送到 chapterThree/Registration.jsp。

STRUTS2

01 chapterThree/Registration.jsp
01 <%@ page contentType="text/html; charset=UTF-8" %>
01 <%@ taglib prefix="s" uri="/struts-tags" %>
01 <html>
01 <head>
01 <title>Portfolio Registration</title>
01 </head>
01 <body>
01 <h4>Complete and submit the form to create your own portfolio.</h4> 
01 <s:form action="Register">
01 <s:textfield name="username" label="Username"/>
01 <s:password name="password" label="Password"/>
01 <s:textfield name="portfolioName" label="Enter a name for your portfolio"/>  	
01 <s:submit/>
01 </s:form>	
01 </body>
01 </html>

在網頁 Registration.jsp 填寫帳號和密碼後,按下 Submit 按鈕,觸發動作 chapterThree/Register,網頁轉送到 chapterThree/RegistrationSuccess.jsp。 接著,控制器會執行類別 manning.chapterThree.Register,完成註冊的相關內部動作。 完成後,轉向 chapterThree/RegistrationSuccess.jsp,顯示註冊完成的訊息。

STRUTS2

   chapterThree/RegistrationSuccess.jsp
01 <%@ page contentType="text/html; charset=UTF-8" %>
01 <%@ taglib prefix="s" uri="/struts-tags" %>
01 <html>
01 <head>
01 <title>Registration Success</title>
01 </head>
01 <body>
01 <h5>Congratulations! You have created </h5>
01 <h3>The <s:property value="portfolioName" /> Portfolio</h3>
01 <p>You may now begin working with <a href="<s:url action='secure/AdminPortfolio'/>">your portfolio.</a></p>
01 </body>
01 </html>

在網頁 RegistrationSuccess.jsp 中點入 your portfolio,觸發動作 secure/AdminPortfolio,將網頁轉送到 chapterThree/AdminPortfolio.jsp。

STRUTS2

   chapterThree/AdminPortfolio.jsp
01 <%@ page contentType="text/html; charset=UTF-8" %>
01 <%@ taglib prefix="s" uri="/struts-tags" %>
01 <html>
01 <head>
01 <title>Administer Portfolio</title>
01 </head>
01 <body>
01 <h4></h4>
01 <ul>
01 <li><a href="<s:url action='AddImage'/>">Add image to portfolio.</a></li>	
01 <li><a href="<s:url action='RemoveImage'/>">Remove an image from portfolio.</a></li>	
01 </ul> 	
01 </body>
01 </html>

在網頁 AdminPortfolio.jsp 中點入 Add image to portfolio,觸發動作 secure/AddImage,將網頁轉送到 chapterThree/ImageUploadForm.jsp。 這是一個將圖檔上傳的網頁,透過瀏覽選擇圖檔,確定後按下 Submit,觸發 secure/ImageUpload,執行類別 manning.chapterThree.ImageUpload,將檔案上傳。 檔案上傳成功後,網頁轉送到 chapterThree/ImageAdded.jsp,顯示檔案加入的成功訊息。

STRUTS2

01 <%@ page contentType="text/html; charset=UTF-8" %>
01 <%@ taglib prefix="s" uri="/struts-tags" %>
01 <html>
01 <head>
01 <title>Image Upload Form</title>
01 </head>
01 <body>
01 <h4>Complete and submit the form to create your own portfolio.</h4> 
01 <s:form action="ImageUpload" method="post" enctype="multipart/form-data">
01 <s:file name="pic" label="Picture"/> 
01 <s:submit/>
01 </s:form>	
01 </body>
01 </html>

檔案上傳成功後,網頁轉送到 chapterThree/ImageAdded.jsp,顯示檔案加入的成功訊息。

STRUTS2

9 CHAPTERFOUR

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

10 CHAPTERFIVE

STRUTS2

,。

STRUTS2

,。

STRUTS2

無法註冊成功,因為 AGE 欄位無法轉換成 DOUBLE PRECESION,出現錯誤訊息。

STRUTS2

11 CHAPTERSIX

第六章的範例的動作映射定義在 chapterSix.xml,該檔案是在目錄 WEB-INF\classes\manning\chapterSix。 此檔案定義了二個命名空間 chapterSix、chapterSix/secure。命名空間中的標籤的用法是[空間名稱\標籤名稱]。

命名空間 chapterSix 有標籤 PortfolioHomePage、ViewPortfolios、ViewPortfolio、LoginForm、Login、 Registration、Register、ErrorProne、PropertyTag、SetTag、PushTag、BeanTagWithVar、BeanTagWithValueStack、ActionTag、 ActionTagNoResult、TargetAction、IteratorTag、IfElseTags、IncludeTag、UrlTag、InternationalizationTags、ParamTag、 DebugTag。

命名空間 chapterSix/secure 有標籤 AdminPortfolio、AddImage、ImageUpload、RemoveImage。

從 menu/Menu.jsp 點入 CHAPTER 6,觸發動作 chapterSix/PortfolioHomePage,根據標籤定義,網頁會轉送到 chapterSix/PortfolioHomePage.jsp。 這個網頁要展示登入與註冊表格的用法,也會展示 12 種 STRUTS 標籤的用法。

STRUTS2

01 <%@ page contentType="text/html; charset=UTF-8" %>
01 <%@ taglib prefix="s" uri="/struts-tags" %>
01 <html>
01     <head>
01     <title>PortfolioHomePage</title>
01     </head>
01     <body>
01         <h4>Welcome to the Struts 2 Portfolio!</h4>     
01         <ul>
01           <li><a href="<s:url action='LoginForm'/>">Login to an Existing Account</a></li>    
01           <li><a href="<s:url action='Registration'/>">Create an Account</a></li>    
01          </ul> 
01          <br/>
01          <br/>
01          <h5>Demos of Struts 2 Tags</h5>
01          <ul> 
01           <li><a href="<s:url action='ActionTag'/>"><action> Tag (Includes Secondary Result)</a></li>
01           <li><a href="<s:url action='ActionTagNoResult'/>"><action> Tag (Doesn't Include Secondary Result)</a></li>
01           <li><a href="<s:url action='BeanTagWithVar'/>"><bean> Tag Using 'var' Attribute to Put Bean in ActionContext</a></li>        
01           <li><a href="<s:url action='BeanTagWithValueStack'/>"><bean> Tag Using ValueStack</a></li>        
01           <li><a href="<s:url action='DebugTag'/>"><debug> Tag</a></li>
01           <li><a href="<s:url action='IfElseTags'/>"><if>, <elseif>, and <else> Tags</a></li>    
01           <li><a href="<s:url action='InternationalizationTags'/>"><i18n> and <text> Tags</a></li>    
01           <li><a href="<s:url action='IteratorTag'/>"><iterator> Tag</a></li>    
01           <li><a href="<s:url action='PropertyTag'/>"><property> Tag</a></li>    
01           <li><a href="<s:url action='PushTag'/>"><push> Tag</a></li>    
01           <li><a href="<s:url action='SetTag'/>"><set> Tag</a></li>    
01           <li><a href="<s:url action='UrlTag'/>"><url> Tag</a></li>              
01           </ul>
01     </body>
01 </html>


12 CHAPTERSEVEN

第七章的範例的動作映射定義在 chapterSeven.xml,該檔案是在目錄 WEB-INF\classes\manning\chapterSeven\。

STRUTS2

01 <%@ page contentType="text/html; charset=UTF-8"%>
01 <%@ taglib prefix="s" uri="/struts-tags"%>
01 <html>
01 <head>
01 <title>PortfolioHomePage</title>
01 <s:head />
01 </head>
01 <body>
01 <h4>Welcome to the Struts 2 Portfolio!</h4>
01 <hr>
01 <h5>Account Management</h5>
01 <ul>
01 	<li><a href="<s:url action='LoginForm'/>">Login to an Existing Account</a></li>
01 	<li><a href="<s:url action='Registration'/>">Create an Account</a></li>
01 </ul>
01 <hr>
01 <h5>Browse an Artist's Portfolios ( Demo of select component. )</h5>
01 <s:form action="SelectPortfolio">
01 	<s:select name="username" list='users' listKey="username"
01 		listValue="username" label="Select an artist" />
01 	<s:submit value="Browse" />
01 </s:form>
01 <hr>
01 <h5>View a Portfolio ( Demo of doubleselect component. )</h5>
01 
01 <s:form action="ViewPortfolio">
01 	<s:doubleselect name="username" list='users' listKey="username"
01 		listValue="username" doubleName="portfolioName"
01 		doubleList="portfolios" doubleListKey="value.name"
01 		doubleListValue="value.name" label="Select an artist and portfolio" />
01 	<s:submit value="View" />
01 </s:form>
01 </body>
01 </html>
01 </td><td width="5%"></td></tr>
01 </table>

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

,。

STRUTS2

13CHAPTEREIGHT

第八章的範例的動作映射定義在 chapterEight.xml,該檔案是在目錄 WEB-INF\classes\manning\chapterEight\。

STRUTS2

,。

STRUTS2

,。

STRUTS2

14 CHAPTERNINE


15 CHAPTERTEN


16 CHAPTERELEVEN