<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 64,  comments - 9,  trackbacks - 0
    Struts2 + Hibernate開發筆記(一)

    由于開發任務緊張,因為這里的開發筆記,僅用于記錄遇到的幾個struts2和hibernate結合開發的現象.不對其做分析.
    1.        在使用struts2時,頁面和action之間的傳值
    這是struts2和struts1最大的區別.
    Struts2中,action和jsp頁面之間的信息交互,是通過 action中定義的成員變量來實現的.
    例如,我在一個名為EstateAction的類中有如下定義
    public class CityAction extends BaseAction {
            private MthCity mthCity ;
            private String cityName;
            private Long cityId
            private int couter;

            public String loadCity() throws DataAccessException, BaseException{
            counter ++;
                    return "city";
            }
    }
    然后,這里上面的類中的成員類MthCity的定義如下
    public class MthCity implements java.io.Serializable {
           
            private Long cityId       
            private String cityName;
            public MthCity() {
            public Long getCityId() {
                    return this.cityId;
            }
            public void setCityId(Long cityId) {
                    this.cityId = cityId;
            public String getCityName() {
                    return this.cityName;
            }
            public void setCityName(String cityName) {
                            this.cityName = cityName;
            }
    }
    這是一個Hibatenate使用的數據對象 POJO類.
    有了這兩個類后,我們來看看Struts2的Action和JSP頁面之間是如何交互的
    一.        JSP -> Action
    Jsp頁面
    ?        以下是一個jsp頁面submit.jsp.這個頁面只有一個功能,就是向struts提交申請
    <%@ page language="java" contentType="text/html; charset=gbk"%>
    <%@ include file="/common/taglibs.jsp"%>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <title>提交</title>
    </head>
    <script>
        function go (){
          window.location ="${pageContext.request.contextPath}/admin/city/loadCity.do”;
      }
    </script>
    <body>
    <form name=”myform” >
      <input type="button" name="cityupdate" id="cityupdate" value="編輯" onclick="javascript:go();"/>
      <input type="hidden" name="mthCity.cityName" id=" mthCity " value="廣州" />

    </form>
    </body>
    </html>

    大家可以看到,這個頁面只有一個按鈕,將頁面提交到struts的一個action中,這是為什么呢.
    ?        我們先看一段struts2的配置文件admin-action.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
            <package name="admin" namespace="/admin" extends="struts-default">
                    <action name="city/*" method="{1}"
                    class="com.mytophome.admin.representation.struts.action.CityAction">
                    <result name="city">/admin/city.jsp</result>
                    <result name="city_update">/admin/city_update.jsp</result>
                    </action>
            </package>
    </struts>

    這是一個struts2的典型配置文件.
    上面有幾處要注意的
    首先是namespace = “/admin” 這是一個struts模塊名,可以不寫,但如果寫了,能比較方便的將struts2的action按配置來分模塊.(何謂分模塊呢?struts2有個特性,是action定義不需要像struts1一樣寫在同一個struts.xml文件中.而是可以使用include的形式.例如我使用的項目的struts.xml文件就是這樣寫的:
    <struts>
        <include file="struts-action/admin-action.xml"/>
            <include file="struts-action/agent-action.xml"/>
    </struts>
    這樣include了一系統的xml配置,而上面的admin-action.xml文件就是其中一段,因此將這一段中涉及的action類設定為一個模塊,就定namespace = “/admin”
    )
    其次
    <action name="city/*" method="{1}"
    這一句配置的意思,就是,當用戶提交一個符合struts2格式的申請時(所有包含.do形式的http鏈接)
    例如http://localhost/admin/city/loadCity.do
    其中包含了/city/那么在配置 文件中,只要定義action name=”city/*”,那么所有包含有/city/的do,都會提交到action定義的類中來,也就是類om.mytophome.admin.representation.struts.action.CityAction中,那么提交到這個類的哪個方法中呢? 因為選擇的是city/*.而且mothed={1},所以方法名由鏈接指定
    也就是loadCity.do所指定的.loadCity方法.

    ?        這個do方法后面是可以帶參數的.所帶的參數名,要是CityAction中定義的成員變量,包括成員類.例如,如果想提交后,CityAction中的cityId有值,鏈接可以這樣寫
    http://localhost/admin/city/loadCity.do?cityId=9
    這樣,在loadCity方法中,如果你訪問cityId,就可以發現cityId的值是9
    System.out.println(Long.toString(cityId));

    但這里有一個條件,就是CityAction中,必須要有cityId變量的getter/setter方法(這兩個方法可以用MyEclipse自動生成)
            public Long getCityId() {
                    return cityId;
            }

            public void setCityId(Long cityId) {
                    this.cityId = cityId;
            }如果要給CityAction中的MthCity類的
    這樣才能在jsp頁面提交時,由struts為cityId賦值.(當然,getter方法就方便當action返回到jsp頁面時,cityId的值能在頁面取到.)
    ?        如果要為action中的類成員變量賦值也是可以的
    例如http://localhost/admin/city/load ... mp;mthCity.cityId=8
    這條鏈接提交后,會和上面一樣調用CityAction的loadCity方法,但這里,action的成員mthCity會被創建實例,并為mthCity的一個屬性cityId賦值,也就是說,在action中,大家可以通過mthCity.getCityId()來獲得這個屬性的值.
    當然,一定要在action設置mthCity的getter setter,jsp上的內容才能傳到action中
            public MthCity getMthCity()
                                    return mthCity;
            }

            public void setMthCity(MthCity city) {
                    this. mthCity = city;
            }


    ?        從JSP提交內容到Action,還有一種方法,就是將參數內容放在表單form的元素中
      <input type="hidden" name="mthCity.cityName" id=" mthCity " value="廣州" />

    這樣,當用以下語句表單提交的時候
    doucment. myform.submit();
    就能在Action中創建一個mthCity實例,并為mthCity.cityName設置值為:廣州.
    原因是在頁面的表單元素中,設置了name= mthCity.cityName,而action中剛好有成員類叫mthCity,而這個類中剛好有屬性叫cityName.,就是通過這樣的方法,能將表單的內容,提交到Action中.


    Struts2 + Hibernate開發筆記(二)

    二.        Action -> JSP
    當要從Action中執行的loadCity方法,要返回到jsp頁面時,要在頁面上指定一個return的頁面.我們在admin-action.xml配置中可以看到一句
    <result name="city">/admin/city.jsp</result>
    而在loadCity方法中有這樣一句
    return "city";
    這樣,當loadCity執行完后,就會返回到http://localhost/admin/city.jsp頁面.
    由于在action中,cityId和mthCity.cityId是被前一個提交過來的jsp頁面賦值過,所以當程序執行轉到city.jsp頁面的時候,這幾個值是能被使用的.我們使用jstl來獲得這些值
    ${cityId} , ${mthCity.citId} (前提是只要在action中,設定了cityId和mthCity類的getter/setter.)


    2.        當action中的類,傳到受hibernate管理的命名空間的類中時
    這個問題比較特殊.
    我的工程中,建立了一個類cityService.這個類是進行數據庫操作的,也就是和hibernate打交道的.而這個類在hibernate的設置中設置為被一個hibernate Session管理的范圍.
    這個cityService中有一個方法,用于更新city的信息的
            public void updateCity(MthCity city) throws DataAccessException,
            BaseException{

                MthCity icity = this.getCityById( city.getCityId());
                icity.setCityName(city.getCityName());
    baseHibernateDAO.update(icity);//這個baseHibernateDAO只是一個封封裝了hibernate  API的包,網上常見.       

            }
    大家可以看到,方法中并沒有直接update從參數傳進來的city,而是新建了一個icity,裝了city中的信息再進行update.

    這是因為,cityService這個類被hibernate管理,所以在這個類中創建的內容,才能被更新.所以我們必須使用一個新的MthCity類實例來裝著外面傳進來的內容,才能更新.否則就會出現類似have the same id object in the session的錯誤,也就是說session中有其它相同id的對象的錯.

    當然,有另一個處理辦法,就是使用baseHibernateDAO.merge來更新內容,而還是用update
    這里就可以寫成
    public void updateCity(MthCity city) throws DataAccessException,
            BaseException{

    baseHibernateDAO.merge(icity);
    }


    3.        Hibernate中的version類型成員
    在我的項目中,city有一個屬性是timestamp,對應的是mth_city這個表,這個表通過hibernate的映射,映射成一個POJO對象
    public class MthCity implements java.io.Serializable {
            private Long cityId
            private Date timestamp;
           
            private String cityName;
            public MthCity() {
            }

            public Long getCityId() {
                    return this.cityId;
            }
            public void setCityId(Long cityId) {
                    this.cityId = cityId;
            }

            public Date getTimestamp() {
                    return this.timestamp;
            }
            public void setTimestamp(Date timestamp) {
                    this.timestamp = timestamp;
            }
            public String getCityName() {
                    return this.cityName;
            }
            public void setCityName(String cityName) {
                            this.cityName = cityName;
            }
    }

    大家可以看到,類中有一個屬性是timestamp,定義為Date類型.
    這個類型是用于記錄數據庫操作的日期的,數據庫中的對應字段也叫timestamp.看一下hibernate的映射配置
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!--
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="com.mytophome.admin.domain.MthCity" table="MTH_CITY" dynamic-update="true">
            <id name="cityId" type="java.lang.Long">
                <column name="CITY_ID" precision="10" scale="0" />
                <generator class="sequence" />
            </id>
            <version name="timestamp" type="java.util.Date">
                <column name="TIMESTAMP" length="7" />
            </version>
            <property name="cityName" type="java.lang.String">
                <column name="CITY_NAME" length="80" />
            </property>
        </class>
    </hibernate-mapping>

    大家可以看到,cityid對應數據庫表中的CITY_ID字段,其值由oracle sequence生成
    而timestamp屬性,對應的是數據庫的TIMESTAMP字段,并且這個屬性在mapping中定義為<version>屬性
    這樣,就會出現兩點要注意的地方
    ?        數據庫中的這個timestamp的字段一定要有值,并且是日期值,否則當hibernate更新這個字段沒有值的那條記錄時,會出現如下錯誤
    org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.mytophome.admin.domain.MthCity#1]
    ?        在更新MthCity中的值到數據庫中,也就是更新一條記錄時,一定不設調用方法人工設置timestamp屬性的值,也就是下面的語句不能出現
    mthCity.setTimeStamp(new Date());
    否則也會引起hibernate的出錯.

    posted on 2009-10-13 11:29 super_nini 閱讀(656) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2009年10月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    相冊

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: www一区二区www免费| 久久国产成人亚洲精品影院| 久久不见久久见中文字幕免费 | 亚洲一卡2卡三卡4卡无卡下载| 亚洲av无码一区二区三区四区| 日韩精品无码免费视频| 国产免费一区二区三区在线观看| 91香蕉在线观看免费高清| 女性无套免费网站在线看| 中文字幕亚洲电影| 亚洲天堂免费在线| 91av免费在线视频| 国产精品久久久久久久久免费| 免费v片在线观看品善网| 久久久久亚洲AV无码麻豆| 亚洲AV无码AV日韩AV网站| 免费视频成人手机在线观看网址| 在线观看免费污视频| 亚洲AV成人片色在线观看| 国产精品久久久久久亚洲影视 | 亚洲中文字幕无码一区| 亚洲一级黄色大片| 二区久久国产乱子伦免费精品| 日本一区二区三区免费高清| 国产精品亚洲成在人线| 久久亚洲精品无码gv| 亚洲视频免费观看| 狠狠亚洲婷婷综合色香五月排名| 自拍偷区亚洲国内自拍| a国产成人免费视频| 国产免费毛不卡片| 亚洲电影中文字幕| 国产免费黄色无码视频| 国产美女无遮挡免费网站| 久久精品国产亚洲AV无码麻豆 | 99爱在线精品视频免费观看9| 国产jizzjizz免费视频| 亚洲日本国产综合高清| 四虎国产成人永久精品免费 | 久久爰www免费人成| 免费人成视频在线观看不卡|