jeecms9自定義標籤以及使用新建立的數據庫表

轉載 https://blog.csdn.net/nice_meng/article/details/89179089java

 

本系統使用的是jeecmsv9版本,收集網上知識後,進行我的彙總web

首先,本身建立一個表.. 咱們使用的是oracle的庫spring

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 建立對應的實體類 WeiPengapache

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接口方法json

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的實現類 TableTestDaoImploracle

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接口   TableTestMngapp

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的實現類  TableTestMngImplide

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 建立標籤類TableTestDirectiveoop

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]最後實現的效果

相關文章
相關標籤/搜索