利用newxy(新坐標(biāo))技術(shù)可以不寫(xiě)一行java代碼實(shí)現(xiàn)多項(xiàng)查詢。(在線演示http://www.newxy.net/zh_cn/samples/sample4/demo.jsp)
-
newxy(
新坐標(biāo))多項(xiàng)查詢利用了NQL查詢類(lèi)。
-
newxy(
新坐標(biāo))多項(xiàng)查詢用到三個(gè)標(biāo)簽,<nlogic:action1>或<nlogic:action2>、<nhtml:form />、<nhtml:button />。
-
在<nhtml:form />標(biāo)簽內(nèi)不要用 submit之類(lèi)的標(biāo)簽,如<html:submit value=""></html:submit>或<input type="submit" value=""/>, 取而代之的是<nhtml:button actionId="" method="find" />。
-
<nhtml:button actionId="" method="find" />
不一定要放到<nhtml:form />標(biāo)簽內(nèi)。
范例對(duì)行業(yè)表、企業(yè)表聯(lián)表查詢,查詢的最基本條件是 企業(yè)表的行業(yè)代碼字段值等于行業(yè)表某條主關(guān)鍵字段值。就是:
????enterprise.industry_code=industry.code
用戶所選擇查詢條件與基本條件是 and 關(guān)系。根據(jù)這樣的前提構(gòu)成的最基本查詢語(yǔ)句:
????select a.industry,b.* from industry a,enterprise b where{a.code=b.industry_code}
newxy(新坐標(biāo))將為此創(chuàng)建NQL實(shí)例:nql=new NQL(dao,"select a.industry,b.* from industry a,enterprise b where{a.code=b.industry_code}");
注意:where后緊跟的是大括號(hào){}。
為了讓理解方便,范例中實(shí)際是“行業(yè)大類(lèi)”述為“行業(yè)代碼”
<nlogic:action1/>
標(biāo)簽應(yīng)放在<nhtml:form />標(biāo)簽上面,這兩個(gè)標(biāo)簽針同一個(gè)formBean,因此屬性formName值相同,在此formName="enterprise"。
<nlogic:action1 id="act1" formName="enterprise"/>
<nhtml:form action=""? formName="enterprise">
? <html:hidden property="_dao" value="dao.default"/>
? <nhtml:hidden property="_sql"
????? value="
select a.industry,b.* from industry as a,enterprise as b where{a.code=b.industry_code}
"/>
? <table border="1">
??? <tr>
????? <td>
邏輯</td>
????? <td>
??????? <html:radio property="lg" value="1">
并且</html:radio>
??????? <html:radio property="lg" value="0">
或者</html:radio>
????? </td>
??? </tr>
??? <tr>
????? <td>
行業(yè)</td>
????? <td>
等于</td>
????? <td>
??????? <nhtml:hidden property="_item1" value="a.code"/>
??????? <html:hidden property="_item1" value="="/>
??????? <html:select property="_item1">
????????? <html:option value=""></html:option>
????????? <html:option value="A">
農(nóng)、林、牧、漁業(yè)</html:option>
????????? <html:option value="B">
采礦業(yè)</html:option>
????????? <html:option value="C">
制造業(yè)</html:option>
??????? </html:select>
作為范例,行業(yè)沒(méi)全列出。
????? </td>
??? </tr>
??? <tr>
????? <td>
企業(yè)名稱</td>
????? <html:hidden property="_item2" value="b.name"/>
????? <td>
??????? <html:select property="_item2">
????????? <html:option value=""></html:option>
????????? <html:option value="=">
等于</html:option>
????????? <html:option value="like">
包含</html:option>
??????? </html:select>
????? </td>
??
???<td>
??????? <html:text property="_item2">
??????? </html:text>
????? </td>
??? </tr>
? </table>
</nhtml:form>
加入標(biāo)簽<nhtml:button actionId="act1" method="find" value="查 詢"/>,產(chǎn)生查詢銨鈕
產(chǎn)生的查詢介面如下:
如果想知道行成的sql語(yǔ)句,可以用struts標(biāo)簽:<bean:write name="enterprise" property="_sql"/>,
說(shuō)明:
-
<nhtml:form />action
屬性值為空,是以當(dāng)前路徑為值。可查看產(chǎn)生的靜態(tài)頁(yè)面。
-
<html:hidden property="_dao" value="dao.default"/>
其作用是指明由哪個(gè)DAO類(lèi)來(lái)處理。默認(rèn)值是dao.default,此處可以不用,只為演示。"_dao"是特定的屬性名
-
<nhtml:hidden property="_sql" value="select a.industry,b.* from industry as a,enterprise as b where{a.code=b.industry_code}"/>
在此不用struts<html:hidden/>標(biāo)簽或<input type="hidden" name="_sql" value="......"/>,是因?yàn)橐陟o態(tài)頁(yè)面上不顯示這條sql語(yǔ)名,達(dá)到安全的目的。 范例靜態(tài)頁(yè)面上產(chǎn)生的是<input type="hidden" name="_sql" value="_hidden_sql_1">。 其它幾處<nhtml:hidden />作用相同。
-
而且、或者邏輯關(guān)系由 <html:radio property="_lg" value="1">并且</html:radio> <html:radio property="_lg" value="0">或者</html:radio>決定。
_lg是特定的屬性名。
-
其它查詢項(xiàng)的組合方法與“企業(yè)名稱”及“行業(yè)代碼”查詢項(xiàng)的組合方式相同。
按照例中的組合,_item1的值可行成類(lèi)似a.code='A',_item2的值可形成類(lèi)似b.name like '%公司%'。
必須一組三項(xiàng),三項(xiàng)用同一屬性名,屬性名以"_item"開(kāi)頭,后用不同數(shù)字區(qū)分。
-
另有組合方式在后序技術(shù)文檔中補(bǔ)充,此文檔暫不介紹。
利用<nlogic:iterate id="" name="" property=""></nlogic:iterate>標(biāo)簽、<nlogic:navigate/>標(biāo)簽、<nhtml:TR></nhtml:TR>標(biāo)簽、<nhtml:javascripts/>標(biāo)簽、<nhtml:stylesheets/>標(biāo)簽來(lái)分頁(yè)顯示查詢結(jié)果。
建義<nhtml:javascripts/>標(biāo)簽、<nhtml:stylesheets/>標(biāo)簽兩標(biāo)簽放在頁(yè)首或較前部。如果有javascripts腳本錯(cuò)本且數(shù)據(jù)列表沒(méi)有光帶, 是因?yàn)闆](méi)有使用<nhtml:javascripts/>標(biāo)簽、<nhtml:stylesheets/>標(biāo)簽。
<nhtml:javascripts/>
<nhtml:stylesheets/>
<logic:notEmpty name="enterprise" property="_coll">
? <table>
? <nlogic:iterate id="rec" name="enterprise" property="_coll">
??? <nhtml:TR>
????? <td>1</td>
????? <td><bean:write name="rec" property="industry"/></td>
????? <td><bean:write name="rec" property="name"/></td>
??? </nhtml:TR>
? </nlogic:iterate>
? </table>
</logic:notEmpty>
?
下圖是范例查詢結(jié)果
?
?
范例的完整jsp代碼如下:
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/newxy-html.tld" prefix="nhtml"%>
<%@ taglib uri="/WEB-INF/newxy-logic.tld" prefix="nlogic"%>
<%@ taglib uri="/WEB-INF/newxy-bean.tld" prefix="nbean"%>
<nhtml:javascripts/>
<nhtml:stylesheets/>
<nlogic:action1 id="actResource" formName="resources"/>
<nbean:formBean name="resources" sql="select resource_id,resName from resources"/>
<logic:present name="resources">
? <logic:notEmpty name="resources" property="_coll">
<div style="padding-left:40px">
<table border="0" width="700">
? <tr>
??? <td valign="top">
????? <table border="0" width="300">
??????? <tr>
????????? <td style="font-size:14px"><b>資源下載</b></td>
??????? </tr>
??????? <nlogic:iterate id="rec" name="resources" property="_coll">
????????? <nhtml:TR onclick="edit(actResource);">
??????????? <td style="cursor:hand">
????????????? <bean:write name="rec" property="resName"/>
??????????? </td>
????????? </nhtml:TR>
??????? </nlogic:iterate>
????? </table>
??? </td>
??? <bean:define id="_id" name="resources" property="resource_id">
??? </bean:define>
??? <nbean:formBean cache="false" refresh="true" name="resource" sql="select * from resources where resource_id="/>
??? <td valign="top">
????? <table border="0" width="400">
??????? <tr>
????????? <td><b>資源名:</b></td><td> <bean:write name="resource" property="resName"/></td>
??????? </tr>
??????? <tr>
????????? <td><b>版本:</b></td><td> <bean:write name="resource" property="ver"/></td>
??????? </tr>
??????? <tr>
????????? <td><b>備注:</b></td><td> <p style="width:300px"><bean:write name="resource" property="remark"/></p></td>
??????? </tr>
??????? <tr>
????????? <td><b>上傳日期:</b></td><td> <bean:write name="resource" property="uploadDate" format="yyyy年MM月dd日"/></td>
??????? </tr>
??????? <tr>
????????? <td colspan="2"> <html:link target="_blank" page="/download?name=resource&property=content&fileName=resName">下載</html:link></td>
??????? </tr>
????? </table>
??? </td>
? </tr>
</table>
</div>
? </logic:notEmpty>
</logic:present>
posted on 2007-03-08 00:18
newxy新坐標(biāo) 閱讀(1965)
評(píng)論(0) 編輯 收藏