技术开发 频道

Tapestry 和 Wicket 的比较

  控制结构

  我们假设有一个分配给许多人的任务列表,并希望将它显示到一个 HTML 表中。 Tapestry 采用 For 标准组件来遍历集合,如清单 9、10、11 所示:

  清单 9. Tapestry 页面说明

    <component id="receivedItems" type="For">
        
<binding name="source" value="ognl:receivedItems"/>
        
<binding name="value" value="ognl:currentItem"/>
    
</component>
    
<component id="itemId" type="Insert">
        
<binding name="value" value="ognl:currentItem.itemId"/>
    
</component>
    
<component id="subject" type="Insert">
        
<binding name="value" value="ognl:currentItem.subject"/>
    
</component>
    
<component id="creator" type="Insert">
        
<binding name="value" value="ognl:currentItem.creator"/>
    
</component>
    
<component id="recipient" type="Insert">
        
<binding name="value" value="ognl:currentItem.recipient"/>
    
</component>

  清单 10. Tapestry Java 类

    public abstract List getReceivedItems();
    
public abstract void setReceivedItems(List items);
    
public abstract ActionItem getCurrentItem();

  清单 11. Tapestry HTML 标记

    <tr jwcid="receivedItems">
        
<td><span jwcid="itemId">ID</span></td>
        
<td><span jwcid="subject">Subject</span></td>
        
<td><span jwcid="creator">Creator</span></td>
        
<td><span jwcid="recipient">Recipient</span></td>
    
</tr>

  在 Wicket 中,我们采用 ListView 类并匿名地实现其 populateItem() 方法,如清单 12、13 所示:

  清单 12. Wicket Java 类

    add(new ListView("receivedItems", items) {
        protected void populateItem(final ListItem item) {
            ActionItem todo
= (ActionItem) item.getModelObject();

            item.add(
new Label("itemId", String.valueOf(todo.getItemId()));
            item.add(
new Label("subject", todo.getSubject()));
            item.add(
new Label("creator", todo.getCreator()));
            item.add(
new Label("recipient", todo.getRecipient()));
        }
    });

  清单 13. Wicket HTML 标记

    <tr wicket:id="receivedItems">
        
<td><span wicket:id="itemId">ID</span></a></td>
        
<td><span wicket:id="subject">Subject</span></td>
        
<td><span wicket:id="creator">Creator</span></td>
        
<td><span wicket:id="recipient">Recipient</span></td>
    
</tr>

  现在让我们实现更复杂的目标并添加一些有条件的文本呈现 — 例如,命名偶数和奇数表行的 CSS 类。因此添加以下内容。关于 Tapestry 页面说明,请参看清单 14、15:

  清单 14. Tapestry 中的页面说明

    <component id="receivedItems" type="For">
        ...
        
<binding name="index" value="ognl:currentIndex"/>
        
<binding name="class" value="ognl:currentStyleClass"/>
    
</component>

  清单 15. Tapestry Java 类

    ...
    
public abstract int getCurrentIndex();
    
public String getCurrentStyleClass() {
        return (getCurrentIndex() %
2 == 0) ? "list-row-even" : "list-row-odd";
    }

  清单 16. Wicket Java 类

    add(new ListView("receivedItems", items) {
        protected void populateItem(final ListItem item) {
            ...
            item.add(
new AttributeModifier("class", true, new AbstractReadOnlyModel() {
                
public Object getObject() {
                    return (item.getIndex() %
2 == 0) ? "list-row-even" : "list-row-odd";
                }
            }));
        }
    });
0
相关文章