jstl 扩展 标签 应用到cms

 

昨天看了篇文章主要把baidu与腾讯2家公司的程序员基于其所处的环境作了对比。哎,我的工资太少了,只有努力写出更强劲,更迅速的代码,提高技术。

看了之后收获专业的程序员都要养成写日志的习惯。总结问题,记录下来。(以前写的程序过段时间就忘了这个确实有用)自己学会了总结一下,方便后面的程序员能够少走弯路迅速掌握。

心胸宽阔些:中国的软件行业,发展并不很理想,中国还需要更努力,中国的软件也需要后面的人努力提高技术。看网上的东西都是外国人开发的,不爽。我也希望其它的程序员能够开放些学习心得,让我迅速掌握些新知识。

 

一共分为2个步骤(前提是下载jar包2个:jstl-1.2.jar与standard.jar)

 

第一步:创建扩展应用 

 

 

package baidu.tag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
 * 快速建站 插件 之"新闻"内容管理
 * @author kettas
 * 上午11:01:07
 */
public class NewsTag extends BodyTagSupport {
	// ######## SQL #############
	protected String sql;
	protected Object runObjects[];
	protected Object value; // tag attribute
	protected boolean valueSpecified = true; // status
	protected Object target; // tag attribute
	protected String property; // tag attribute
	private String var; // tag attribute
	private int scope; // tag attribute
	private boolean scopeSpecified; // status

	public NewsTag() {
		super();
		init();
	}

	private void init() {
		value = var = null;
		scopeSpecified = valueSpecified = false;
		scope = PageContext.PAGE_SCOPE;
	}

	public void release() {
		super.release();
		init();
	}
	/**List 可通过其它方法产生出来,当然举一反三你可以写出更多更好用的插件*/
	public int doEndTag() throws JspException {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		for (int i = 0; i < 10; i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("resource_name", "name" + i);
			map.put("resource_id", "id" + i);
			list.add(map);
		}
		value=list;
		pageContext.setAttribute(var,value, scope);
		return EVAL_PAGE;
	}
}
 

 

 

第二步:配置文件:要配置2个(/WEB-INF/tag.tld及/WEB-INF/web.xml)

 

 

<?xml version="1.0" encoding="utf-8"?>
<taglib version="2.0"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:shcemalocation="http://java.sun.com/xml/ns/j2ee 
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<description>自定义标签</description>
 <display-name>JSTL core</display-name>
 <tlib-version>1.1</tlib-version>
 <short-name>firstLabel</short-name>
 <uri>http://java.sun.com/jsp/jstl/core</uri>
  <tag>
  <description>
        Sets the result of an expression evaluation in a 'scope'
    </description>
    <name>news</name>
    <tag-class>baidu.NewsTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
        <description>
Name of the exported scoped variable to hold the value
specified in the action. The type of the scoped variable is
whatever type the value expression evaluates to.
        </description>
        <name>var</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Expression to be evaluated.
        </description>
        <name>value</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        <deferred-value>
	    <type>java.lang.Object</type>
        </deferred-value>
    </attribute>
    <attribute>
        <description>
Target object whose property will be set. Must evaluate to
a JavaBeans object with setter property property, or to a
java.util.Map object.
        </description>
        <name>target</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Name of the property to be set in the target object.
        </description>
        <name>property</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Scope for var.
        </description>
        <name>scope</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <description>
Expression to be evaluated.
        </description>
        <name>run</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
 </tag>
</taglib>

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

<web-app version="2.4" 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-app_2_4.xsd">
	<display-name>tagWeb</display-name>
	<jsp-config>
 		<taglib>
 		 <taglib-uri>firstTag</taglib-uri>
 		 <taglib-location>/WEB-INF/tag.tld</taglib-location>
 	</taglib>
  
</jsp-config>
</web-app>
 

 

第三步:使用标签

 

<%@page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib uri="firstTag" prefix="C"%>

<html>
<head>
<title>MyJSP'tag.jsp'startingpage</title>
</head>
<body>
<C:news var="list1"  run="select top 5 * from up_resource"></C:news><!-- 扩展的标签用来装载数据 依次原理可实现像国内其它CMS的标签 并且依赖于JSTL变得更强大-->
<c:forEach items="${list1}" var="map" step="1">
map-id: <c:out value="${map.resource_id}" />
Name-name: <c:out value="${map.resource_name}" /><br>
</c:forEach>
</body>
</html>
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值