truts2 + Hibernate開發(fā)筆記(一) 由于開發(fā)任務(wù)緊張,因為這里的開發(fā)筆記,僅用于記錄遇到的幾個struts2和hibernate結(jié)合開發(fā)的現(xiàn)象.不對其做分析. 1. 在使用struts2時,頁面和action之間的傳值 這是struts2和struts1最大的區(qū)別. Struts2中,action和jsp頁面之間的信息交互,是通過 action中定義的成員變量來實現(xiàn)的. 例如,我在一個名為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使用的數(shù)據(jù)對象 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了一系統(tǒng)的xml配置,而上面的admin-action.xml文件就是其中一段,因此將這一段中涉及的action類設(shè)定為一個模塊,就定namespace = “/admin” ) 其次 <action name="city/*" method="{1}" 這一句配置的意思,就是,當(dāng)用戶提交一個符合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方法后面是可以帶參數(shù)的.所帶的參數(shù)名,要是CityAction中定義的成員變量,包括成員類.例如,如果想提交后,CityAction中的cityId有值,鏈接可以這樣寫 http://localhost/admin/city/loadCity.do?cityId=9 這樣,在loadCity方法中,如果你訪問cityId,就可以發(fā)現(xiàn)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賦值.(當(dāng)然,getter方法就方便當(dāng)action返回到j(luò)sp頁面時,cityId的值能在頁面取到.) 如果要為action中的類成員變量賦值也是可以的 例如http://localhost/admin/city/load ... mp;mthCity.cityId=8 這條鏈接提交后,會和上面一樣調(diào)用CityAction的loadCity方法,但這里,action的成員mthCity會被創(chuàng)建實例,并為mthCity的一個屬性cityId賦值,也就是說,在action中,大家可以通過mthCity.getCityId()來獲得這個屬性的值. 當(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)用以下語句表單提交的時候 doucment. myform.submit(); 就能在Action中創(chuàng)建一個mthCity實例,并為mthCity.cityName設(shè)置值為:廣州. 原因是在頁面的表單元素中,設(shè)置了name= mthCity.cityName,而action中剛好有成員類叫mthCity,而這個類中剛好有屬性叫cityName.,就是通過這樣的方法,能將表單的內(nèi)容,提交到Action中. |
__________________ |