jeecms自定义标签

jeecms 自定义标签及使用自己创建的表的实现过程

 
标签: 

jeecms

 

cms

 

自定义标签

 

二次开发

分类: JEECMS

下面以自定义标签mycontent_list为例讲解实现步骤:
首先,在数据库里创建了一个jc_mycontent的表,其中有id,title,content三个字段
其次,创建了一个实体类
public class MyContent {
private static final long serialVersionUID 1L;
private Integer id;
private String title;
private String content;
public MyContent () {
super();
}
……get set方法
}
接下来是配置hibernate中jc_mycontent表的配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.jeecms.cms.entity.main">
<class name="MyContent" table="jc_mycontent">
<meta attribute="sync-DAO">false</meta>
<cache usage="read-write"/>
<id name="id" type="java.lang.Integer" column="id"><generator class="identity"/></id>
<property name="title" column="title" type="java.lang.String" not-null="true" />
<property name="content" column="content" type="java.lang.String" not-null="true" />
</class>
</hibernate-mapping>

与数据库交互的持久层接口
public interface MyContentDao {
public List<MyContent> getList();
}

持久层实现类
@Repository//持久层
public class MyContentDaoImpl extends HibernateBaseDao<MyContent, Integer>
implements MyContentDao {
@SuppressWarnings("unchecked")
public List<MyContent> getList(){
return find(byNothing());
}
private Finder byNothing(){
Finder Finder.create();
f.append("from MyContent");//可以在此处添加查询条件或者添加各种方法进行动态查询
f.setCacheable(true);
return f;
}

@Override
protected Class<MyContent> getEntityClass() {
return MyContent.class;
}
}
业务层接口
public interface MyContentMng {
public List<MyContent> getList();
}

业务层实现类
@Service//业务层
@Transactional
public class MyContentMngImpl implements MyContentMng {

@Transactional(readOnly true)//配置事务为只读
public List<MyContent> getList(){
return myContentDao.getList();
}
private MyContentDao myContentDao;
@Autowired//自动绑定
public void setMyContentDao(MyContentDao myContentDao) {
this.myContentDao myContentDao;
}
private List<ContentListener> listenerList;
@Autowired
public void setListenerList(List<ContentListener> listenerList) {
this.listenerList listenerList;
}
}
标签类的抽象类,最主要的就是getData这个方法,以及绑定业务层(其中也可以添加多种查询方法,可参考类AbstractContentDirective )。
public abstract class AbstractMyContentDirective implements
TemplateDirectiveModel {
protected Object getData(Map<String, TemplateModel> params, Environment env)
throws TemplateException {
return myContentMng.getList();
}
@Autowired
protected MyContentMng myContentMng;
}
自定义标签中最重要的类继承上边的抽象类
public class MyContentListDirective extends AbstractMyContentDirective {

public static final String TPL_NAME "mycontent_list";
@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
//获取站点
CmsSite site FrontUtils.getSite(env);
//获取内容列表
List<MyContent> list getList(params, env);
Map<String, TemplateModel> paramWrap new HashMap<String, TemplateModel>(params);
//MYOUT_LIST值为mytag_list,将内容列表放入其中
paramWrap.put(MYOUT_LISTDEFAULT_WRAPPER.wrap(list));
//将params的值复制到variable中
Map<String, TemplateModel> origMap DirectiveUtils.addParamsToVariable(env, paramWrap);
//没有采用默认的模板,直接采用自己写的简单的模板(mycontent_list.html)
FrontUtils.includeTpl(TPL_NAME, site, params, env);
//将variable中的params值移除
DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
}
protected List<MyContent> getList(Map<String, TemplateModel> params,
Environment env) throws TemplateException {
return myContentMng.getList();
}
}

注:1.在Freemarker标签工具类DirectiveUtils下定义输出参数(列表数据):MYOUT_LIST
      public static final String MYOUT_LIST = "mytag_list";
   2.每一个标签(如cms_mycontent_list)的声明都是在jeecms-context.xml中进行的,还要    记得Manager、DAO的bean配置

   此外,在配置文件jeecms-servlet-front.xml中,还有一段对标签的配置.  
    
在WEB-INF\t\cms\www\red\tag下新建模板mycontent_list.html,并加入如下代码(里边也可以自己添加一些样式,可参考\t\cms_sys_defined\style_list下样式文件)
[#list mytag_list as a]
<li><a href="${a.title}"><font color='blue'>"${a.content}"</font></a></li>
[/#list]


在WEB-INF\t\cms\www\red\index下首页.html里加入如下代码
[@cms_mycontent_list]
   <ul class="topnews">
   </ul>
[/@cms_mycontent_list]

通过以上这些代码,可以实现将自己的表jc_mycontent中的数据查询并显示在页面上


上述代码可能在复制中出现排版问题。

请查看原文连接:http://blog.sina.com.cn/s/blog_71420de901019v8o.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值