Chrysalis View: JSPChrysalis views consists of JSP together with a library of custom tags supporting application flow control and HTML form generation. Many of these tags support automatic form generation for JavaBeans. A typical Chrysalis form might be rendered as follows: <jutil:use var="item" controller="Catalog" /> <h1>Edit catalog item</h1> <jhtml:form action="Catalog.editItem.cmd"> <table> <tr> <td><jhtml:label property="itemId" />: </td> <td><jhtml:input property="itemId" /></td> </tr> <tr> <td><jhtml:label property="name" />: </td> <td><jhtml:input property="name" /></td> </tr> <tr> <td><jhtml:label property="stock" />: </td> <td><jhtml:input property="stock" /></td> </tr> <tr> <td></td> <td><input type="submit" value="Submit"></td> </tr> </table> </jhtml:form> The <h1>Edit catalog item</h1> <form action="Catalog.editItem.cmd" method="post"> <table> <tr> <td>Item Id: </td> <td>1098<input type="hidden" value="1098" name="itemId"></td> </tr> <tr> <td>Name: </td> <td><input value="Hat" name="name" size="20"></td> </tr> <tr> <td>Stock: </td> <td><input value="16" name="stock" size="20"></td> </tr> <tr> <td></td> <td><input type="submit" value="Submit"></td> </tr> </table> </form> For forms generated as two-column tables, more compact view
logic is possible using the <jutil:use var="item" controller="Catalog" /> <h1>Edit catalog item</h1> <jhtml:form action="Catalog.editItem.cmd"> <jhtml:field property="itemId" /> <jhtml:field property="name" /> <jhtml:field property="stock" /> <tr> <td></td> <td><input type="submit" value="Submit"></td> </tr> </table> </jhtml:form> Form Validation JavaScriptFor generated JavaScript validations to work, the page must
import and initialize the <!-- In template.jsp for the sample application --> <script src="/catalog/javascript/FormValidation.js"></script> <body onload="initFormValidation()"> If for some reason you want to generate your own form validation
logic, you can disable the automatic generation of JavaScript
validation by setting the <jhtml:form action="Catalog.editItem.cmd" validations="false"> Tag LogicChrysalis custom tags support limited tag logic in a JSP. A typical page will do the following:
There are three ways to load object data into a page:
Each of these tags has a <jutil:use var="order" controller="Cart" property="order" /> <jutil:new var="item" type="com.catalog.domain.Item" /> <jutil:set var="lineItems" object="order" property="lineItems" /> The tags above:
Objects loaded elsewhere in the page are referenced via the
<jutil:print object="order" property="id" /> <jhtml:label object="item" property="stock" /> <jutil:if object="lineItems" property="empty"> [Executed if lineItems collection is empty] </jutil:if> The tag logic in Chrysalis is deliberately limited, because the view component is the wrong place for complex logic. If you must have more sophisticate logic in the view, you always have the option of using scripting or some other tag library (like JSTL). Default ObjectsEvery time an object is stored in a variable via a
<jutil:use var="order" controller="Cart" /> <jutil:print property="id" /> <jutil:use var="order" controller="Cart" /> <jutil:print property="id" object="order" /> Default PropertiesFor tags that have both a <jutil:use var="order" controller="Cart" /> <jutil:set var="lineItems" object="order" /> <jutil:use var="order" controller="Cart" property="order" /> <jutil:set var="lineItems" object="order" property="lineItems" /> View Helper JavaBeansIf you need complex logic for a particular JSP view, rather than embedding that logic in the page itself, you should create a helper Java class to support the logic. The helper class should be a JavaBean. To use a helper JavaBean in a page:
For example suppose you needed to make complex computations in
package com.domain.catalog; import java.util.Collection; import java.util.Iterator; public class OrderHelper { private Order order; /** Constructor */ public OrderHelper(Order order) { this.order = order; } public double getOrder() { return this.order; } public double getOrderTotal() { double total = 0; Collection lineItems = order.getLineItems(); Iterator i = lineItems.iterator(); while (i.hasNext()) { LineItem item = (LineItem) i.next(); total += item.getAmount() * item.getPrice(); } return total; } public boolean isHighPriceOrder() throws Exception { return (getOrderTotal() > 100); } // Other methods ... } The initializer for the public class CartController { private Order currentOrder; public double getOrderHelper() { return new OrderHelper(this.currentOrder); } // Other methods ... } You could then use the <jutil:use var="orderHelper" controller="Cart" /> Order total: <jutil:print object="orderHelper" property="orderTotal" /> <jutil:if object="orderHelper" property="highPriceOrder"> You qualify for free shipping! </jutil:if object="orderHelper"> <jutil:set var="order" object="orderHelper" /> [Print other order data using order object] This approach has many advantages:
If correctly written, the view helper JavaBeans will make no
reference to Chrysalis or the Servlet API. Therefore, they can be
reused in other UI. Better still, important logic in the view
helpers can be migrated into the business object layer (e.g. moving
the Servlet 2.2 View URLsMost of Chrysalis is based on the older Servlet 2.2/JSP 1.1 standard. Chrysalis does use one feature of the Servlet 2.3+ API: filters. Chrysalis needs to perform some preprocessing before displaying JSP pages, and filters are the best way to do this. To support the older standard, Chrysalis provides a special
FilterServlet that invokes Servlet 2.3 filters in Servlet 2.2
containers. The FilterServlet requires a URL pattern, and this
pattern cannot be Unfortunately, this means that for Servlet 2.2 containers, the URLs used to invoke the view and the file name of the page will not be the same.
Though awkward, this mechanism does allow Chrysalis to support
older servlet engines that only comply with the Servlet 2.2
standard. The Servlet 2.3 version of Chrysalis also recognizes the
" The sample catalog application uses the " Tag Library DocumentationAdditional information about Chrysalis JSP logic can be found in the documentation for the Chrysalis Tag Libraries. |