下面是我自己定义的标签mycontent_list
首先,在数据库里创建了一个jc_mycontent的表,其中有id,title,content三个字段
其次,创建了一个实体类
1
2
3
4
5
6
7
8
|
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表的配置文件
1
2
3
4
5
6
7
8
9
10
11
|
<?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
|
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
|
@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
;
}
}
|
业务层接口
public interface MyContentMng {
public List<MyContent> getList();
}
业务层实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@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
|
public
abstract
class
AbstractMyContentDirective
implements
TemplateDirectiveModel {
protected
Object getData(Map<String, TemplateModel> params, Environment env)
throws
TemplateException {
return
myContentMng.getList();
}
@Autowired
protected
MyContentMng myContentMng;
}
|
自定义标签中最重要的类继承上边的抽象类
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
|
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);
//OUT_LIST值为tag_list,将内容列表放入其中
paramWrap.put(MYOUT_LIST, DEFAULT_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();
}
}
|
样式模板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]
首页里加入如下代码
[@cms_mycontent_list]
<ul class="topnews">
</ul>
[/@cms_mycontent_list]
通过以上这些代码,可以实现将自己的表jc_mycontent中的数据查询并显示在页面上