熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts開發指南之工作流程實例演示

2013-11-23 20:22:56  來源: Java開源技術 

  下圖是Struts的工作流程前邊我們提到所有的請求都提交給ActionServlet來處理
  

  ActionServlet是一個FrontController它是一個標准的Servlet它將request轉發給RequestProcessor來處理
  
  ActionMapping是ActionConfig的子類實質上是對strutsconfigxml的一個映射從中可以取得所有的配置信息
  
  RequestProcessor根據提交過來的url如*do從ActionMapping 中得到相應的ActionForn和Action然後將request的參數對應到ActionForm中進行form驗證如果驗證通過則調用Action的execute()方法來執行Action最終返回ActionFoward
  
  ActionFoward是對mapping中一個foward的包裝對應於一個url
  
  ActionForm使用了ViewHelper模式是對HTML中form的一個封裝其中包含有validate方法用於驗證form數據的有效性ActionForm是一個符合JavaBean規范的類所有的屬性都應滿足get和set對應對於一些復雜的系統還可以采用DynaActionForm來構造動態的Form即通過預制參數來生成Form這樣可以更靈活的擴展程序
  
  ActionErrors是對錯誤信息的包裝一旦在執行action或者formvalidate中出現異常即可產生一個ActionError並最終加入到ActionErrors在Form驗證的過程中如果有Error發生則會將頁面重新導向至輸入頁並提示錯誤
  
  Action是用於執行業務邏輯的RequsestHandler每個Action都只建立一個instanceAction不是線程安全的所以不應該在Action中訪問特定資源一般來說應改使用 Business Delegate 模式來對Business tier進行訪問以解除耦合
  
  Struts提供了多種Action供選擇使用普通的Action只能通過調用execute執行一項任務而DispatchAction可以根據配置參數執行而不是僅進入execute()函數這樣可以執行多種任務如insertupdate等LookupDispatchAction可以根據提交表單按鈕的名稱來執行函數
  
  我們可以先回到剛才的例子理解一下Struts的流程
  
  下面我們看Struts自帶的example實例
  
  說明實例二是Struts自帶的example程序 實現了登錄注冊修改功能
  
  代碼中大量應用了struts taglib並且采用validator插件進行form的驗證
  
  但是代碼樹立了一個不好的榜樣即把大量的業務邏輯寫在了action中
  
  部分代碼如下
  
  登錄logonjsp
  
  <%@ page con_tentType=text/html;charset=UTF language=java %>
  
  // 聲明Taglib
  <%@ taglib uri=/WEBINF/strutsbeantld prefix=bean %>
  <%@ taglib uri=/WEBINF/strutshtmltld prefix=html %>
  
  <html:html locale=true
  <head>
  // bean是用來從ApplicationResource中讀取in信息
  <title><bean:message key=logontitle/></title>
  <html:base/>
  </head>
  <body bgcolor=white
  
  // 錯誤信息部分
  <html:errors/>
  
  // 登錄formaction為logiondo
  <html:form action=/logon focus=username
  on_submit=return validateLogonForm(this);
  <table border= width=%
  
  <tr>
  <th align=right
  <bean:message key=promptusername/>:
  </th>
  <td align=left
  <html:text property=username size= maxlength=/>
  </td>
  </tr>
  
  <tr>
  <th align=right
  <bean:message key=promptpassword bundle=alternate/>:
  </th>
  <td align=left
  <html:password property=password size= maxlength= redisplay=false/>
  </td>
  </tr>
  
  <tr>
  <td align=right
  <html:submit value=Submit/>
  </td>
  <td align=left
  <html:reset/>
  </td>
  </tr>
  
  </table>
  
  </html:form>
  
  // Validator插件用於form驗證
  <html:javascript formName=logonForm dynamicJavascript=true staticJavascript=false/>
  <script language=Javascript src=staticJavascriptjsp></script>
  
  </body>
  </html:html>
  
  strutsconfigxml配置
  
  <formbeans>
  
  <! Logon form bean 
  <formbean name=logonForm type=orgapachestrutsvalidatorDynaValidatorForm
  <formproperty name=username type=javalangString/>
  <formproperty name=password type=javalangString/>
  </formbean>
  
  <! Subscription form bean 
  <formbean name=subscriptionFormtype=orgapachestrutswebappexampleSubscriptionForm/>
  
  </formbeans>
  <actionmappings>
  
  <! Edit mail subscription 
  <action path=/editSubscription
  type=orgapachestrutswebappexampleEditSubscriptionAction
  attribute=subscriptionForm
  scope=request
  validate=false
  <forward name=failure path=/mainMenujsp/>
  <forward name=success path=/subscriptionjsp/>
  </action>
  
  
  subscriptionForm 是一個標准的ActionForm其中reset方法用於清除form的值validate方法用於驗證
  
  public final class SubscriptionForm extends ActionForm {
  // The maintenance action we are performing (Create or Edit)
  private String action = Create;
  // Should we autoconnect at startup time?
  private boolean autoConnect = false;
  // The host name
  private String host = null;
  private String password = null;
  private String type = null;
  private String username = null;
  
  public String getAction() { return (thisaction); }
  public void setAction(String action) { thisaction = action; }
  
  public boolean getAutoConnect() { return (thisautoConnect); }
  public void setAutoConnect(boolean autoConnect) { thisautoConnect = autoConnect; }
  
  public String getHost() { return (thishost); }
  public void setHost(String host) { thishost = host; }
  
  public String getPassword() { return (thispassword); }
  public void setPassword(String password) { thispassword = password; }
  
  public String getType() { return (thistype); }
  public void setType(String type) { thistype = type; }
  
  public String getUsername() { return (thisusername); }
  public void setUsername(String username) { thisusername = username; }
  
  /**
  * Reset all properties to their default values
  *
  * @param mapping The mapping used to select this instance
  * @param request The servlet request we are processing
  */
  public void reset(ActionMapping mapping HttpServletRequest request) {
  
  thisaction = Create;
  thisautoConnect = false;
  thishost = null;
  thispassword = null;
  thistype = null;
  thisusername = null;
  
  }
  
  
  /**
  * Validate the properties that have been set from this HTTP request
  * and return an <code>ActionErrors</code> object that encapsulates any
  * validation errors that have been found If no errors are found return
  * <code>null</code> or an <code>ActionErrors</code> object with no
  * recorded error messages
  *
  * @param mapping The mapping used to select this instance
  * @param request The servlet request we are processing
  */
  public ActionErrors validate(ActionMapping mapping
  HttpServletRequest request) {
  
  ActionErrors errors = new ActionErrors();
  
  if ((host == null) || (hostlength() < ))
  errorsadd(host
  new ActionError(errorhostrequired));
  if ((username == null) || (usernamelength() < ))
  errorsadd(username
  new
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28405.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.