|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
Feature Java Feature — Jakarta Struts & JavaServer Faces
The add-and-remove pattern
By: Heman Robinson
Aug. 20, 2006 11:30 AM
The "languageList" and "chosenItem" fields and the "move" method are accessed from the "execute" method of the form's submit action. In the Struts implementation, this is an instance of the Struts Action class.
/**
JavaServer Faces Implementation In JavaServer Faces, the JSP for the two lists is smaller, because JSF's tags are more compact. Instead of <table>, <tr>, and <td>, JSF uses <h:panelGrid>. Instead of <fmt:message>, JSF uses <h:outputText>. Instead of <html:select> and <html:optionsCollection>, JSF uses <h:selectManyListbox> and <f:selectItems>.
<h:panelGrid columns="3" rowClasses="center"> As in the Struts implementation, the <h:selectManyListbox> tags refer to the "availableList," "availableValues," "chosenList," and "chosenValues" properties of the backing JavaBean. The interfaces for these properties are identical to the Struts implementation. Internally the Struts Bean stores List properties as LabelValueBeans, while the JSF Bean stores them as SelectItems. Like the Struts implementation, the four buttons in the middle column are implemented with standard HTML <input> tags. For JSF these have to be enclosed in <f:verbatim> tags. Other than that, the buttons are almost identical with the Struts implementation. The only wrinkle is that JSF generates hierarchical element identifiers. That's why JavaScript for JSF often contains identifiers like those with the "form:" prefix in the code below.
<h:panelGrid columns="1"> Other than the hierarchical identifiers, the "doMove()" and "doUpdate()" functions for JSF are identical to those in the Struts implementation. JavaScript provides client-side interactivity in JSF just as it does in Struts. A convenience of JavaServer Faces is its handling of the submit action. In JavaServer Faces, you can define the submit button to explicitly invoke a method in the form bean: <h:commandButton action="#{example.submit}" value="#{bundle.submit}" /> The "example:submit" method reads the hidden field and populates the lists. Because this method is attached to the submit button, there's no need to implement an Action object.
public String submit() There's not much of a learning curve to JSF. Tags are different, but they usually produce smaller JSPs. Java code is similar and sometimes requires fewer objects. The strongest advantage of JavaServer Faces is its component architecture. If you get the free download of Sun's Java Studio Creator, you'll find it contains a complete Add-and-Remove component that you can drag-and-drop in your GUI. Sun's component includes features such as "Move Up" and "Move Down" buttons to tweak the order of the chosen items. We can expect many such useful components to emerge as JSF development advances.
Conclusion JSF provides a natural migration path for projects moving from Struts. The JSP tags are simplified; the backing bean code is similar; and if you need JavaScript for interactivity, the same functions can be used. JSF can be thought of as a simplified, componentized version of Struts. Its designers have done exactly the type of work one hopes for in a "second system": they've added useful features and reduced complexity. For any new Web project, JavaServer Faces should be strongly considered. For existing Struts projects, JSF provides a smooth migration path. Resources
Reader Feedback: Page 1 of 1
Your Feedback
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||