虽然 很少人用了,希望有人能用到,在CMS系统中,为了灵活地调用数据,通常会做成jsp tag标签来调用,下面以文章列表为例子说明,通过传过栏目id做为参数,可以控制调用不同的栏目,size控制文章的显示条数,titlelen控制标题长度,避免过长的标题影响样式
package net.mbzj.tags;
import java.io.IOException;
import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.jsp.JspException;
import jakarta.servlet.jsp.PageContext;
import jakarta.servlet.jsp.tagext.SimpleTagSupport;
import net.mbzj.bean.Category;
import net.mbzj.bean.Site;
import net.mbzj.bean.Title;
import net.mbzj.dao.ArticleDao;
import net.mbzj.dao.CategoryDao;
import net.mbzj.dao.SiteDao;
import net.mbzj.utils.Criteria;
import net.mbzj.utils.StringUtils;
public class ArticleListTag extends SimpleTagSupport{
private long cid = 0;//当前栏目的ID
private long pid = 0;//当前栏目父ID
private String size;
private String orderby;
private String flag;
private int pageNo;
private int var;
private long siteId;
private int virtual;
private int titlelen;
private int infolen;
private String attrs;
private String userId;
public int getInfolen() {
return infolen;
}
public void setInfolen(int infolen) {
this.infolen = infolen;
}
public long getCid() {
return cid;
}
public void setCid(long cid) {
this.cid = cid;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getOrderby() {
return orderby;
}
public void setOrderby(String orderby) {
this.orderby = orderby;
}
public int getVar() {
return var;
}
public void setVar(int var) {
this.var = var;
}
public long getSiteId() {
return siteId;
}
public void setSiteId(long siteId) {
this.siteId = siteId;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public int getVirtual() {
return virtual;
}
public void setVirtual(int virtual) {
this.virtual = virtual;
}
public int getTitlelen() {
return titlelen;
}
public void setTitlelen(int titlelen) {
this.titlelen = titlelen;
}
public long getPid() {
return pid;
}
public void setPid(long pid) {
this.pid = pid;
}
public String getAttrs() {
return attrs;
}
public void setAttrs(String attrs) {
this.attrs = attrs;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
@Override
public void doTag() throws JspException, IOException {
Criteria cri = new Criteria();
if(size!=null&&!size.equals("")) {
cri.setPageSize(Integer.valueOf(size));
}else {
cri.setPageSize(10);
}
Site site = new Site();
if(siteId>0) {
site = SiteDao.findById(siteId);
cri.eq("siteId", siteId);
}else {
HttpServletRequest request = (HttpServletRequest) ((PageContext)this.getJspContext()).getRequest();
var s = (Site) request.getAttribute("site");
cri.eq("siteId", s.getId());
site = SiteDao.findById(s.getId());
}
siteId = site.getId();
if(orderby!=null&&!orderby.equals("")) {
cri.setOrderBy(orderby);
}
if(flag!=null&&!flag.equals("")) {
cri.eq("flag", flag);
}
if(virtual>0) {
cri.eq("virtuals", virtual);
}
if(StringUtils.isNotEmpty(userId)) {
cri.eq("userId", userId);
}else {
if(cid>0) {
//查看当前栏目是不是顶级栏目,如果是顶级栏目需要查找所有
Category category = CategoryDao.findById(Long.valueOf(cid));
if(category.getPid()==0) {
List<Category> clist = CategoryDao.findByPid(cid, siteId);
StringBuffer sb = new StringBuffer();
for(Category c:clist) {
List<Category> clist2 = CategoryDao.findByPid(c.getId(), siteId);
if(clist2!=null&&clist2.size()>0) {
for(Category c2:clist2) {
sb.append(c2.getId());
sb.append(",");
}
}else {
sb.append(c.getId());
sb.append(",");
}
}
sb.append(pid);
cri.custom(" and t1.categoryId in("+sb+") ");
}else {
cri.eq("categoryId", cid);
}
}else {
List<Category> clist = CategoryDao.findByPid(pid, siteId);
StringBuffer sb = new StringBuffer();
for(Category c:clist) {
List<Category> clist2 = CategoryDao.findByPid(c.getId(), siteId);
if(clist2!=null&&clist2.size()>0) {
for(Category c2:clist2) {
sb.append(c2.getId());
sb.append(",");
}
}else {
sb.append(c.getId());
sb.append(",");
}
}
sb.append(pid);
cri.custom(" and t1.categoryId in("+sb+") ");
System.out.println(sb.toString());
}
}
if(StringUtils.isNotEmpty(attrs)) {
//cri.eq("attr", attrs);
System.out.println("arttrs:"+attrs);
//cri.custom(" and find_in_set("+attrs+",t1.attr) ");
}
//cri.eq("status", 1);
List<Title> list = ArticleDao.page2(cri,attrs);
var = 0;
if(list!=null) {
for(Title a:list){
var++;
a.setIndex(var);
//字符串截取
if(titlelen>0&&a.getTitle().length()>titlelen) {
a.setTitle(a.getTitle().substring(0,titlelen));
}
if(infolen>0&&a.getDescription().length()>infolen) {
a.setDescription(a.getDescription().substring(0, infolen));
}
if(a.getIsJump()==1) {
a.setUrl(a.getJumpUrl());
}else {
if(site.getIsHtml()==1) {
a.setUrl("/html/"+a.getId()+".html");
}else {
a.setUrl("/article/view/"+a.getId());
}
}
getJspContext().setAttribute("art", a );
getJspContext().setAttribute("postfix", StringUtils.ACTION_POSTFIX);
getJspContext().setAttribute("size", size);
//输出标签体
getJspBody().invoke(null);
}
}
}
}
tag标签可以这样配置
<tag>
<name>articleList</name>
<tag-class>net.mbzj.tags.ArticleListTag</tag-class>
<body-content>scriptless</body-content>
<!-- 文章列表 -->
<attribute>
<name>siteId</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cid</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>attrs</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>virtual</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pid</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>size</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>orderby</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>flag</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>titlelen</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>infolen</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>userId</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
在jsp页面调用示例如下:
<cms:articleList size="6" attrs="4,">
<div class="post grid">
<div class="img"><a href="${art.url }" title="${art.title }" target="_blank" rel="bookmark">
<img data-src="${ROOT }${art.logo }" class="thumb" alt="title">
</a></div>
<a href="${art.url }" class="cat">${art.category.title }</a>
<h3 itemprop="name headline"><a itemprop="url" rel="bookmark" href="${art.url }" title="${art.title }" target="_blank">${art.title }</a></h3>
<div class="grid-meta">
<span class="views"><i class="icon icon-eye"></i> ${art.click }</span> </div>
</div>
</cms:articleList>