如何實(shí)現(xiàn)表單一次上傳多表數(shù)據(jù)并更新到數(shù)據(jù)庫
newxy的標(biāo)簽<nbean:formBean name=”…” sql=”……”/>會(huì)經(jīng)查詢語句得到一formBean,formBean中含有記錄集。如果sql語句是聯(lián)表查詢,formBean中會(huì)有來自多個(gè)表的數(shù)據(jù)。
如果用戶在<nhtml:form …>…</nhtml:form>標(biāo)簽產(chǎn)生的表單中修改了數(shù)據(jù),如何將這些修改后的多表數(shù)據(jù)更新到數(shù)據(jù)庫中。可以利用newxy的標(biāo)簽<nlogic:action1/>或<nlogic:action2/>及標(biāo)簽<nhtml:buttons/>或<nhtml:button/>來實(shí)現(xiàn)。有兩種方法。
方法一:將<nlogic:action1/>換成<nlogic:action2/>,其path屬性是struts某action配置的path元素值,通常帶有“.do"。 這樣可在后臺(tái)Struts的action中處理。
BaseDAO baseDao=new BaseDAO();
DynaDto dto=(DynaDto)formBean.getDto();
//保存第一個(gè)表
dto.set_table(“table
baseDao.update(dto);
//保存第二個(gè)表
dto.set_table(“table
baseDao.update(dto);
//保存第三個(gè)表
dto.set_table(“table
baseDao.update(dto);
如果查詢結(jié)果使用了別名,如select a.field1 as f1,... from table
dto.set_table(“table
dto.set('field1',dto.get('f1'));
baseDao.update(dto);
前臺(tái)jsp頁面類似如下:
<nlogic:action2 id=”act
<nbean:formBean name="form1" sql="..."/>
<nhtml:form action="" formName="form1">
<html:hidden property="_table" value="table1"/>
......
</nhtml:form>
<div><nhtml:buttons actionId="act1"/></div>
前臺(tái)表單中的_table屬性被忽略。如果表單某屬性是table1字段也是table2字段,如聯(lián)表查詢時(shí)的id,這個(gè)id就會(huì)更新到table1和table2中。
方法二:讓<nlogic:action1/>更新或插入表table1,在jsp頁面上鑲?cè)?/span>java腳本更新或插入其它表。但要理解<nlogic:action1/>執(zhí)行動(dòng)作的條件。
<nlogic:action1/>執(zhí)行動(dòng)作的條件是:訪問jsp頁面的參數(shù)含有_actionId、_formName,且值分別等于<nlogic:action1/>的id值、formName值。這個(gè)<nlogic:action1/>是"update"、"remove"還是其它動(dòng)作要看參數(shù)method的值是什么。java腳本執(zhí)行條件應(yīng)與<nlogic:action1/>的條件相同。jsp頁面應(yīng)類似這樣的:
<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();
//保存第二個(gè)表 第一個(gè)table1由標(biāo)簽更新了。
dto.set_table(“table
baseDao.update(dto);
//保存第三個(gè)表
dto.set_table(“table
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>
如果要在一個(gè)事務(wù)中更新多表,則應(yīng)使用方法一。如果利用newxy實(shí)現(xiàn)事務(wù),可參看newxy的技術(shù)文檔有關(guān)事務(wù)部分。
newxy新坐標(biāo)技術(shù)網(wǎng)站:http://www.newxy.net