日期:2014-05-16  浏览次数:20482 次

编写jstl函数

1)

函数

package cn.com.leadfar.cms.site;

import javax.servlet.jsp.PageContext;

import cn.com.leadfar.cms.backend.dao.ArticleDao;
import cn.com.leadfar.cms.backend.dao.ChannelDao;
import cn.com.leadfar.cms.backend.model.Article;
import cn.com.leadfar.cms.backend.model.Channel;
import cn.com.leadfar.cms.backend.view.InitBeanFactoryServlet;
import cn.com.leadfar.cms.utils.BeanFactory;

public class SiteFunction {
	
	/**
	 * 根据频道ID得到频道
	 * @param pc
	 * @param channelId
	 * @return
	 */
	public static Channel findChannelById(PageContext pc,String channelId){
		BeanFactory factory = (BeanFactory)pc.getServletContext().getAttribute(InitBeanFactoryServlet.INIT_FACTORY_NAME);
		ChannelDao cd = (ChannelDao)factory.getBean("channelDao");
		return cd.findChannelById(Integer.parseInt(channelId));
	}
	
	/**
	 * 根据文章ID得到Article对象
	 * @param pc
	 * @param articleId
	 * @return
	 */
	public static Article findArticleById(PageContext pc,String articleId){
		BeanFactory factory = (BeanFactory)pc.getServletContext().getAttribute(InitBeanFactoryServlet.INIT_FACTORY_NAME);
		ArticleDao articleDao = (ArticleDao)factory.getBean("articleDao");
		return articleDao.findArticleById(Integer.parseInt(articleId));
	}
}

?

2)

tld

2)

cms.tld

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">

  <tlib-version>1.0</tlib-version>
  <short-name>cms</short-name>
  <uri>http://www.leadfar.org/cms/site/function</uri>

  <function>
    <description>
      本函数的主要作用是通过频道的ID查询频道的有关信息
    </description>
    <name>channel</name>
    <function-class>cn.com.leadfar.cms.site.SiteFunction</function-class>
    <function-signature>cn.com.leadfar.cms.backend.model.Channel findChannelById(javax.servlet.jsp.PageContext, java.lang.String)</function-signature>
    <example>
       ${cms:channel(channelId)}
    </example>
  </function>
  <function>
    <description>
      本函数的主要作用是通过文章的ID查询文章的有关信息
    </description>
    <name>article</name>
    <function-class>cn.com.leadfar.cms.site.SiteFunction</function-class>
    <function-signature>cn.com.leadfar.cms.backend.model.Article findArticleById(javax.servlet.jsp.PageContext, java.lang.String)</function-signature>
    <example>
       ${cms:article(articleId)}
    </example>
  </function>
</taglib>

??3)

引入

<%@ taglib prefix="cms" uri="http://www.leadfar.org/cms/site/function" %>      

?4)

调用

${cms:channel(pageContext,param.channelId).name }