java类:
package com.test.work.label;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Hashtable;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;
import org.apache.velocity.runtime.parser.node.SimpleNode;
import com.test.common.Page;
import com.test.work.entity.CmsInfoEntity;
import com.test.work.web.WorkFactory;
//注意这2个接口自己定义import com.test.work.entity.CmsInfoEntity;import com.test.work.web.WorkFactory;
/**
* 列表标签
* 输入参数:
* 前台调用方式(#info_list("栏目ID","条数"))
*/
public class ArticleListAction extends Directive {
final static Hashtable<String,String> body_tpls = new Hashtable<String, String>();
@Override
public String getName() {
return "info_list";
}
@Override
public int getType() {
return BLOCK;
}
@Override
public boolean render(InternalContextAdapter context, Writer writer,
Node node) throws IOException, ResourceNotFoundException,
ParseErrorException, MethodInvocationException {
// 获取栏目ID
SimpleNode sn_Channel = (SimpleNode) node.jjtGetChild(0);
//栏目ID为空时,查询最新信息
String channelId = null;
if(!StringUtils.isEmpty((String) sn_Channel.value(context))){
channelId = (String) sn_Channel.value(context);
}
// 获取显示条数
SimpleNode sn_PageSize = (SimpleNode) node.jjtGetChild(1);
Integer pageSize = Integer.valueOf((String) sn_PageSize.value(context));
Page<CmsInfoEntity> infosPage = WorkFactory.getCmsInfoRemote().findList(1, pageSize, channelId);
Node body = node.jjtGetChild(2);
context.put("infosPage", infosPage);
StringWriter sw = new StringWriter();
body.render(context, sw);
String cache_html = sw.toString();
writer.write(cache_html.toString());
return true;
}
}
//velocity.properties 注意:多个标签的action 以英文逗号,隔开
userdirective=com.test.work.label.ArticleListAction,
//vm页面调用
velocity 标签开发
#info_list("1","5")
#foreach($news in $newsPage.Results)
<div class="wh-portal-i-item clearfix"><a><i class="fa fa-file-o"></i><span class="wh-portal-a-cursor">ss $!news.infoTitle</span> <em>( 04-28 00:00阅640次 )</em></a></div>
#end
#end
//com.test.common
Page.java
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlRootElement
@XmlSeeAlso({PageImpl.class})
public abstract class Page<E>
implements Serializable
{
private static final long serialVersionUID = 2L;
protected List<E> results;
protected int curPage;
protected int pageSize;
protected int recordCount;
public Page()
{
}
@XmlElement
public int getCurPage()
{
return this.curPage;
}
@XmlElement
public int getPageSize()
{
return this.pageSize;
}
@XmlElement
public List<E> getResults()
{
return this.results;
}
@XmlElement
public int getRecordCount()
{
return this.recordCount;
}
public Page(List<E> results, int curPage, int pageSize, int recordCount)
{
this.results = results;
this.curPage = (curPage < 1 ? 1 : curPage);
this.pageSize = (pageSize < 1 ? 1 : pageSize);
this.recordCount = recordCount;
}
public int getNextPageNumber()
{
return this.curPage < getPageCount() ? this.curPage + 1 : getPageCount();
}
public int getPrePageNumber()
{
return this.curPage > 1 ? this.curPage - 1 : 1;
}
public boolean isFirstPage()
{
return this.curPage == 1;
}
public boolean isLastPage()
{
int pageCount = getPageCount();
return (pageCount == 0) || (this.curPage == pageCount);
}
public boolean hasPrePage()
{
return this.curPage > 1;
}
public boolean hasNextPage()
{
return this.curPage < getPageCount();
}
public int getPageCount()
{
return this.recordCount / this.pageSize + (this.recordCount % this.pageSize == 0 ? 0 : 1);
}
public int getStartRecord()
{
return (this.curPage - 1) * this.pageSize + 1;
}
public int getEndRecord()
{
return getStartRecord() - 1 + getResults().size();
}
}
//PageImpl.java
import java.util.List;public class PageImpl<E> extends Page<E>
{
private static final long serialVersionUID = 1L;
public void setResults(List<E> results)
{
this.results = results;
}
public void setCurPage(int curPage)
{
this.curPage = curPage;
}
public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}
public void setRecordCount(int recordCount)
{
this.recordCount = recordCount;
}
public PageImpl()
{
super(null, 0, 0, 0);
}
public PageImpl(List<E> results, int curPage, int pageSize, int recordCount)
{
super(results, curPage, pageSize, recordCount);
}
}