CMS 是”Content Management System” 的缩写,意为” 内容管理系统”。 内容管理系统是企业信息化建设和电子政务的新宠,也是一个相对较新的市场。对于内容管理,业界还没有一个统一的定义,不同的机构有不同的理解。

自定义标签 [mycontent_list] 实现步骤:
创建 jc_mycontent 的表
-- Create table
create table JC_MYCONTENT
(
id NUMBER not null,
title VARCHAR2(),
content VARCHAR2()
)
tablespace CMS
pctfree
initrans
maxtrans
storage
(
initial 64K
minextents
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table JC_MYCONTENT
add constraint PK_ID primary key (ID)
using index
tablespace CMS
pctfree
initrans
maxtrans ;
创建实体类
package com.jeecms.cms.entity.main; public class MyContent {
private Integer id;
private String title;
private String content; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public MyContent(Integer id, String title, String content) {
super();
this.id = id;
this.title = title;
this.content = content;
} public MyContent() {
super();
} }
接下来是配置 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>
持久层接口
package com.jeecms.cms.dao.main; import java.util.List; import com.jeecms.cms.entity.main.MyContent; public interface MyContentDao {
public List<MyContent> getList();
}
持久层实现类
package com.jeecms.cms.dao.main.impl; import java.util.List; import org.springframework.stereotype.Repository; import com.jeecms.cms.dao.main.MyContentDao;
import com.jeecms.cms.entity.main.MyContent;
import com.jeecms.common.hibernate4.Finder;
import com.jeecms.common.hibernate4.HibernateBaseDao; @Repository
// 持久层
public class MyContentDaoImpl extends HibernateBaseDao<MyContent, Integer> implements MyContentDao {
@SuppressWarnings("unchecked")
public List<MyContent> getList() {
return find(byNothing());
} private Finder byNothing() {
Finder f = Finder.create();
f.append("from MyContent");// 可以在此处添加查询条件或者添加各种方法进行动态查询
f.setCacheable(true);
return f;
} @Override
protected Class<MyContent> getEntityClass() {
return MyContent.class;
}
}
业务层接口
package com.jeecms.cms.manager.main; import java.util.List; public interface MyContentMng {
public List getList();
}public interface MyContentMng {
public List getList();
}
业务层实现类
package com.jeecms.cms.manager.main.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.jeecms.cms.dao.main.MyContentDao;
import com.jeecms.cms.entity.main.MyContent;
import com.jeecms.cms.manager.main.MyContentMng;
import com.jeecms.cms.service.ContentListener; @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)。 package com.jeecms.cms.action.directive.abs; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import com.jeecms.cms.manager.main.MyContentMng; import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException; public abstract class AbstractMyContentDirective implements TemplateDirectiveModel {
protected Object getData(Map params, Environment env) throws TemplateException {
return myContentMng.getList();
} @Autowired
protected MyContentMng myContentMng;
}
标签工具类 DirectiveUtils 下定义输出参数: MYOUT_LIST
public static final String MYOUT_LIST = "mytag_list";
自定义标签中最重要的类继承上边的抽象类
package com.jeecms.cms.action.directive; import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.jeecms.cms.action.directive.abs.AbstractMyContentDirective;
import com.jeecms.cms.entity.main.MyContent; import static com.jeecms.common.web.freemarker.DirectiveUtils.MYOUT_LIST;
import com.jeecms.common.web.freemarker.DefaultObjectWrapperBuilderFactory;
import com.jeecms.common.web.freemarker.DirectiveUtils;
import com.jeecms.core.entity.CmsSite;
import com.jeecms.core.web.util.FrontUtils; import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel; public class MyContentListDirective extends AbstractMyContentDirective {
/**
* 模板名称
*/
public static final String TPL_NAME = "mycontent_list"; @SuppressWarnings("unchecked")
public void execute(Environment env, @SuppressWarnings("rawtypes") 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);
// OUT_LIST值为tag_list,将内容列表放入其中
paramWrap.put(MYOUT_LIST, DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper()
.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);
} @SuppressWarnings("unchecked")
protected List<MyContent> getList(Map<String, TemplateModel> params, Environment env)
throws TemplateException {
return myContentMng.getList();
}
}
在 jeecms-context.xml 中声明标签
<bean id="cms_mycontent_list" class="com.jeecms.cms.action.directive.MyContentListDirective"/>
在 jeecms-context.xml 中注入 DAO
<bean id="myContentDao" class="com.jeecms.cms.dao.main.impl.MyContentDaoImpl"/>
在 jeecms-context.xml 中注入 Manager
<bean id="myContentMng" class="com.jeecms.cms.manager.main.impl.MyContentMngImpl"/>
配置文件 jeecms-servlet-front.xml 中有一段对标签的配置
jeecms.properties 中配置标签名
directive.cms_mycontent_list=cms_mycontent_list
新建模板
WEB-INF\t\cms\www\oa\tag 下新建模板 mycontent_list.html, 并加入如下代码 (里边也可以自己添加一些样式,可参考 WEB-INF\t\cms_sys_defined\style_list 下样式文件) [#list mytag_list as a]
<li>
<a href="${a.title}">"${a.content}"</a>
</li>
[/#list]
调用代码
[@cms_mycontent_list]
[#list mycontent_list as a]
<li>
<a href="${a.title}">"${a.content}"</a>
</li>
[/#list]
[/@cms_mycontent_list]
通过以上这些代码,实现将自己的表 jc_mycontent 中的数据查询并显示在页面上 本文作者: IIsKei
本文链接: http://www.iskei.cn/posts/25712.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

CMS 是”Content Management System” 的缩写,意为” 内容管理系统”。 内容管理系统是企业信息化建设和电子政务的新宠,也是一个相对较新的市场。对于内容管理,业界还没有一个统一的定义,不同的机构有不同的理解。

自定义标签 [mycontent_list] 实现步骤:

创建 jc_mycontent 的表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- Create table
create table JC_MYCONTENT
(
id NUMBER not null,
title VARCHAR2(250),
content VARCHAR2(250)
)
tablespace CMS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table JC_MYCONTENT
add constraint PK_ID primary key (ID)
using index
tablespace CMS
pctfree 10
initrans 2
maxtrans 255;

创建实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.jeecms.cms.entity.main;

public class MyContent {
private Integer id;
private String title;
private String content; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public MyContent(Integer id, String title, String content) {
super();
this.id = id;
this.title = title;
this.content = content;
} public MyContent() {
super();
} }

接下来是配置 hibernate 中 jc_mycontent 表的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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>

持久层接口

1
2
3
4
5
6
7
8
9
package com.jeecms.cms.dao.main;

import java.util.List;

import com.jeecms.cms.entity.main.MyContent;

public interface MyContentDao {
public List<MyContent> getList();
}

持久层实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.jeecms.cms.dao.main.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.jeecms.cms.dao.main.MyContentDao;
import com.jeecms.cms.entity.main.MyContent;
import com.jeecms.common.hibernate4.Finder;
import com.jeecms.common.hibernate4.HibernateBaseDao; @Repository
// 持久层
public class MyContentDaoImpl extends HibernateBaseDao<MyContent, Integer> implements MyContentDao {
@SuppressWarnings("unchecked")
public List<MyContent> getList() {
return find(byNothing());
} private Finder byNothing() {
Finder f = Finder.create();
f.append("from MyContent");// 可以在此处添加查询条件或者添加各种方法进行动态查询
f.setCacheable(true);
return f;
} @Override
protected Class<MyContent> getEntityClass() {
return MyContent.class;
}
}

业务层接口

1
2
3
4
5
6
7
8
9
package com.jeecms.cms.manager.main;

import java.util.List;

public interface MyContentMng {
public List getList();
}public interface MyContentMng {
public List getList();
}

业务层实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.jeecms.cms.manager.main.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.jeecms.cms.dao.main.MyContentDao;
import com.jeecms.cms.entity.main.MyContent;
import com.jeecms.cms.manager.main.MyContentMng;
import com.jeecms.cms.service.ContentListener; @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)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.jeecms.cms.action.directive.abs;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.jeecms.cms.manager.main.MyContentMng;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException; public abstract class AbstractMyContentDirective implements TemplateDirectiveModel {
protected Object getData(Map params, Environment env) throws TemplateException {
return myContentMng.getList();
} @Autowired
protected MyContentMng myContentMng;
}

标签工具类 DirectiveUtils 下定义输出参数: MYOUT_LIST

1
public static final String MYOUT_LIST = "mytag_list";

自定义标签中最重要的类继承上边的抽象类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.jeecms.cms.action.directive;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.jeecms.cms.action.directive.abs.AbstractMyContentDirective;
import com.jeecms.cms.entity.main.MyContent; import static com.jeecms.common.web.freemarker.DirectiveUtils.MYOUT_LIST;
import com.jeecms.common.web.freemarker.DefaultObjectWrapperBuilderFactory;
import com.jeecms.common.web.freemarker.DirectiveUtils;
import com.jeecms.core.entity.CmsSite;
import com.jeecms.core.web.util.FrontUtils; import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel; public class MyContentListDirective extends AbstractMyContentDirective {
/**
* 模板名称
*/
public static final String TPL_NAME = "mycontent_list"; @SuppressWarnings("unchecked")
public void execute(Environment env, @SuppressWarnings("rawtypes") 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);
// OUT_LIST值为tag_list,将内容列表放入其中
paramWrap.put(MYOUT_LIST, DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper()
.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);
} @SuppressWarnings("unchecked")
protected List<MyContent> getList(Map<String, TemplateModel> params, Environment env)
throws TemplateException {
return myContentMng.getList();
}
}

在 jeecms-context.xml 中声明标签

1
<bean id="cms_mycontent_list" class="com.jeecms.cms.action.directive.MyContentListDirective"/>

在 jeecms-context.xml 中注入 DAO

1
<bean id="myContentDao" class="com.jeecms.cms.dao.main.impl.MyContentDaoImpl"/>

在 jeecms-context.xml 中注入 Manager

1
<bean id="myContentMng" class="com.jeecms.cms.manager.main.impl.MyContentMngImpl"/>

配置文件 jeecms-servlet-front.xml 中有一段对标签的配置

jeecms.properties 中配置标签名

1
directive.cms_mycontent_list=cms_mycontent_list

新建模板

WEB-INF\t\cms\www\oa\tag 下新建模板 mycontent_list.html, 并加入如下代码 (里边也可以自己添加一些样式,可参考 WEB-INF\t\cms_sys_defined\style_list 下样式文件)

1
2
3
4
5
[#list mytag_list as a]
<li>
<a href="${a.title}">"${a.content}"</a>
</li>
[/#list]

调用代码

1
2
3
4
5
6
7
[@cms_mycontent_list]
[#list mycontent_list as a]
<li>
<a href="${a.title}">"${a.content}"</a>
</li>
[/#list]
[/@cms_mycontent_list]

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

相关文章
Donate comment here

打赏

JEECMS 自定义标签的更多相关文章

  1. JEECMS自定义标签

    查看JEECMS的源代码发现开发者版本还没有类似现成的统计标签,一种解决的办法是使用现有的JEECMS标签,像这样Struts( [@cms_content_list channel=id]${tag ...

  2. Jeecms自定义标签用法[单个内容]

    1.com.jeecms.cms.action.directive包下建立自己的标签类

  3. [原创]JEECMS 自定义标签调用广告版位下的所有广告(利用广告管理管理首页幻灯片)

    JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法: [@cms_advertising id='3']             <img src=&quo ...

  4. JEECMS自定义标签开发步骤2

    JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法: [@cms_advertising id='3']             <img src=&quo ...

  5. JEECMS自定义标签开发步骤

    JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法: [@cms_advertising id='3']             <img src=&quo ...

  6. JEECMS站群管理系统-- 自定义标签及使用自己创建的表的实现过程

    下面是我自己定义的标签mycontent_list 首先,在数据库里创建了一个jc_mycontent的表,其中有id,title,content三个字段 其次,创建了一个实体类 public cla ...

  7. 自己动手编写JEECMS自定义栏目统计标签

    今天想在给Java客二级版面加入栏目文章统计效果,如下图, 查看JEECMS的源代码发现开发者版本还没有类似现成的统计标签,一种解决的办法是使用现有的JEECMS标签,像这样Struts( [@cms ...

  8. [JSP]自定义标签库taglib

    自定义标签的步骤 自定义标签的步骤大概有三步: 1.继承javax.servlet.jsp.tagext.*下提供的几个标签类,如Tag.TagSupport.BodyTagSupport.Simpl ...

  9. [Java] JSP笔记 - 自定义标签

    自定义标签的创建步骤: 自定义标签的四大功能: 自定义标签的类结构: 在 1.0 中呢, 可以将 <body-content> 的值设置为 JSP, 2.0中则不允许在自定义标签体中出现j ...

随机推荐

  1. RHEL7中网卡绑定team和bond的区别

    red hat 官方给出的team和bond特性对比 A Comparison of Features in Bonding and Team Feature Bonding Team broadca ...

  2. leetcode 题型 数据结构 解法 分类总结

    第2章 线性表 2.1 数组 2.1.1 Remove Duplicates from Sorted Array 2.1.2 Remove Duplicates from Sorted Array I ...

  3. 转——调试寄存器 原理与使用:DR0-DR7

    下面介绍的知识性信息来自intel IA-32手册(可以在intel的开发手册或者官方网站查到),提示和补充来自学习调试器实现时的总结. 希望能给你带去有用的信息. (DRx对应任意的一个调试寄存器. ...

  4. vue3+node全栈项目部署到云服务器

    一.前言 最近在B站学习了一下全栈开发,使用到的技术栈是Vue+Element+Express+MongoDB,为了让自己学的第一个全栈项目落地,于是想着把该项目部署到阿里云服务器.经过网上一番搜索和 ...

  5. iOS开发系列-Category

    Category Category是OC中特有的语法.Category的作用 * 可以在不修改原来类的基础上,为这个类扩充一些方法 * 一个庞大的类可以分为多个模块开发 * 一个庞大的类可以由多个人来 ...

  6. MySql General error:2006

    当启用模块时发生Mysql数据库错误,错误信息见附件,实际是“General error: 2006 MySQL server has gone away......”错误. 解决方法:找到my.in ...

  7. Java 集群高可用监控(结合阿里SLB)脚本

    欢迎点评,大家一起来优化 计划思路: 只有在mysql slave java 进程状态都正常的情况下才允许nginx 运行, 否则就干掉它, 负载用的是阿里的SLB #bin/bash #邮件函数  ...

  8. mysql双主热备

    先搭建mysql主从模式,主从请参考mysql 主从笔记 然后在在配置文件里添加如下配置 1 log_slave_updates= #双主热备的关键参数.默认情况下从节点从主节点中同步过来的修改事件是 ...

  9. scala中ArrayBuffer简单使用

    import scala.collection.mutable.ArrayBuffer /** * 与Array区别: * 1.Array是不可变的,不能直接地对其元素进行删除操作,只能通过重赋值或过 ...

  10. spring整合Quartz框架过程,大家可以参考下

    这篇文章详细介绍了spring集成quartz框架流程,通过示例代码进行了详细说明,对学习或任务有参考学习价值,并可供需要的朋友参考. 1.quartz框架简介(m.0831jl.com) quart ...