Struts原理與應用(二)

來源:互聯網
上載者:User
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"><br />請輸入值<br /><input type="text" name="test"/><br /><br><br><br /><input type="submit" name="Submit" ><br /><input name="reset" type="reset" ><br /></form><br />

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

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

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.