zoukankan      html  css  js  c++  java
  • 自定义标签mycontent_list实现

    1. 创建表jc_mycontent

         三个字段id,title,content。

    2. 创建实体类

     1 public class MyContent {
     2 private static final long serialVersionUID = 1L;
     3 private Integer id;
     4 private String title;
     5 private String content;
     6 public MyContent () {
     7 super();
     8 }
     9 ……get set方法
    10 }

    3. 配置hibernate中jc_mycontent表的配置文件

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     3 <hibernate-mapping package="com.jeecms.cms.entity.main">
     4 <class name="MyContent" table="jc_mycontent">
     5 <meta attribute="sync-DAO">false</meta>
     6 <cache usage="read-write"/>
     7 <id name="id" type="java.lang.Integer" column="id"><generator class="identity"/></id>
     8 <property name="title" column="title" type="java.lang.String" not-null="true" />
     9 <property name="content" column="content" type="java.lang.String" not-null="true" />
    10 </class>
    11 </hibernate-mapping>

    4. DAO接口

     1 public interface MyContentDao { 2 public List<MyContent> getList(); 3 } 

    5. DAOImpl

    @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;
    }
    }
    

      6. service接口

    public interface MyContentMng {
    public List<MyContent> getList();
    }
    

      7.serviceImpl

    @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;
    }
    }
    

      8. 标签类的抽象类

    最主要的就是getData这个方法,以及绑定业务层(其中也可以添加多种查询方法,可参考类AbstractContentDirective )。

    public abstract class AbstractMyContentDirective implements
    TemplateDirectiveModel {
    protected Object getData(Map<String, TemplateModel> params, Environment env)
    throws TemplateException {
    return myContentMng.getList();
    }
    @Autowired
    protected MyContentMng myContentMng;
    }
    

      9.自定义标签中最重要的类继承抽象类

    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);
    //MYOUT_LIST值为mytag_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();
    }
    }
    

      

    注:1).在Freemarker标签工具类DirectiveUtils下定义输出参数(列表数据):MYOUT_LIST
          public static final String MYOUT_LIST = "mytag_list";

         2).每一个标签(如cms_mycontent_list)的声明都是在jeecms-context.xml中进行的,还要     记得Manager、DAO的bean配置

       此外,在配置文件jeecms-servlet-front.xml中,还有一段对标签的配置.

    10. 在WEB-INF cmswww ed ag下新建模板mycontent_list.html,并加入如下代码(里边也可以自己添加一些样式,可参考 cms_sys_definedstyle_list下样式文件)

    [#list mytag_list as a]
    <li><a href="${a.title}"><font color='blue'>"${a.content}"</font></a></li>
    [/#list]


    11.在WEB-INF cmswww edindex下首页.html里加入如下代码

    [@cms_mycontent_list]
       <ul class="topnews">
       </ul>
    [/@cms_mycontent_list]


    --END--

  • 相关阅读:
    AC自动机---病毒侵袭
    线段树或树状数组---Flowers
    AC自动机---Keywords Search
    AC自动机基础知识讲解
    线段树---Atlantis
    状态压缩DP---Hie with the Pie
    状态压缩DP--Mondriaan's Dream
    【PAT-并查集-水题】L2-007-家庭房产
    【PAT-一道看着很难的水题】L2-023. 图着色问题
    【二叉搜索树】PAT-天梯赛- L2-004. 这是二叉搜索树吗?
  • 原文地址:https://www.cnblogs.com/ikei/p/7356685.html
Copyright © 2011-2022 走看看