ITEEDU

第一个 Struts  程式

第一个 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 设定档案的位置与名称,例如:

•      web.xml
<?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 物件:

•      HelloAction.java
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 定义档是如何撰写的:

•      struts‐config.xml
<?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 的撰写如下所示:

•      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 学习笔记。