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. (十三)在ASP.NET CORE中使用Options

    这一节介绍Options方法,继续在OptionsBindSample项目下. 在项目中添加一个Controllers文件夹,文件夹添加一个HomeController控制器 HomeControll ...

  2. Oracle SQL外连接

    SQL提供了多种类型的连接方式,它们之间的区别在于:从相互交叠的不同数据集合中选择用于连接的行时所采用的方法不同.连接类型        定义内连接           只连接匹配的行左外连接     ...

  3. GNU 交叉工具链的介绍与使用

    常用工具介绍 名称 归属 作用 arm­linux­as binutils 编译 ARM 汇编程序 arm­linux­ar binutils 把多个.o 合并成一个.o 或静态库(.a) arm­l ...

  4. 详解redis服务

    http://mp.weixin.qq.com/s?__biz=MzIyMDA1MzgyNw==&mid=2651968327&idx=1&sn=6e6cb01d334d7ae ...

  5. 【hihocoder】Demo Day

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You work as an intern at a robotics startup. Today is your co ...

  6. 小程序template怎样渲染页面的

    template模板渲染demo wxml页面 <view class="btmcon"> <text class="btmtitle"> ...

  7. Vim统计字符串出现次数

    关键命令: :%s/pattern//gn 参数说明: % - 指明操作区间,%表示全文本:可以使用1,$或者行区间代替 s – substitute,表示替换 pattern - 要查找的字符串 / ...

  8. CUDA 关于 BLOCK数目与Thread数目设置

    GPU的计算核心是以一定数量的Streaming Processor(SP)组成的处理器阵列,NV称之为Texture Processing Clusters(TPC),每个TPC中又包含一定数量的S ...

  9. error LNK1104: 无法打开文件“C:\Users\Administrator\Desktop\....\\xxxx.exe”

    1.错误原因:项目在在打开前已经打开了win32里面的程序,并且运行.  解决方法:关闭直接在win32中运行的程序,再点击调试 2.暂时不知道原因,打开程序还会报无权限创建可执行文件.如下图所示 解 ...

  10. java读写属性配置文件

    package readproperties; import java.io.FileInputStream; import java.io.IOException; import java.io.I ...