自定义组件(转发)

组件类:

package com.logcd.tapestry5.myapp.components;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

public class NavBar {
@Parameter(defaultPrefix = "literal", required = true)
private String pages;

@Inject
private ComponentResources resources;

@Property
private Page _pageAttr;

public List<Page> getPageNames() {
List<Page> pageList = new ArrayList<Page>();
String[] pageArr = pages.split(",");
for(int i=0;i<pageArr.length;i++){
Page page=new Page();
page.setName(pageArr[i].split("=")[0]);
page.setUrl(pageArr[i].split("=")[1]);
pageList.add(page);
page=null;
}
return pageList;
}

public String getTabClass() {
if (_pageAttr.name.equalsIgnoreCase(resources.getPageName()))
return "current";

return null;
}

public class Page{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
private String url;
}
}

组件页面
<table class="navigation" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">

   <tr>
        <t:loop source="pageNames" value="pageAttr">
            <td class="${tabClass}">
                [<t:pagelink page="${pageAttr.url}">${pageAttr.name}</t:pagelink>]
            </td>
        </t:loop>
    </tr>

</table>

用法:
<span t:type='NavBar' pages="首页=Index,用户管理=user/CreateUser"/>

你可能感兴趣的:(自定义组件(转发))