Web DbForms (续一)
(说明:由于本人水平有限,所以翻译错误在所难免。为了不给读者造成错误的概念,这里采用了中英文对照方式)
A Simple Example
一个简单例子
Description: This JSP view (service.jsp) enables the user(s) to administer the services the agency provides to its customers. The user (an employee of the agency or call center agent) should get a list of all the existing services, along with textfields and buttons to update and delete data. Finally, an empty input mask for inserting new services is needed.
描述:此JSP视图(service.jsp)允许用户管理代理商提供给客户的服务。用户(代理商的一个雇员或者呼叫中心的代表)会得到一个所有存在的服务的列表,连同文本输入框和按钮一起来更新和删除数据。最后,需要一个空的输入界面来增加新的服务。
Listing 1. -- service.jsp
<%-- import DbForms tag library --%>
<%@ taglib uri="/WEB-INF/taglib.tld" prefix="db" %>
<html>
<head>
<db:base/>
</head>
<body>
<db:errors/> <%-- show eventually occured errors --%>
<db:dbform tableName="service" maxRows="*" followUp="/service.jsp">
<%-- the header gets rendered one time --%>
<db:header>
<db:gotoButton caption="Menu" destination="/menu.jsp" />
<h1>Services we provide</h1>
<center><h3>Our existing services</h3></center>
<table border="5" width="60%" align="CENTER">
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Actions</th>
</tr>
</db:header>
<%-- the body gets rendered for each data row in the query
it contains textfields and action buttons for manipulating data --%>
<db:body>
<tr>
<td><db:textField fieldName="id" size="5"/><;/td>
<td><db:textField fieldName="name" size="20" maxlength="30"/></td>
<td><db:textField fieldName="description" size="24" maxlength="255"/></td>
<td>
<db:updateButton caption="Update"/>
<db:deleteButton caption="Delete"/>
</td>
</tr>
</db:body>
<%-- the footer gets rendered 1 time
it contains a textfields for entering new datasets --%>
<db:footer>
</table>
<center><h3>Enter new service:</h3></center>
<table align="center" border="3">
<tr>
<td>Id</td>
<td><db:textField size="5" fieldName="id"/></td>
</tr>
<tr>
<td>Name</td>
<td><db:textField size="20" maxlength="30" fieldName="name"/></td>
</tr>
<tr>
<td>Description</td>
<td><db:textArea rows="4" cols="20" wrap="virtual" fieldName="description"/></td>
<tr>
</table>
<br><center><db:insertButton caption="Insert new service!"/></center>
</db:footer>
</db:dbform>
</body>
</html>
Remarks
备注
The result is shown in Figure 3:
结果如图3所示
Figure 3. Managing services (service.jsp)
An Example of Nested Forms
一个嵌套表单的例子
The following page (customer_order.jsp) gives you the functionality to manage the incoming orders of a customer. You are able to edit both orders of a customer and the customer data itself. Furthermore, you do not need to struggle with plain service IDs, but will be able to conveniently select the services from a select box by name.
下面的页面(customer_order.jsp)让你能管理收到的客户订单。你既可以编辑客户订单,也可以编辑客户数据。此外,你不用对单调的服务ID苦恼,相反,可以方便的在选择列表中用姓名来选择服务。
Figure 4. Underlying data for customer_order.jsp
Listing 2. customer_order.jsp
<%-- import DbForms tag library --%>
<%@ taglib uri="/WEB-INF/dbforms.tld" prefix="db" %>
<html>
<head>
<db:base/>
</head>
<body>
<db:errors/> <%-- show eventually occured errors --%>
<%-- the root form --%>
<db:dbform tableName="customer" maxRows="1" followUp="/customer_orders.jsp"
autoUpdate="false">
<db:header>
<db:gotoButton caption="Menu" destination="/menu.jsp" />
<h1>Customer</h1>
</db:header>
<db:body> <%-- the body shows the data of the current customer --%>
<table align="center">
<tr>
<td>Id </td>
<td><db:textField fieldName="id" size="4"/></td>
</tr>
<tr>
<td>First Name</td>
<td><db:textField fieldName="firstname" size="18"/></td>
</tr>
<tr>
<td>Last Name</td>
<td><db:textField fieldName="lastname" size="18"/></td>
</tr>
<tr>
<td>Address:</td>
<td><db:textField fieldName="address" size="25" /></td>
</tr>
<tr>
<td>Postal code/City</td>
<td><db:textField fieldName="pcode" size="6"/> -
<db:textField fieldName="city" size="16"/> </td>
</tr>
</table>
<br>
<%-- table embedding the subform --%>
<table align="center" border="1">
<tr>
<td>
<center><p><b>Orders</b></p></center>
<%-- this is the begin of the subform:
the subform renders all the services the current customer has ordered --%>
<db:dbform tableName="orders" maxRows="3" parentField="id" childField="customer_id"
followUp="/customer_orders.jsp" autoUpdate="false">
<db:header>
<%-- code for showing existing orders of services for that customer --%>
<table>
<tr>
<td width="40"></td>
<td>service</td>
<td>orderdate</td>
</tr>
</db:header>
<db:body allowNew="false">
<tr>
<td width="40"><db:associatedRadio name="radio_order" /></td>
<td>
<db:select fieldName="service_id"> <%-- this allows the --%>
<db:tableData <%-- users of the application to --%>
name = "our_services" <%-- select services conveniently --%>
foreignTable = "service" <%-- from a select-box --%>
visibleFields = "name"
storeField = "id"
/>
</db:select>
</td>
<td><db:dateField fieldName="orderdate" size="14"/></td>
</tr>
</db:body>
<db:footer>
<tr>
<td width="40"></td>
<td><db:updateButton caption="Update Order" associatedRadio="radio_order"/></td>
<td><db:deleteButton caption="Delete Order" associatedRadio="radio_order"/></td>
</tr>
</table>
<%-- code for entering new orders of services --%>
<br><hr>
<table>
<tr>
<td>service</td>
<td>date</td>
<td></td>
</tr>
<tr>
<td>
<db:select fieldName="service_id">
<db:tableData
name = "our_services"
foreignTable = "service"
visibleFields = "name"
storeField = "id"
/>
</db:select>
</td>
<td><db:dateField fieldName="orderdate" size="10" /></td>
<td><db:insertButton caption="insert order" /></td>
</tr>
</table>
<center> <%-- navigating in the subform (in the list of orders of a customer --%>
<db:navFirstButton caption="<< First" />
<db:navPrevButton caption="< Previous" />
<db:navNextButton caption="Next >" />
<db:navLastButton caption="Last >>" />
</center>
</db:footer>
</db:dbform>
<%-- subform end --%>
</td>
</tr>
</table>
<%-- end of table embedding the subform --%>
<br><center>
<db:insertButton caption="Store this new Customer!" /> <%-- action buttons for --%>
<db:updateButton caption="Update Customer" /> <%-- editing data of --%>
<db:deleteButton caption="Delete Customer" /> <%-- customers --%>
</center>
</db:body>
<db:footer>
<br><center>
<db:navFirstButton caption="<< First" /> <%-- navigating in the --%>
<db:navPrevButton caption="< Previous" /> <%-- list of customers --%>
<db:navNextButton caption="Next >" />
<db:navLastButton caption="Last >>" />
<db:navNewButton caption"*"/>
</center>
</db:footer>
</db:dbform>
</body>
</html>
Remarks
备注
With this page, we have demonstrated another couple of major features of DbForms:
在本页,我们已经讲述了DbForms的另外一些主要特征:
Figure 5. Managing orders and customers with one single page (customer_order.jsp)
(未完待续)
本文地址:http://com.8s8s.com/it/it17885.htm