由于開發任務緊張,因為這里的開發筆記,僅用于記錄遇到的幾個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的出錯.