1.tml文件
<t:layout xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<t:beaneditform t:id="user" submitlabel="Create User" object="user"/>
<h1>List Users</h1>
<t:grid source="users" row="user" rowsPerPage="5" pagerPosition="bottom" empty="nodata"
t:rowClass="literal:evenodd" add="operation">
<t:parameter name="userNameCell">
<t:pagelink page="user/edit" context="user.id">${user.userName}</t:pagelink>
</t:parameter>
<t:parameter name="operationCell">
<t:actionlink t:id="delete" context="user.id">Delete</t:actionlink>
</t:parameter>
</t:grid>
</t:layout>
2.java文件
package com.logcd.tapestry5.myapp.pages.user;
import java.util.List;
import org.apache.tapestry5.annotations.IncludeStylesheet;
import org.apache.tapestry5.ioc.annotations.Inject;
import com.logcd.myapp.dao.UserDao;
import com.logcd.myapp.models.User;
@SuppressWarnings("unchecked")
@IncludeStylesheet("classpath:com/logcd/tapestry5/myapp/pages/user/CreateUser.css")
public class CreateUser {
private User user;
@Inject
private UserDao userDao;
public CreateUser(){}
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
Object onSuccess() {
user.setPhones(null);
userDao.save(user);
return CreateUser.class;
}
void onActionFromDelete(long userId){
userDao.delete(userDao.findById(userId));
}
public List<User> getUsers() { return userDao.findAll(); }
}
3.properties文件
#定义表头文本
operation-label=操作
userName-label=用户名称
emailAddress-label=电子邮箱
birthday-label=出生日期
role-label=角色
也可以在java文件中定义表头
@Inject
private BeanModelSource beanModelSource;
@Inject
private ComponentResources componentSources;
@Inject
private Messages _messages;
private BeanModel userBeanModel;
public BeanModel getUserBeanModel(){
return buildUserBeanModel(beanModelSource,componentSources);
}
public BeanModel buildUserBeanModel(BeanModelSource beanModelSource, ComponentResources componentSources){
if(userBeanModel == null){
userBeanModel = beanModelSource.createEditModel(User.class,_messages);
userBeanModel.get("userName").label("用户名");
userBeanModel.get("emailAddress").label("电子邮箱");
userBeanModel.get("birthday").label("出生日期");
userBeanModel.get("role").label("角色");
userBeanModel.add("operation",new LogcdPropertyConduit()).label("操作");
}
return userBeanModel;
}
同时给grid标签加上model="userBeanModel"就可以了。
public class LogcdPropertyConduit implements PropertyConduit{
public Object get(Object arg0) {
return "";
}
@SuppressWarnings("unchecked")
public Class getPropertyType() {
return String.class;
}
public void set(Object arg0, Object arg1) {
}
public <T extends Annotation> T getAnnotation(Class<T> arg0) {
return null;
}
}
4.css文件
table.t-data-grid {
border-collapse: collapse;
border-left: 1px solid silver;
width: 100%;
}
table.t-data-grid thead tr th{
background: #990000
color: white;
text-align: center;
vertical-align: middle;
font-size:10px;
}
div.t-data-grid-pager span.current {
background: #FFFFFF none repeat scroll 0%;
border: 1px solid #CCCCCC;
color: #CCCCCC;
padding: 2px 5px;
text-decoration: none;
}
div.t-data-grid-pager a:hover {
background: #CC0000 none repeat scroll 0%;
color: #FFFFFF;
}
div.t-data-grid-pager a {
border: 1px solid silver;
color: #CC0000;
font-size: medium;
margin-right: 5px;
padding: 2px 5px;
text-decoration: none;
}
div.t-data-grid-pager span.current {
border: 1px solid silver;
color: black;
font-size: medium;
margin-right: 5px;
padding: 2px 5px;
text-decoration: none;
}
.evenodd {
line-height: 23px;
background-color: expression(( this.sectionRowIndex % 2 == 0) ?
"#E6EFF7" : "#F4F8FF" );
}
.userName,.operation,.birthday,.role{
text-align: center;
vertical-align: middle;
}