• 原 jeecms9自定义标签以及使用新创建的数据库表


     

    本系统使用的是jeecmsv9版本,收集网上知识后,进行个人汇总

    首先,自己创建一个表.. 我们使用的是oracle的库

    1.  
      CREATE TABLE WEIPENG(
    2.  
      ID NUMBER(4) PRIMARY KEY,
    3.  
      CONTENT VARCHAR2(255)
    4.  
      );

    配置表对应的映射 在 com.jeecms.cms.entity.main.hbm.oracle创建 WeiPeng.hbm.xml

    1.  
      <?xml version="1.0"?>
    2.  
      <!DOCTYPE hibernate-mapping PUBLIC
    3.  
      "-//Hibernate/Hibernate Mapping DTD//EN"
    4.  
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
    5.  
       
    6.  
      <hibernate-mapping package="com.jeecms.cms.entity.main">
    7.  
      <class
    8.  
      name="WeiPeng"
    9.  
      table="WEIPENG"
    10.  
      >
    11.  
      <meta attribute="sync-DAO">false</meta>
    12.  
      <cache usage="read-write"/>
    13.  
      <id name="id" type="java.lang.Integer" column="ID">
    14.  
      <generator class="assigned">
    15.  
      </generator>
    16.  
      </id>
    17.  
      <property
    18.  
      name="content"
    19.  
      column="CONTENT"
    20.  
      type="string"
    21.  
      not-null="true"
    22.  
      length="100"
    23.  
      />
    24.  
      </class>
    25.  
      </hibernate-mapping>

    在 com.jeecms.cms.entity.main 创建对应的实体类 WeiPeng

    1.  
      package com.jeecms.cms.entity.main;
    2.  
       
    3.  
      import java.io.Serializable;
    4.  
      import java.util.ArrayList;
    5.  
      import java.util.Date;
    6.  
      import java.util.List;
    7.  
      import java.util.Set;
    8.  
       
    9.  
      import org.apache.commons.lang.StringUtils;
    10.  
      import org.json.JSONArray;
    11.  
      import org.json.JSONObject;
    12.  
       
    13.  
      import com.jeecms.common.util.DateUtils;
    14.  
      /**
    15.  
      * 投稿表实体类
    16.  
      * @author liyu
    17.  
      *
    18.  
      */
    19.  
      public class WeiPeng implements Serializable{
    20.  
      private Integer id;//投稿id
    21.  
      private String content;//标题
    22.  
      public JSONObject convertToJson(){
    23.  
      JSONObject json = new JSONObject();
    24.  
      if (getId()!=null) {
    25.  
      json.put("id", getId());
    26.  
      }else{
    27.  
      json.put("id", "");
    28.  
      }
    29.  
      if (StringUtils.isNotBlank(getContent())) {
    30.  
      json.put("content", getContent());
    31.  
      }else{
    32.  
      json.put("content", "");
    33.  
      }
    34.  
      return json;
    35.  
      }
    36.  
       
    37.  
      public Integer getId() {
    38.  
      return id;
    39.  
      }
    40.  
       
    41.  
      public void setId(Integer id) {
    42.  
      this.id = id;
    43.  
      }
    44.  
       
    45.  
      public String getContent() {
    46.  
      return content;
    47.  
      }
    48.  
       
    49.  
      public void setContent(String content) {
    50.  
      this.content = content;
    51.  
      }
    52.  
       
    53.  
      }

    在 com.jeecms.cms.dao.main 创建Dao接口方法

    1.  
      package com.jeecms.cms.dao.main;
    2.  
       
    3.  
      import java.util.List;
    4.  
       
    5.  
      import com.jeecms.cms.entity.main.WeiPeng;
    6.  
       
    7.  
      public interface TableTestDao {
    8.  
      public List<WeiPeng> getList();//获取数据库中的所有内容
    9.  
      }
    10.  
       

    在 com.jeecms.cms.dao.main.impl 创建   Dao的实现类 TableTestDaoImpl

    1.  
      package com.jeecms.cms.dao.main.impl;
    2.  
       
    3.  
      import java.util.List;
    4.  
       
    5.  
      import com.jeecms.cms.dao.main.TableTestDao;
    6.  
      import com.jeecms.cms.entity.main.TableTest;
    7.  
      import com.jeecms.cms.entity.main.WeiPeng;
    8.  
      import com.jeecms.common.hibernate4.Finder;
    9.  
      import com.jeecms.common.hibernate4.HibernateBaseDao;
    10.  
       
    11.  
      public class TableTestDaoImpl extends HibernateBaseDao<WeiPeng, Integer>implements TableTestDao{
    12.  
      public TableTestDaoImpl() {}//空构造
    13.  
       
    14.  
      @Override
    15.  
      protected Class<WeiPeng> getEntityClass() {
    16.  
      return WeiPeng.class;
    17.  
      }
    18.  
       
    19.  
      @Override
    20.  
      public List<WeiPeng> getList() {
    21.  
      Finder f = Finder.create("from WeiPeng bean");
    22.  
      f.setCacheable(true);
    23.  
      List find = find(f);
    24.  
      return find;
    25.  
      }
    26.  
      }
    27.  
       

     在com.jeecms.cms.manager.main创建Mng接口   TableTestMng

    1.  
      package com.jeecms.cms.manager.main;
    2.  
       
    3.  
      import java.util.List;
    4.  
      import com.jeecms.cms.entity.main.WeiPeng;
    5.  
      public interface TableTestMng {
    6.  
      public List<WeiPeng> getList();
    7.  
      }
    8.  
       

    在 com.jeecms.cms.manager.main.impl创建Mng的实现类  TableTestMngImpl

    1.  
      package com.jeecms.cms.manager.main.impl;
    2.  
       
    3.  
       
    4.  
      import org.springframework.beans.factory.annotation.Autowired;
    5.  
      import org.springframework.transaction.annotation.Transactional;
    6.  
      import com.jeecms.cms.dao.main.TableTestDao;
    7.  
      import java.util.List;
    8.  
      import com.jeecms.cms.entity.main.TableTest;
    9.  
      import com.jeecms.cms.entity.main.WeiPeng;
    10.  
      import com.jeecms.cms.manager.main.TableTestMng;
    11.  
      public class TableTestMngImpl implements TableTestMng{
    12.  
       
    13.  
      public TableTestMngImpl() {}//空构造
    14.  
       
    15.  
      private TableTestDao dao;
    16.  
       
    17.  
      @Autowired //自动绑定
    18.  
      public void setDao(TableTestDao dao) {
    19.  
      this.dao = dao;
    20.  
      }
    21.  
       
    22.  
      @Transactional(readOnly = true)
    23.  
      public List<WeiPeng> getList() {
    24.  
      return dao.getList();
    25.  
      }
    26.  
      }
    27.  
       

    在com.jeecms.cms.action.directive 创建标签类TableTestDirective

    1.  
      package com.jeecms.cms.action.directive;
    2.  
      import java.io.IOException;
    3.  
      import java.util.HashMap;
    4.  
      import java.util.List;
    5.  
      import java.util.Map;
    6.  
      import com.jeecms.cms.Constants;
    7.  
      import com.jeecms.cms.entity.main.TableTest;
    8.  
      import com.jeecms.cms.entity.main.WeiPeng;
    9.  
      import com.jeecms.cms.manager.main.TableTestMng;
    10.  
      import static com.jeecms.cms.Constants.TPL_SUFFIX;
    11.  
      import static com.jeecms.common.web.Constants.UTF8;
    12.  
      import org.apache.commons.lang.StringUtils;
    13.  
      import org.springframework.beans.factory.annotation.Autowired;
    14.  
      import com.jeecms.common.web.freemarker.DefaultObjectWrapperBuilderFactory;
    15.  
      import com.jeecms.common.web.freemarker.DirectiveUtils;
    16.  
      import com.jeecms.common.web.freemarker.DirectiveUtils.InvokeType;
    17.  
      import com.jeecms.common.web.freemarker.ParamsRequiredException;
    18.  
      import com.jeecms.core.entity.CmsSite;
    19.  
      import com.jeecms.core.web.util.FrontUtils;
    20.  
      import freemarker.template.TemplateDirectiveModel;
    21.  
      import freemarker.core.Environment;
    22.  
      import freemarker.template.TemplateDirectiveBody;
    23.  
      import freemarker.template.TemplateException;
    24.  
      import freemarker.template.TemplateModel;
    25.  
      public class TableTestDirective implements TemplateDirectiveModel{
    26.  
      /**
    27.  
      * 标签名称
    28.  
      * */
    29.  
      public static final String TPL_NAME = "cms_table_test";
    30.  
      /**
    31.  
      * 输出参数:列表数据
    32.  
      */
    33.  
      public static final String OUT_LIST = "tag_list";
    34.  
      public TableTestDirective() {}
    35.  
      /**
    36.  
      * 传入参数,列表样式。
    37.  
      */
    38.  
      public static final String PARAM_STYLE_LIST = "styleList";
    39.  
      @Override
    40.  
      public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    41.  
      CmsSite site = FrontUtils.getSite(env);//获取站点
    42.  
      List<WeiPeng> list = getList(params, env);//获取内容列表
    43.  
      Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(params);
    44.  
      paramWrap.put(OUT_LIST, DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(list));
    45.  
      //将params的值复制到variable中
    46.  
      Map<String, TemplateModel> origMap = DirectiveUtils.addParamsToVariable(env, paramWrap);
    47.  
      InvokeType type = DirectiveUtils.getInvokeType(params);
    48.  
      String listStyle = DirectiveUtils.getString(PARAM_STYLE_LIST, params);
    49.  
      if (InvokeType.sysDefined == type) {
    50.  
      if (StringUtils.isBlank(listStyle)) {
    51.  
      throw new ParamsRequiredException(PARAM_STYLE_LIST);
    52.  
      }
    53.  
      env.include(Constants.TPL_STYLE_LIST + listStyle + TPL_SUFFIX, UTF8, true);
    54.  
      } else if (InvokeType.userDefined == type) {
    55.  
      if (StringUtils.isBlank(listStyle)) {
    56.  
      throw new ParamsRequiredException(PARAM_STYLE_LIST);
    57.  
      }
    58.  
      FrontUtils.includeTpl(Constants.TPL_STYLE_LIST, site, env);
    59.  
      } else if (InvokeType.custom == type) {
    60.  
      FrontUtils.includeTpl(TPL_NAME, site, params, env);
    61.  
      } else if (InvokeType.body == type) {
    62.  
      body.render(env.getOut());
    63.  
      } else {
    64.  
      throw new RuntimeException("invoke type not handled: " + type);
    65.  
      }
    66.  
      DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);//将variable中的params值移除
    67.  
      }
    68.  
      private List<WeiPeng> getList(Map params, Environment env)
    69.  
      throws TemplateException {
    70.  
      return tableTestMng.getList();
    71.  
      }
    72.  
       
    73.  
      @Autowired
    74.  
      private TableTestMng tableTestMng;
    75.  
       
    76.  
      }
    77.  
       

    标签类需要在jeecms-context.xml和jeecms.properties中进行配置

    在jeecms-context.xml中加入

    1.  
       
    2.  
      <bean id="cms_table_test" class="com.jeecms.cms.action.directive.TableTestDirective"/>
    3.  
       
    4.  
       
    5.  
      <bean id="TableTestMng" class="com.jeecms.cms.manager.main.impl.TableTestMngImpl" />
    6.  
       
    7.  
       
    8.  
      <bean id="TableTestDao" class="com.jeecms.cms.dao.main.impl.TableTestDaoImpl" />

    在jeecms.properties中加入

    directive.cms_table_test=cms_table_test
    

    前台页面 

    1.  
      [@cms_table_test]
    2.  
      [#list tag_list as a]
    3.  
      <li>id为:${a.id}</li>
    4.  
      <li>对应的内容是:${a.content}</li>
    5.  
      [/#list]
    6.  
      [/@cms_table_test]

    最后实现的效果

  • 相关阅读:
    RGB888转RGB666
    bmp文件格式详细解析
    Qt 5简介
    IntelliJ IDEA 快捷键
    猫猫学iOS之小知识之_xcode插件的删除方法_自己主动提示图片插件KSImageNamed有时不灵_分类或宏之类不能自己主动提示,
    sql server 2008出现远程过程调用失败
    Oracle-31-对视图DML操作
    uva 11127(暴力)
    各种排序算法的分析与实现
    我的Android进阶之旅------&gt;怎样解决Android 5.0中出现的警告: Service Intent must be explicit:
  • 原文地址:https://www.cnblogs.com/Jeely/p/11214307.html
Copyright © 2020-2023  润新知