如何實現表單一次上傳多表數據并更新到數據庫
胡立新
newxy的標簽<nbean:formBean name=”…” sql=”……”/>會經查詢語句得到一formBean,formBean中含有記錄集。如果sql語句是聯表查詢,formBean中會有來自多個表的數據。
如果用戶在<nhtml:form …>…</nhtml:form>標簽產生的表單中修改了數據,如何將這些修改后的多表數據更新到數據庫中。可以利用newxy的標簽<nlogic:action1/>或<nlogic:action2/>及標簽<nhtml:buttons/>或<nhtml:button/>來實現。有兩種方法。
方法一:將<nlogic:action1/>換成<nlogic:action2/>,其path屬性是struts某action配置的path元素值,通常帶有“.do"。 這樣可在后臺Struts的action中處理。
BaseDAO baseDao=new BaseDAO();
DynaDto dto=(DynaDto)formBean.getDto();
//保存第一個表
dto.set_table(“table1”);
baseDao.update(dto);
//保存第二個表
dto.set_table(“table2”);
baseDao.update(dto);
//保存第三個表
dto.set_table(“table3”);
baseDao.update(dto);
如果查詢結果使用了別名,如select a.field1 as f1,... from table1 a,...,則在update前應作處理,如下:
dto.set_table(“table1”);
dto.set('field1',dto.get('f1'));
baseDao.update(dto);
前臺jsp頁面類似如下:
<nlogic:action2 id=”act1” path="/myAction.do"/>
<nbean:formBean name="form1" sql="..."/>
<nhtml:form action="" formName="form1">
<html:hidden property="_table" value="table1"/>
......
</nhtml:form>
<div><nhtml:buttons actionId="act1"/></div>
前臺表單中的_table屬性被忽略。如果表單某屬性是table1字段也是table2字段,如聯表查詢時的id,這個id就會更新到table1和table2中。
方法二:讓<nlogic:action1/>更新或插入表table1,在jsp頁面上鑲入java腳本更新或插入其它表。但要理解<nlogic:action1/>執行動作的條件。
<nlogic:action1/>執行動作的條件是:訪問jsp頁面的參數含有_actionId、_formName,且值分別等于<nlogic:action1/>的id值、formName值。這個<nlogic:action1/>是"update"、"remove"還是其它動作要看參數method的值是什么。java腳本執行條件應與<nlogic:action1/>的條件相同。jsp頁面應類似這樣的:
<nlogic:action1 id="act1" formName="form1"/>
<%
String _actionId=request.getParameter("_actionId");
String _formName=request.getParameter("_formName");
if("act1".equals(_actionId) && "form1".equals(_formName)){
net.newxy.struts_faces.DynaFormBean formBean=(net.newxy.struts_faces.DynaFormBean)session.getAttribute("form1");
BaseDAO baseDao=new BaseDAO();
DynaDto dto=(DynaDto)formBean.getDto();
//保存第二個表 第一個table1由標簽更新了。
dto.set_table(“table2”);
baseDao.update(dto);
//保存第三個表
dto.set_table(“table3”);
baseDao.update(dto);
}
%>
<nbean:formBean name="form1" sql="..."/>
<nhtml:form action="/manager/user/index.jsp" formName="form1">
<html:hidden property="_table" value="table1"/>
......
</nhtml:form>
<div><nhtml:buttons actionId="act1"/></div>
如果要在一個事務中更新多表,則應使用方法一。如果利用newxy實現事務,可參看newxy的技術文檔有關事務部分。
newxy新坐標技術網站:http://www.newxy.net
posted on 2007-04-09 23:39
newxy新坐標 閱讀(1895)
評論(0) 編輯 收藏