第一個Struts程式的入門是針對未接觸過Struts的讀者所安排的內容,目的在讓您了快速了解與知道如何撰寫第一個Struts程式,如果您想要下載最新的Struts實作,則可以至http://struts.apache.org進行下載。
對於第一個Struts程式來說,您需要以下的.jar檔,您可以分別在Spring下載檔案的lib目錄下的jakarta-commons與struts目錄下找到這些.jar檔案:
- struts.jar
- commons-beanutils.jar
- commons-digester.jar
- commons-collections.jar
- commons-logging.jar
在Struts中,擔任前端控制器(Front Controller)的是org.apache.struts.action.ActionServlet類別,您必須在web.xml中加以定義,並指定Struts設定檔案的位置與名稱,例如:
<?xml version="1.0" encoding="UTF-8"?>
<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"> <session-config> <session-timeout> 30 </session-timeout> </session-config> <!-- 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>1</load-on-startup> </servlet>
<!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
ActionServlet的"config"屬性用來設定Struts設定檔案的位置與名稱,在以上的設定中,所有對*.do的請求都會交由ActionServlet來轉發請求給控制物件處理。
在Struts中,控制物件的實作是透過繼承org.apache.struts.action.Action類別,並重新定義其execute()方法來完成,例如在以下的實作中,將取得使用者的"user"請求參數值,並設定給一個Map型態的Model物件:
package onlyfun.caterpillar;
import java.util.*; import javax.servlet.http.*; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;
public class HelloAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String username = request.getParameter("user");
Map model = new HashMap(); if(username != null) { model.put("username", username); } else { model.put("username", "nobody"); }
request.setAttribute("userInfo", model);
return mapping.findForward("helloUser"); } }
在請求處理完成之後,必須由org.apache.struts.action.ActionMapping來查找呈現頁面的位址,ActionMapping是在Struts定義檔中的定義實例代表,來看一下Struts定義檔是如何撰寫的:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration → 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1 → _2.dtd"> <struts-config> <action-mappings> <action path="/hello" type="onlyfun.caterpillar.HelloAction"> <forward name="helloUser" path="/WEB-INF/jsp/hello.jsp"/> </action> </action-mappings> </struts-config>
在Struts定義檔中,<action>標籤的"path"屬性設定當請求路徑為hello.do時,ActionServlet將交給
HelloAction的實例來處理使用者的請求,而<forward>標籤設定的是使用ActionMapping物件的
findForward()方法查找名稱時,所以告知的下一個呈現頁面的位址,例如在HelloAction中findForward()中的名稱是
"helloUser",因此請求處理完畢後,將由/WEB-INF/jsp/hello.jsp來呈現結果畫面,hello.jsp的撰寫如下所示:
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Struts Demo</title> </head> <body> <H1>Hello, ${userInfo["username"]} !</H1> </body> </html>
hello.jsp網頁只是將request中的Map物件中"username"資料顯示出來,有關於Struts更詳盡的介紹,可以 Struts 學習筆記。
|