jpetstore 中的struts解析

來源:互聯網
上載者:User

 

Struts總結

2011.11.6

Jpetstore的實現主要用了兩個重要的東西,一個是struts,另一個是ibatis。

首先總結一個struts。據我瞭解,struts其實就是在MVC模式的基礎上進行了完善的函數封裝。使得我們更加高效的進行Web應用開發。因此,struts就是物件導向的設計,也就是一個個功能齊全的類。所以,使用很方便。

struts能夠很好的分離出商務邏輯層,使得jsp與交易處理之間交叉更少,使得邏輯流程更加清晰明了。同時struts很好的實現了請求的過濾和請求的跳轉。

MVC設計模式

M: Model(Business process layer),模型,操作資料的業務處理層,並獨立於表現層V,主要設計資料庫的各種操作。

V: View(Presentation layer),視圖,就是使用者互動介面,在VC之間進行資料的互動。

C: Controller(Control layer),控制器,也就是視圖層和模型層橋樑,控制資料的流向,接受視圖層發出的事件,並重繪視圖。

Struts 提供的API

M:由於不同的web開發,會有不同商務邏輯,因此就沒有提供相應的類。因此ibatis的引進完善這個體系,使得資料的儲存變得更加靈活。

V:Struts提供了action form建立form bean, 用於在controller和view間傳輸資料。

C:Struts提供了一個核心的控制器ActionServlet,通過這個核心的控制器來調用其他使用者註冊了的自訂的控制器Action,自訂Action需要符合Struts的自訂Action規範,還需要在struts-config.xml的特定設定檔中進行配置,接收JSP輸入欄位形成Action form,然後調用一個Action控制器。Action控制器中提供了model的邏輯介面。

對比jpetstore上的代碼(如登陸驗證操作):

signonForm.jsp

  <html:form action="/shop/signon" method="POST">

    <p>Please enter your username and password.</p>

    <p>

      Username:<input type="text" name="username" value="j2ee"/>

      <br/>

      Password:<input type="password" name="password" value="j2ee"/>

    </p>

    <input type="submit" name="submit" value="Login"/>

  </html:form>

Struts-config.xml

 <form-bean name="accountBean"

 type="com.ibatis.jpetstore.presentation.AccountBean"/>

...

<action path="/shop/signon"

type="org.apache.struts.beanaction.BeanAction"

            name="accountBean" scope="session"

            validate="false">

      <forward name="success" path="/shop/index.shtml"/>

</action>

Web.xml

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

    <init-param>

      <param-name>debug</param-name>

      <param-value>2</param-value>

    </init-param>

    <init-param>

      <param-name>detail</param-name>

      <param-value>2</param-value>

    </init-param>

    <load-on-startup>2</load-on-startup>

  </servlet>

AccountBean.java

 public String signon() {

    account = accountService.getAccount(account.getUsername(), account.getPassword());

    if (account == null || account == null) {

      String value = "Invalid username or password.  Signon failed.";

      setMessage(value);

      clear();

      return FAILURE;

    } else {

      account.setPassword(null);

      myList = catalogService.getProductListByCategory (account.getFavouriteCategoryId());

      authenticated = true;

      return SUCCESS;

    }

  }

總的來說,struts就是一個輔助的開發套件,能夠以MVC架構來輔助開發人員實現便捷的開發過程,使得我們只關注商務邏輯,而不需要去關注C層的實現過程。因為struts已經把控制層完善得很好了。我們只需要把重點放到商務邏輯上,也就是M,資料層次邏輯。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.