由于開發(fā)任務(wù)緊張,因?yàn)檫@里的開發(fā)筆記,僅用于記錄遇到的幾個(gè)struts2和hibernate結(jié)合開發(fā)的現(xiàn)象.不對(duì)其做分析.
1. 在使用struts2時(shí),頁(yè)面和action之間的傳值
這是struts2和struts1最大的區(qū)別.
Struts2中,action和jsp頁(yè)面之間的信息交互,是通過(guò) action中定義的成員變量來(lái)實(shí)現(xiàn)的.
例如,我在一個(gè)名為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;
}
}
這是一個(gè)Hibatenate使用的數(shù)據(jù)對(duì)象 POJO類.
有了這兩個(gè)類后,我們來(lái)看看Struts2的Action和JSP頁(yè)面之間是如何交互的
一. JSP -> Action
Jsp頁(yè)面
? 以下是一個(gè)jsp頁(yè)面submit.jsp.這個(gè)頁(yè)面只有一個(gè)功能,就是向struts提交申請(qǐng)
<%@ 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>
大家可以看到,這個(gè)頁(yè)面只有一個(gè)按鈕,將頁(yè)面提交到struts的一個(gè)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>
這是一個(gè)struts2的典型配置文件.
上面有幾處要注意的
首先是namespace = “/admin” 這是一個(gè)struts模塊名,可以不寫,但如果寫了,能比較方便的將struts2的action按配置來(lái)分模塊.(何謂分模塊呢?struts2有個(gè)特性,是action定義不需要像struts1一樣寫在同一個(gè)struts.xml文件中.而是可以使用include的形式.例如我使用的項(xiàng)目的struts.xml文件就是這樣寫的:
<struts>
<include file="struts-action/admin-action.xml"/>
<include file="struts-action/agent-action.xml"/>
</struts>
這樣include了一系統(tǒng)的xml配置,而上面的admin-action.xml文件就是其中一段,因此將這一段中涉及的action類設(shè)定為一個(gè)模塊,就定namespace = “/admin”
)
其次
<action name="city/*" method="{1}"
這一句配置的意思,就是,當(dāng)用戶提交一個(gè)符合struts2格式的申請(qǐng)時(shí)(所有包含.do形式的http鏈接)
例如
http://localhost/admin/city/loadCity.do
其中包含了/city/那么在配置 文件中,只要定義action name=”city/*”,那么所有包含有/city/的do,都會(huì)提交到action定義的類中來(lái),也就是類om.mytophome.admin.representation.struts.action.CityAction中,那么提交到這個(gè)類的哪個(gè)方法中呢? 因?yàn)檫x擇的是city/*.而且mothed={1},所以方法名由鏈接指定
也就是loadCity.do所指定的.loadCity方法.
? 這個(gè)do方法后面是可以帶參數(shù)的.所帶的參數(shù)名,要是CityAction中定義的成員變量,包括成員類.例如,如果想提交后,CityAction中的cityId有值,鏈接可以這樣寫
http://localhost/admin/city/loadCity.do?cityId=9
這樣,在loadCity方法中,如果你訪問(wèn)cityId,就可以發(fā)現(xiàn)cityId的值是9
System.out.println(Long.toString(cityId));
但這里有一個(gè)條件,就是CityAction中,必須要有cityId變量的getter/setter方法(這兩個(gè)方法可以用MyEclipse自動(dòng)生成)
public Long getCityId() {
return cityId;
}
public void setCityId(Long cityId) {
this.cityId = cityId;
}如果要給CityAction中的MthCity類的
這樣才能在jsp頁(yè)面提交時(shí),由struts為cityId賦值.(當(dāng)然,getter方法就方便當(dāng)action返回到j(luò)sp頁(yè)面時(shí),cityId的值能在頁(yè)面取到.)
? 如果要為action中的類成員變量賦值也是可以的
例如
http://localhost/admin/city/load ... mp;mthCity.cityId=8
這條鏈接提交后,會(huì)和上面一樣調(diào)用CityAction的loadCity方法,但這里,action的成員mthCity會(huì)被創(chuàng)建實(shí)例,并為mthCity的一個(gè)屬性cityId賦值,也就是說(shuō),在action中,大家可以通過(guò)mthCity.getCityId()來(lái)獲得這個(gè)屬性的值.
當(dāng)然,一定要在action設(shè)置mthCity的getter setter,jsp上的內(nèi)容才能傳到action中
public MthCity getMthCity()
return mthCity;
}
public void setMthCity(MthCity city) {
this. mthCity = city;
}
? 從JSP提交內(nèi)容到Action,還有一種方法,就是將參數(shù)內(nèi)容放在表單form的元素中
<input type="hidden" name="mthCity.cityName" id=" mthCity " value="廣州" />
這樣,當(dāng)用以下語(yǔ)句表單提交的時(shí)候
doucment. myform.submit();
就能在Action中創(chuàng)建一個(gè)mthCity實(shí)例,并為mthCity.cityName設(shè)置值為:廣州.
原因是在頁(yè)面的表單元素中,設(shè)置了name= mthCity.cityName,而action中剛好有成員類叫mthCity,而這個(gè)類中剛好有屬性叫cityName.,就是通過(guò)這樣的方法,能將表單的內(nèi)容,提交到Action中.
Struts2 + Hibernate開發(fā)筆記(二)
二. Action -> JSP
當(dāng)要從Action中執(zhí)行的loadCity方法,要返回到j(luò)sp頁(yè)面時(shí),要在頁(yè)面上指定一個(gè)return的頁(yè)面.我們?cè)赼dmin-action.xml配置中可以看到一句
<result name="city">/admin/city.jsp</result>
而在loadCity方法中有這樣一句
return "city";
這樣,當(dāng)loadCity執(zhí)行完后,就會(huì)返回到
http://localhost/admin/city.jsp頁(yè)面.
由于在action中,cityId和mthCity.cityId是被前一個(gè)提交過(guò)來(lái)的jsp頁(yè)面賦值過(guò),所以當(dāng)程序執(zhí)行轉(zhuǎn)到city.jsp頁(yè)面的時(shí)候,這幾個(gè)值是能被使用的.我們使用jstl來(lái)獲得這些值
${cityId} , ${mthCity.citId} (前提是只要在action中,設(shè)定了cityId和mthCity類的getter/setter.)
2. 當(dāng)action中的類,傳到受hibernate管理的命名空間的類中時(shí)
這個(gè)問(wèn)題比較特殊.
我的工程中,建立了一個(gè)類cityService.這個(gè)類是進(jìn)行數(shù)據(jù)庫(kù)操作的,也就是和hibernate打交道的.而這個(gè)類在hibernate的設(shè)置中設(shè)置為被一個(gè)hibernate Session管理的范圍.
這個(gè)cityService中有一個(gè)方法,用于更新city的信息的
public void updateCity(MthCity city) throws DataAccessException,
BaseException{
MthCity icity = this.getCityById( city.getCityId());
icity.setCityName(city.getCityName());
baseHibernateDAO.update(icity);//這個(gè)baseHibernateDAO只是一個(gè)封封裝了hibernate API的包,網(wǎng)上常見.
}
大家可以看到,方法中并沒(méi)有直接update從參數(shù)傳進(jìn)來(lái)的city,而是新建了一個(gè)icity,裝了city中的信息再進(jìn)行update.
這是因?yàn)?cityService這個(gè)類被hibernate管理,所以在這個(gè)類中創(chuàng)建的內(nèi)容,才能被更新.所以我們必須使用一個(gè)新的MthCity類實(shí)例來(lái)裝著外面?zhèn)鬟M(jìn)來(lái)的內(nèi)容,才能更新.否則就會(huì)出現(xiàn)類似have the same id object in the session的錯(cuò)誤,也就是說(shuō)session中有其它相同id的對(duì)象的錯(cuò).
當(dāng)然,有另一個(gè)處理辦法,就是使用baseHibernateDAO.merge來(lái)更新內(nèi)容,而還是用update
這里就可以寫成
public void updateCity(MthCity city) throws DataAccessException,
BaseException{
baseHibernateDAO.merge(icity);
}
3. Hibernate中的version類型成員
在我的項(xiàng)目中,city有一個(gè)屬性是timestamp,對(duì)應(yīng)的是mth_city這個(gè)表,這個(gè)表通過(guò)hibernate的映射,映射成一個(gè)POJO對(duì)象
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;
}
}
大家可以看到,類中有一個(gè)屬性是timestamp,定義為Date類型.
這個(gè)類型是用于記錄數(shù)據(jù)庫(kù)操作的日期的,數(shù)據(jù)庫(kù)中的對(duì)應(yīng)字段也叫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對(duì)應(yīng)數(shù)據(jù)庫(kù)表中的CITY_ID字段,其值由oracle sequence生成
而timestamp屬性,對(duì)應(yīng)的是數(shù)據(jù)庫(kù)的TIMESTAMP字段,并且這個(gè)屬性在mapping中定義為<version>屬性
這樣,就會(huì)出現(xiàn)兩點(diǎn)要注意的地方
? 數(shù)據(jù)庫(kù)中的這個(gè)timestamp的字段一定要有值,并且是日期值,否則當(dāng)hibernate更新這個(gè)字段沒(méi)有值的那條記錄時(shí),會(huì)出現(xiàn)如下錯(cuò)誤
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.mytophome.admin.domain.MthCity#1]
? 在更新MthCity中的值到數(shù)據(jù)庫(kù)中,也就是更新一條記錄時(shí),一定不設(shè)調(diào)用方法人工設(shè)置timestamp屬性的值,也就是下面的語(yǔ)句不能出現(xiàn)
mthCity.setTimeStamp(new Date());
否則也會(huì)引起hibernate的出錯(cuò).