Struts2、Spring和Hibernate應用實例(下)

Struts2SpringHibernate應用實例

七、 Web頁面

 

在這一節中,主要使用到了Struts2的標籤庫。在這裏,會對所用到的主要標籤做一個初步的介紹。更多的知識請讀者訪問Struts的官方網站做更多的學習。在編寫Web頁面之前,先從總體上,對Struts 1.xStruts 2.0的標誌庫(Tag Library)作比較。

 

 Struts 1.x

 Struts 2.0

 

分類

 將標誌庫按功能分成HTMLTilesLogicBean等幾部分

 嚴格上來說,沒有分類,所有標誌都在URI爲“/struts-tags”命名空間下,不過,我們可以從功能上將其分爲兩大類:非UI標誌和UI標誌

 

表達式語言(expression languages

 不支持嵌入語言(EL

 OGNLJSTLGroovyVelcity

 

1、主頁面:index.jsp,其代碼如下:

 

<%@pagepageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>

<%@taglib prefix="s" uri="/struts-tags" %>

<html>

<head>

<metahttp-equiv="Content-Type" content="text/html;charset=GBK"/>

<title>圖書管理系統</title>

</head>

<body>

<p><ahref="<s:url action="list" />">進入圖書管理系統</a></p>

</body>

</html>

 

WebRoot/index.jsp

 

要在JSP中使用Struts 2.0標誌,先要指明標誌的引入。通過在JSP的代碼的頂部加入以下代碼可以做到這點。<%@taglibprefix="s" uri="/struts-tags" %>

 

1<s:url>標籤:該標籤用於創建url,可以通過"param"標籤提供request參數。當includeParams的值時'all'或者'get', param標籤中定義的參數將有優先權,也就是說其會覆蓋其他同名參數的值。

 

2、列表頁面:list.jsp

 

<%@pagepageEncoding="gb2312" contentType="text/html;charset=UTF-8" %>

<%@taglib prefix="s" uri="/struts-tags" %>

 

<html>

<head><title>圖書管理系統</title></head>

    <style type="text/css">

        table {

            border: 1px solid black;

            border-collapse: collapse;

        }

       

        table thead tr th {

            border: 1px solid black;

            padding: 3px;

            background-color: #cccccc;

            background-color:expression(this.rowIndex % 2 == 0 ? "#FFFFFF" : "#EEEEEE");

        }

       

        table tbody tr td {

            border: 1px solid black;

            padding: 3px;

        }

        .trs{

            background-color: expression(this.rowIndex% 2 == 0 ? "#FFFFFF" : "#EEEEEE");

        }

    </style>

 

    <scriptlanguage="JavaScript">  

        function doSearch(){

           if(document.all.searchValue.value=="")

            {   

                alert("請輸入查詢關鍵字!");

            }else{

               window.location.href="bookAdmin/list.action?queryName="+document.all.searchName.value+"&&queryValue="+document.all.searchValue.value;

             }

        }

    </script>

<body>

 

<tablealign="center">

<tralign="center">

    <td>

        <selectname="searchName">

            <optionvalue="bookName">書名</option>

            <optionvalue="bookAuthor">作者</option>

            <optionvalue="bookPublish">出版社</option>

            <optionvalue="bookDate">出版日期</option>

            <optionvalue="bookIsbn">ISNB</option>

            <optionvalue="bookPage">頁數</option>

        </select>

        <input type="text"name="searchValue" value="" size="10"/>

        <input type="button"value="查詢"onClick="doSearch();">

    </td>

</tr>

<tralign="center">   

    <td>

        <a href="<s:urlaction="list" includeParams="none"/>">全部</a>

        <a href='<s:urlaction="edit" ></s:url>'>增加</a>

    </td>

</tr>

<tr>

<td>

<tablecellspacing="0">

    <thead>

    <tr>

        <th>書名</th>

        <th>作者</th>

        <th>出版社</th>

        <th>出版日期</th>

        <th>ISNB</th>

        <th>頁數</th>

        <th>價格</th>

        <th>內容提要</th>

        <th>刪除</th>

    </tr>

    </thead>

    <tbody>

    <s:iteratorvalue="availableItems">

        <tr>

            <td>

            <a href='<s:urlaction="edit" ><s:param name="bookId"value="bookId" /></s:url>'>

            <s:propertyvalue="bookName"/>

            </a>

            </td>

            <td><s:propertyvalue="bookAuthor"/></td>

            <td><s:propertyvalue="bookPublish"/></td>

            <td><s:textname="format.date"><s:paramvalue="bookDate"/></s:text></td>    

            <td><s:propertyvalue="bookIsbn" /></td>

            <td><s:propertyvalue="bookPage" /></td>

            <td><s:propertyvalue="bookPrice"/></td>

            <td><s:propertyvalue="bookContent"/></td>

           

            <td><a href='<s:urlaction="delete"><s:param name="bookId"value="bookId" /></s:url>'>刪除</a></td>

        </tr>

    </s:iterator>

    <tr>

        <td colspan="9">

            <s:property value="totalRows"/>&nbsp;

            <s:property value="currentPage"/>&nbsp;

            <s:propertyvalue="pager.getTotalPages()"/>&nbsp;

            <a href="<s:urlvalue="list.action">

                <s:paramname="currentPage" value="currentPage"/>

                <s:paramname="pagerMethod" value="'first'"/>

               

            </s:url>">首頁</a>

            <a href="<s:urlvalue="list.action">

                <s:paramname="currentPage" value="currentPage"/>

                <s:paramname="pagerMethod" value="'previous'"/>

            </s:url>">上一頁</a>

            <a href="<s:urlvalue="list.action">

                <s:paramname="currentPage" value="currentPage"/>

                <s:paramname="pagerMethod" value="'next'"/>

            </s:url>">下一頁</a>

            <a href="<s:urlvalue="list.action">

                <s:paramname="currentPage" value="currentPage"/>

                <s:paramname="pagerMethod" value="'last'"/>

            </s:url>">尾頁</a>

        </td>

    </tr>   

    </tbody>

</table>

</td>

</tr>

</table>

</body>

</html>

 

/WebRoot/list.jsp

 

(1)<s:property> :得到'value'的屬性,如果value沒提供,默認爲堆棧頂端的元素。其相關的參數及使用如下表所示:

 

名稱

 必需

 默認

 類型

 描述

 

default

 

  String

 如果屬性是null則顯示的default

 

escape

 

 true

 Booelean

 是否escape HTML

 

value

 

 棧頂

 Object

 要顯示的值

 

id

 

  Object/String

 用來標識元素的id。在UI和表單中爲HTMLid屬性

 

 

(2)<s:Iterator>:用於遍歷集合(java.util.Collection)或枚舉值(java.util.Iterator)。其相關的參數及使用如下表所示: 

 

名稱

 必需

 默認

 類型

 描述

 

status

 

  String

 如果設置此參數,一個IteratorStatus的實例將會壓入每個遍歷的堆棧

 

value

 

  Object/String

 要遍歷的可枚舉的(iteratable)數據源,或者將放入新列表(List)的對象

 

id

 

  Object/String

 用來標識元素的id。在UI和表單中爲HTMLid屬性

 

 

(3)<s:param>:爲其他標籤提供參數,比如include標籤和bean標籤. 參數的name屬性是可選的,如果提供,會調用Component的方法addParameter(String, Object),如果不提供,則外層嵌套標籤必須實現UnnamedParametric接口(TextTag) value的提供有兩種方式,通過value屬性或者標籤中間的text,不同之處我們看一下例子:

 

<paramname="color">blue</param><!-- (A) -->

 

<paramname="color" value="blue"/><!-- (B) -->

(A)參數值會以String的格式放入statck.

(B)該值會以java.lang.Object的格式放入statck.

 

其相關的參數及使用如下表所示:

 

名稱

 必需

 默認

 類型

 描述

 

name

 

  String

 參數名

 

value

 

  String

 value表達式

 

id

 

  Object/String

 用來標識元素的id。在UI和表單中爲HTMLid屬性

 

 

4)、國際化是商業系統中不可或缺的一部分,所以無論您學習的是什麼Web框架,它都是必須掌握的技能。其實,Struts 1.x在此部分已經做得相當不錯了。它極大地簡化了我們程序員在做國際化時所需的工作,例如,如果您要輸出一條國際化的信息,只需在代碼包中加入FILE-NAME_xx_XX.properties(其中FILE-NAME爲默認資源文件的文件名),然後在struts-config.xml中指明其路徑,再在頁面用<bean:message>標誌輸出即可。

 

不過,所謂“沒有最好,只有更好”。Struts 2.0並沒有在這部分止步,而是在原有的簡單易用的基礎上,將其做得更靈活、更強大。

 

5)、list.jsp文件中:

 

<s:textname="format.date"><s:paramvalue="bookDate"/></s:text>,爲了正確的輸出出版日期的格式,採用在資源文件中定義輸出的格式,並在頁面上調用。format.date就是在資源文件com.sterning.books.web.actions.BooksAction.properties中定義。當然也可以別的文件,放在別的路徑下,但此時需要在web.xml中註冊纔可以使用它。

 

正如讀者所見,在pojo(本例爲Books.java)中將日期字段設置爲java.util.Date,在映射文件中(books.hbm.xml)設置爲timestamp(包括日期和時間)。爲了便於管理,將日期格式保存在國際化資源文件中。如:globalMessagesglobalMessages_zh_CN文件。

 

其內容爲:

 

format.date={0,date,yyyy-MM-dd}

 

在頁面顯示日期時間時:<s:textname="format.date"><s:paramvalue="bookDate"/></s:text>。這樣就解決了日期(時間)的顯示格式化問題。

 

3、增加/修改頁面:editBook.jsp 

 

<%@pagepageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>

<%@taglib prefix="s" uri="/struts-tags" %>

 

<html>

<head>

    <title>編輯圖書</title>

    <s:head/>

</head>

<body>

    <h2>

        <s:if test="null ==book">

            增加圖書

        </s:if>

        <s:else>

            編輯圖書

        </s:else>

    </h2>

    <s:form name="editForm"action="save" validate="true">

   

         <s:textfield label="書名"name="book.bookName"/>

         <s:textfield label="作者" name="book.bookAuthor"/>

         <s:textfield label="出版社"name="book.bookPublish"/>

         <s:datetimepicker label="出版日期"name="book.bookDate"></s:datetimepicker>

         <s:textfield label="ISBN"name="book.bookIsbn"/>

         <s:textfield label="頁數" name="book.bookPage"/>

         <s:textfield label="價格()"name="book.bookPrice"/>

         <s:textfield label="內容摘要"name="book.bookContent"/>

         <s:if test="null ==book">

             <s:hiddenname="book.bookId" value="%{bookId}"/>

         </s:if>         

         <s:else>

             <s:hiddenname="book.bookId" />

         </s:else>

         <s:hiddenname="queryName" />

         <s:hiddenname="queryValue" />

         <s:submit value="%{getText('保存')}" />

    </s:form>

 

<p><ahref="<s:url action="list"/>">返回</a></p>

</body>

</html>

 

 

WebRoot/editBook.jsp

 

1)、<s:if><s:elseif><s:else> :執行基本的條件流轉。其相關的參數及使用如下表所示:

 

名稱

 必需

 默認

 類型

 描述

 備註

 

test

 

 

 Boolean

 決定標誌裏內容是否顯示的表達式

 else標誌沒有這個參數

 

id

 

 

 Object/String

 用來標識元素的id。在UI和表單中爲HTMLid屬性

 

 

2)、<s:text>:支持國際化信息的標籤。國際化信息必須放在一個和當前action同名的resource bundle,如果沒有找到相應message,tag body將被當作默認message,如果沒有tag body,messagename會被作爲默認message其相關的參數及使用如下表所示:

 

名稱

 必需

 默認

 類型

 描述

 

name

 

 

 String

 資源屬性的名字

 

id

 

 

 Object/String

 用來標識元素的id。在UI和表單中爲HTMLid屬性

 

八、  配置Struts2

 

 

Struts的配置文件都會在web.xml中註冊的。

 

a)   Struts的配置文件如下:

 

<?xmlversion="1.0" encoding="UTF-8" ?>

<!DOCTYPEstruts PUBLIC

    "-//Apache Software Foundation//DTDStruts Configuration 2.0//EN"

   "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

 

    <constantname="struts.enable.DynamicMethodInvocation" value="false"/>

    <constantname="struts.devMode" value="true" />

    <constantname="struts.i18n.encoding" value="GBK" />  

 

    <!-- Add packages here -->

 

</struts>

 

Src/struts.xml

 

b)   struts_book.xml配置文件如下:

 

 

<?xmlversion="1.0" encoding="UTF-8" ?>

<!DOCTYPEstruts PUBLIC

        "-//Apache SoftwareFoundation//DTD Struts Configuration 2.0//EN"

       "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

 

    <package name="products"extends="struts-default">

        <!--default-interceptor-refname="validation"/-->

         <!-- Add actions here -->

        <action name="list"class="bookAction" method="list">           

           <result>/list.jsp</result>

        </action>

 

    <action name="delete"class="bookAction" method="delete">           

            <resulttype="redirect">list.action?queryMap=${queryMap}</result>

        </action>

 

        <action name="*"class="com.sterning.commons.AbstractAction">

            <result>/{1}.jsp</result>

        </action>

       

    <action name="edit"class="bookAction" method="load">

           <result>/editBook.jsp</result>

        </action>

      

       <action name="save"class="bookAction" method="save">

           <interceptor-refname="params"/>

           <interceptor-refname="validation"/>

            <resultname="input">/editBook.jsp</result>

            <resulttype="redirect">list.action?queryMap=${queryMap}</result>

             

        </action>

    </package>

</struts>

文件中的<interceptor-refname="params"/>,使用了struts2自己的攔截器,攔截器在AOPAspect-Oriented Programming)中用於在某個方法或字段被訪問之前,進行攔截然後在之前或之後加入某些操作。攔截是AOP的一種實現策略。

 

Struts2已經提供了豐富多樣的,功能齊全的攔截器實現。大家可以到struts2-all-2.0.6.jarstruts2-core-2.0.6.jar包的struts-default.xml查看關於默認的攔截器與攔截器鏈的配置。

 

struts-default.xml中已經配置了大量的攔截器。如果您想要使用這些已有的攔截器,只需要在應用程序struts.xml文件中通過“<includefile="struts-default.xml" />”將struts-default.xml文件包含進來,並繼承其中的struts-default包(package),最後在定義Action時,使用“<interceptor-refname="xx" />”引用攔截器或攔截器棧(interceptor stack)。一旦您繼承了struts-default包(package),所有Action都會調用攔截器棧——defaultStack。當然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆蓋defaultStack

 

作爲“框架(framework)”,可擴展性是不可或缺的,因爲世上沒有放之四海而皆準的東西。雖然,Struts 2爲我們提供如此豐富的攔截器實現,但是這並不意味我們失去創建自定義攔截器的能力,恰恰相反,在Struts 2自定義攔截器是相當容易的一件事。所有的Struts 2的攔截器都直接或間接實現接口com.opensymphony.xwork2.interceptor.Interceptor。除此之外,大家可能更喜歡繼承類com.opensymphony.xwork2.interceptor.AbstractInterceptor

 

九、配置Spring

 

 

1Spring的配置文件如下:

 

<?xmlversion="1.0" encoding="UTF-8"?>

<!DOCTYPEbeans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">

 

<beans>

    <!-- dataSource config -->

    <bean id ="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

        <property name="driverClassName"value="com.mysql.jdbc.Driver" />

        <property name="url"value="jdbc:mysql://localhost:3306/game" />

        <property name="username"value="root" />

        <property name="password"value="root"/>

    </bean>

   

    <!-- SessionFactory -->

    <bean id="sessionFactory"

       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

 

        <propertyname="dataSource">

            <refbean="dataSource"/>

        </property>

        <propertyname="configLocation">

            <value>classpath:com\sterning\bean\hibernate\hibernate.cfg.xml</value>

        </property>       

    </bean>

   

    <!-- TransactionManager  不過這裏暫時沒注入-->

    <bean id="transactionManager"

       class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <propertyname="sessionFactory">

            <reflocal="sessionFactory"/>

        </property>

    </bean>

   

    <!-- DAO -->

    <bean id="booksDao"class="com.sterning.books.dao.hibernate.BooksMapDao">

        <property name="sessionFactory">

            <refbean="sessionFactory"/>

        </property>

    </bean>

   

    <!-- Services -->

    <bean id="booksService"class="com.sterning.books.services.BooksService">

        <propertyname="booksDao">

            <ref bean="booksDao"/>

        </property>

    </bean>

   

    <bean id="pagerService"class="com.sterning.commons.PagerService"/>

   

    <!-- view -->

    <bean id="bookAction"class="com.sterning.books.web.actions.BooksAction"singleton="false">

        <property name="booksService">

            <refbean="booksService"/>

        </property>

        <propertyname="pagerService">

            <refbean="pagerService"/>

        </property>

    </bean> 

   

</beans>

 

 WebRoot/WEB-INF/srping-content/applicationContent.xml

2Struts.properties.xml

 

本來此文件應該寫在struts 配置一節,但主要是考慮這體現了集成spring的配置,所以放在spring的配置這裏來講。

 

struts.objectFactory= spring 

struts.locale=zh_CN

struts.i18n.encoding= GBK

struts.objectFactoObjectFactory 實現了com.opensymphony.xwork2.ObjectFactory接口(spring)。struts.objectFactory=spring,主要是告知Struts 2運行時使用Spring來創建對象(如Action等)。當然,SpringContextLoaderListener監聽器,會在web.xml文件中編寫,負責SpringWeb容器交互。

struts.localeThe default locale for theStruts application默認的國際化地區信息。

struts.i18n.encoding:國際化信息內碼。

 

十、Web.xml配置

 

 

<?xmlversion="1.0" encoding="GB2312"?>

<!DOCTYPEweb-app

    PUBLIC "-//Sun Microsystems, Inc.//DTDWeb Application 2.3//EN"

   "http://java.sun.com/dtd/web-app_2_3.dtd">

 

<web-app>

    <display-name>圖書管理系統</display-name>

    <context-param>

        <param-name>log4jConfigLocation</param-name>

       <param-value>/WEB-INF/classes/log4j.properties</param-value>

    </context-param>

    <!-- ContextConfigLocation -->

    <context-param>

       <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/spring-context/applicationContext.xml</param-value>

      </context-param>

   

    <filter>

       <filter-name>encodingFilter</filter-name>

       <filter-class>com.sterning.commons.SetCharacterEncodingFilter</filter-class>

        <init-param>

           <param-name>encoding</param-name>

           <param-value>UTF-8</param-value>

        </init-param>

        <init-param>

           <param-name>forceEncoding</param-name>

           <param-value>true</param-value>

        </init-param>

    </filter>

     <filter>

       <filter-name>struts2</filter-name>

       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

        <init-param>

           <param-name>config</param-name>

            <param-value>struts-default.xml,struts-plugin.xml,struts.xml,struts_books.xml</param-value>

        </init-param>

    </filter>   

 

    <filter-mapping>

       <filter-name>encodingFilter</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

    <filter-mapping>

       <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>       

   

    <!-- Listener contextConfigLocation-->

      <listener>

       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

      </listener>

    <!-- Listener log4jConfigLocation -->

      <listener>

       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

      </listener>

 

    <!-- The Welcome File List -->

    <welcome-file-list>

       <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

</web-app>

 

 

publicvoid setQueryName(String queryName) {

this.queryName= queryName;

}

publicString getQueryValue() {

returnqueryValue;

}

publicvoid setQueryValue(String queryValue) {

this.queryValue= queryValue;

}

publicString getSearchName() {

returnsearchName;

}

publicvoid setSearchName(String searchName) {

this.searchName= searchName;

}

publicString getSearchValue() {

returnsearchValue;

}

publicvoid setSearchValue(String searchValue) {

this.searchValue= searchValue;

}

publicString getQueryMap() {

returnqueryMap;

}

publicvoid setQueryMap(String queryMap) {

this.queryMap= queryMap;

}

publicPagerService getPagerService() {

returnpagerService;

}

publicvoid setPagerService(PagerService pagerService) {

this.pagerService= pagerService;

}

}

 

com.sterning.books.web.actions.BookAction.java

 

 

1)、默認情況下,當請求bookAction.action發生時(這個會在後面的Spring配置文件中見到的)Struts運行時(Runtime)根據struts.xml裏的Action映射集(Mapping),實例化com.sterning.books.web.actions.BookAction類,並調用其execute方法。當然,我們可以通過以下兩種方法改變這種默認調用。這個功能(Feature)有點類似Struts 1.x中的LookupDispathAction

 

classes/sturts.xml中新建Action,並指明其調用的方法;

訪問Action時,在Action名後加上“!xxx”(xxx爲方法名)。

 

2)、細心的朋友應該可能會發現com.sterning.books.web.actions.BookAction.javaAction方法(execute)返回都是SUCCESS。這個屬性變量我並沒有定義,所以大家應該會猜到它在ActionSupport或其父類中定義。沒錯,SUCCESS在接口com.opensymphony.xwork2.Action中定義,另外同時定義的還有ERROR, INPUT, LOGIN, NONE

 

此外,我在配置Action時都沒有爲result定義名字(name),所以它們默認都爲success。值得一提的是Struts 2.0中的result不僅僅是Struts 1.xforward的別名,它可以實現除forward外的很激動人心的功能,如將Action輸出到FreeMaker模板、Velocity模板、JasperReports和使用XSL轉換等。這些都過result裏的type(類型)屬性(Attribute)定義的。另外,您還可以自定義result類型。

 

3)、使用Struts 2.0,表單數據的輸入將變得非常方便,和普通的POJO一樣在Action編寫GetterSetter,然後在JSPUI標誌的name與其對應,在提交表單到Action時,我們就可以取得其值。

 

4)、Struts 2.0更厲害的是支持更高級的POJO訪問,如this.getBook().getBookPrice()private Books book所引用的是一個關於書的對象類,它可以做爲一個屬性而出現在BookActoin.java類中。這樣對我們開發多層系統尤其有用。它可以使系統結構更清晰。

5)、有朋友可能會這樣問:“如果我要取得Servlet API中的一些對象,如requestresponsesession等,應該怎麼做?這裏的execute不像Struts 1.x的那樣在參數中引入。”開發Web應用程序當然免不了跟這些對象打交道。在Strutx 2.0中可以有兩種方式獲得這些對象:非IoC(控制反轉Inversion of Control)方式和IoC方式。

點擊這裏:Struts2、Spring和Hibernate應用實例(上)

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