| truts2 + Hibernate開(kāi)發(fā)筆記(一) 由于開(kāi)發(fā)任務(wù)緊張,因為這里的開(kāi)發(fā)筆記,僅用于記錄遇到的幾個(gè)struts2和hibernate結合開(kāi)發(fā)的現象.不對其做分析. 1. 在使用struts2時(shí),頁(yè)面和action之間的傳值 這是struts2和struts1最大的區別. Struts2中,action和jsp頁(yè)面之間的信息交互,是通過(guò) action中定義的成員變量來(lái)實(shí)現的. 例如,我在一個(gè)名為EstateAction的類(lèi)中有如下定義 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"; } } 然后,這里上面的類(lèi)中的成員類(lèi)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使用的數據對象 POJO類(lèi). 有了這兩個(gè)類(lèi)后,我們來(lái)看看Struts2的Action和JSP頁(yè)面之間是如何交互的 一. JSP -> Action Jsp頁(yè)面 以下是一個(gè)jsp頁(yè)面submit.jsp.這個(gè)頁(yè)面只有一個(gè)功能,就是向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> 大家可以看到,這個(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模塊名,可以不寫(xiě),但如果寫(xiě)了,能比較方便的將struts2的action按配置來(lái)分模塊.(何謂分模塊呢?struts2有個(gè)特性,是action定義不需要像struts1一樣寫(xiě)在同一個(gè)struts.xml文件中.而是可以使用include的形式.例如我使用的項目的struts.xml文件就是這樣寫(xiě)的: <struts> <include file="struts-action/admin-action.xml"/> <include file="struts-action/agent-action.xml"/> </struts> 這樣include了一系統的xml配置,而上面的admin-action.xml文件就是其中一段,因此將這一段中涉及的action類(lèi)設定為一個(gè)模塊,就定namespace = “/admin” ) 其次 <action name="city/*" method="{1}" 這一句配置的意思,就是,當用戶(hù)提交一個(gè)符合struts2格式的申請時(shí)(所有包含.do形式的http鏈接) 例如http://localhost/admin/city/loadCity.do 其中包含了/city/那么在配置 文件中,只要定義action name=”city/*”,那么所有包含有/city/的do,都會(huì )提交到action定義的類(lèi)中來(lái),也就是類(lèi)om.mytophome.admin.representation.struts.action.CityAction中,那么提交到這個(gè)類(lèi)的哪個(gè)方法中呢? 因為選擇的是city/*.而且mothed={1},所以方法名由鏈接指定 也就是loadCity.do所指定的.loadCity方法. 這個(gè)do方法后面是可以帶參數的.所帶的參數名,要是CityAction中定義的成員變量,包括成員類(lèi).例如,如果想提交后,CityAction中的cityId有值,鏈接可以這樣寫(xiě) http://localhost/admin/city/loadCity.do?cityId=9 這樣,在loadCity方法中,如果你訪(fǎng)問(wèn)cityId,就可以發(fā)現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類(lèi)的 這樣才能在jsp頁(yè)面提交時(shí),由struts為cityId賦值.(當然,getter方法就方便當action返回到j(luò )sp頁(yè)面時(shí),cityId的值能在頁(yè)面取到.) 如果要為action中的類(lèi)成員變量賦值也是可以的 例如http://localhost/admin/city/load ... mp;mthCity.cityId=8 這條鏈接提交后,會(huì )和上面一樣調用CityAction的loadCity方法,但這里,action的成員mthCity會(huì )被創(chuàng )建實(shí)例,并為mthCity的一個(gè)屬性cityId賦值,也就是說(shuō),在action中,大家可以通過(guò)mthCity.getCityId()來(lái)獲得這個(gè)屬性的值. 當然,一定要在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="廣州" /> 這樣,當用以下語(yǔ)句表單提交的時(shí)候 doucment. myform.submit(); 就能在A(yíng)ction中創(chuàng )建一個(gè)mthCity實(shí)例,并為mthCity.cityName設置值為:廣州. 原因是在頁(yè)面的表單元素中,設置了name= mthCity.cityName,而action中剛好有成員類(lèi)叫mthCity,而這個(gè)類(lèi)中剛好有屬性叫cityName.,就是通過(guò)這樣的方法,能將表單的內容,提交到Action中. |
| __________________ |
聯(lián)系客服