Struts原理與應用(二)

Chapter 3: Struts Configuration

Struts principle and practice

Struts可以運行在任何一個支持JSP1.2和Servlet2.3的WEB Container中

Struts將所有的請求提交到同一個中心控制器,org.apache.struts.action.ActionServlet 類

web.xml配置

<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-mapping>

一個標準的使用了Struts的URL樣式如下:

擴展映射:http://www.my_site_name.com/mycontext/actionName.do

路徑映射:http://www.my_site_name.com/mycontext/do/action_Name

 

<servlet-name>action</servlet-name>
<url-pattern>*.do或/do/*</url-pattern>
</servlet-mapping>

Struts運行

Struts首先在Container啓動的時候調用ActionServlet的init()方法。初始化各種配置。這些配置寫在struts-config.xml文件中。

一個標準的struts-config文件包含如下結構:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
    <data-sources /> // 定義數據源
    <form-beans />  // 定義ActionForm
    <global-exceptions /> // 定義全局異常
    <global-forwards /> // 定義全局轉向url
    <action-mappings />  // 定義action
    <controller /> // 配置Controller
    <message-resources /> // 配置資源文件
</struts-config>
 

Struts由上述幾部分組成。其中最主要的是Action和Form。下面簡單敘述一下其處理過程。

一個請求提交給ActionServlet,ActionServlet會尋找相應的Form和Action,首先將提交的request對象,映射到form中。,然後將form傳遞給action來進行處理。action得到form,對xml的mapping,request,response四個對象,並調用execute()方法然後返回一個forward-url(相應視圖)給ActionServlet,最終返回給客戶端。

我們來看一個最簡單的實例。

 

Chapter 4: Example 1: Basic Framework

Struts principle and practice

說明:實例一是最簡單的Struts程序。它僅僅使用了1個form和1個action
功能是將首頁輸入的值傳遞給action,經過判斷後返回結果。如果是空則返回empty
代碼如下:

input.jsp:
<form method="post" action="/example.do">請輸入值<input type="text" name="test"/><br><br><input type="submit" name="Submit" ><input name="reset" type="reset" ></form>

struts-config.xml:
<struts-config> // 配置formbean <form-beans> <form-bean name="exampleActionForm" type="com.zotn.struts.example1.ExampleActionForm" /> </form-beans> // 配置action <action-mappings> <action name="exampleActionForm" path="/example" type="com.zotn.struts.example1.ExampleAction"> // action內部的foward <forward name="foward" path="/foward.jsp" /> </action> </action-mappings></struts-config>

Action:
public class ExampleAction extends Action { public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) { // 得到對應的form ExampleActionForm eaf = (ExampleActionForm)actionForm; // 取得輸入的test String test = eaf.getTest(); // 判斷並將值放入request if ("".equals(test)){ request.setAttribute("test","empty"); }else{ request.setAttribute("test",test); } // 通過mapping尋找相應的url,返回ActionFoward return actionMapping.findForward("foward"); }}

FormBean:
public class ExampleActionForm extends ActionForm { private String test; public String getTest() { return test; } public void setTest(String test) { this.test = test; }}

 



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