转载 https://blog.csdn.net/nice_meng/article/details/89179089
本系统使用的是jeecmsv9版本,收集网上知识后,进行个人汇总
首先,自己创建一个表.. 我们使用的是oracle的库
CREATE TABLE WEIPENG(
ID NUMBER(4) PRIMARY KEY,
CONTENT VARCHAR2(255)
);
配置表对应的映射 在 com.jeecms.cms.entity.main.hbm.oracle创建 WeiPeng.hbm.xml
<?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="WeiPeng"
table="WEIPENG"
>
<meta attribute="sync-DAO">false</meta>
<cache usage="read-write"/>
<id name="id" type="java.lang.Integer" column="ID">
<generator class="assigned">
</generator>
</id>
<property
name="content"
column="CONTENT"
type="string"
not-null="true"
length="100"
/>
</class>
</hibernate-mapping>
在 com.jeecms.cms.entity.main 创建对应的实体类 WeiPeng
package com.jeecms.cms.entity.main;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.jeecms.common.util.DateUtils;
/**
* 投稿表实体类
* @author liyu
*
*/
public class WeiPeng implements Serializable{
private Integer id;//投稿id
private String content;//标题
public JSONObject convertToJson(){
JSONObject json = new JSONObject();
if (getId()!=null) {
json.put("id", getId());
}else{
json.put("id", "");
}
if (StringUtils.isNotBlank(getContent())) {
json.put("content", getContent());
}else{
json.put("content", "");
}
return json;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
在 com.jeecms.cms.dao.main 创建Dao接口方法
package com.jeecms.cms.dao.main;
import java.util.List;
import com.jeecms.cms.entity.main.WeiPeng;
public interface TableTestDao {
public List<WeiPeng> getList();//获取数据库中的所有内容
}
在 com.jeecms.cms.dao.main.impl 创建 Dao的实现类 TableTestDaoImpl
package com.jeecms.cms.dao.main.impl;
import java.util.List;
import com.jeecms.cms.dao.main.TableTestDao;
import com.jeecms.cms.entity.main.TableTest;
import com.jeecms.cms.entity.main.WeiPeng;
import com.jeecms.common.hibernate4.Finder;
import com.jeecms.common.hibernate4.HibernateBaseDao;
public class TableTestDaoImpl extends HibernateBaseDao<WeiPeng, Integer>implements TableTestDao{
public TableTestDaoImpl() {}//空构造
@Override
protected Class<WeiPeng> getEntityClass() {
return WeiPeng.class;
}
@Override
public List<WeiPeng> getList() {
Finder f = Finder.create("from WeiPeng bean");
f.setCacheable(true);
List find = find(f);
return find;
}
}
在com.jeecms.cms.manager.main创建Mng接口 TableTestMng
package com.jeecms.cms.manager.main;
import java.util.List;
import com.jeecms.cms.entity.main.WeiPeng;
public interface TableTestMng {
public List<WeiPeng> getList();
}
在 com.jeecms.cms.manager.main.impl创建Mng的实现类 TableTestMngImpl
package com.jeecms.cms.manager.main.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.jeecms.cms.dao.main.TableTestDao;
import java.util.List;
import com.jeecms.cms.entity.main.TableTest;
import com.jeecms.cms.entity.main.WeiPeng;
import com.jeecms.cms.manager.main.TableTestMng;
public class TableTestMngImpl implements TableTestMng{
public TableTestMngImpl() {}//空构造
private TableTestDao dao;
@Autowired //自动绑定
public void setDao(TableTestDao dao) {
this.dao = dao;
}
@Transactional(readOnly = true)
public List<WeiPeng> getList() {
return dao.getList();
}
}
在com.jeecms.cms.action.directive 创建标签类TableTestDirective
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.Constants;
import com.jeecms.cms.entity.main.TableTest;
import com.jeecms.cms.entity.main.WeiPeng;
import com.jeecms.cms.manager.main.TableTestMng;
import static com.jeecms.cms.Constants.TPL_SUFFIX;
import static com.jeecms.common.web.Constants.UTF8;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.jeecms.common.web.freemarker.DefaultObjectWrapperBuilderFactory;
import com.jeecms.common.web.freemarker.DirectiveUtils;
import com.jeecms.common.web.freemarker.DirectiveUtils.InvokeType;
import com.jeecms.common.web.freemarker.ParamsRequiredException;
import com.jeecms.core.entity.CmsSite;
import com.jeecms.core.web.util.FrontUtils;
import freemarker.template.TemplateDirectiveModel;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
public class TableTestDirective implements TemplateDirectiveModel{
/**
* 标签名称
* */
public static final String TPL_NAME = "cms_table_test";
/**
* 输出参数:列表数据
*/
public static final String OUT_LIST = "tag_list";
public TableTestDirective() {}
/**
* 传入参数,列表样式。
*/
public static final String PARAM_STYLE_LIST = "styleList";
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
CmsSite site = FrontUtils.getSite(env);//获取站点
List<WeiPeng> list = getList(params, env);//获取内容列表
Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(params);
paramWrap.put(OUT_LIST, DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(list));
//将params的值复制到variable中
Map<String, TemplateModel> origMap = DirectiveUtils.addParamsToVariable(env, paramWrap);
InvokeType type = DirectiveUtils.getInvokeType(params);
String listStyle = DirectiveUtils.getString(PARAM_STYLE_LIST, params);
if (InvokeType.sysDefined == type) {
if (StringUtils.isBlank(listStyle)) {
throw new ParamsRequiredException(PARAM_STYLE_LIST);
}
env.include(Constants.TPL_STYLE_LIST + listStyle + TPL_SUFFIX, UTF8, true);
} else if (InvokeType.userDefined == type) {
if (StringUtils.isBlank(listStyle)) {
throw new ParamsRequiredException(PARAM_STYLE_LIST);
}
FrontUtils.includeTpl(Constants.TPL_STYLE_LIST, site, env);
} else if (InvokeType.custom == type) {
FrontUtils.includeTpl(TPL_NAME, site, params, env);
} else if (InvokeType.body == type) {
body.render(env.getOut());
} else {
throw new RuntimeException("invoke type not handled: " + type);
}
DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);//将variable中的params值移除
}
private List<WeiPeng> getList(Map params, Environment env)
throws TemplateException {
return tableTestMng.getList();
}
@Autowired
private TableTestMng tableTestMng;
}
标签类需要在jeecms-context.xml和jeecms.properties中进行配置
在jeecms-context.xml中加入
<bean id="cms_table_test" class="com.jeecms.cms.action.directive.TableTestDirective"/>
<bean id="TableTestMng" class="com.jeecms.cms.manager.main.impl.TableTestMngImpl" />
<bean id="TableTestDao" class="com.jeecms.cms.dao.main.impl.TableTestDaoImpl" />
在jeecms.properties中加入
directive.cms_table_test=cms_table_test
前台页面
[@cms_table_test]
[#list tag_list as a]
<li>id为:${a.id}</li>
<li>对应的内容是:${a.content}</li>
[/#list]
[/@cms_table_test]
最后实现的效果
---------------------
作者:nice_meng
来源:CSDN
原文:https://blog.csdn.net/nice_meng/article/details/89179089
版权声明:本文为博主原创文章,转载请附上博文链接!