创建并运用客户化jsp标签

1.在WEB-INF目录下新建message.properties属性文件
      文件内容为“key-value”对,添加测试内容如下:title=hello world
                                                                   body=hello taglib 

2.定义初始化类TaglibInit,用于加载属性文件
package com.douyongtao.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class TaglibInit extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
Properties ps = new Properties();
try {
ServletContext context = config.getServletContext();
InputStream is = context.getResourceAsStream("/WEB-INF/message.properties");
ps.load(is);
is.close();
context.setAttribute("ps", ps);
} catch (IOException e) {
e.printStackTrace();
}
}
}


3.在web.xml中配置该初始化类
<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ;
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>TaglibInit</servlet-name>
<servlet-class>com.douyongtao.servlet.TaglibInit</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


4.定义标签处理类MyTag

package com.douyongtao.tag;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
public class MyTag extends TagSupport {
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@Override
public int doEndTag() throws JspException {
Properties ps = (Properties) this.pageContext.getAttribute("ps",
PageContext.APPLICATION_SCOPE);
String message = ps.getProperty(key);
try {
this.pageContext.getOut().println(message);
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
}


5.在WEB-INF目录下创建标签库描述文件MyTag.tld

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.2</jspversion>
<shortname>myTag</shortname>
<uri>/myTag</uri>
<tag>
<name>message</name>
<tagclass>com.douyongtao.tag.MyTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>key</name>
<required>true</required>
</attribute>
</tag>
</taglib>


6.创建index.jsp文件并引入标签库,然后插入标签 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/myTag" prefix="hellotag" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<body>
<p>
<hellotag:message key="title"/><br/>
<hellotag:message key="body"/>
</p>
</body>
</html>

 7.部署应用,查看运行效果,如果页面输出一下内容,恭喜你,你成功了

hello world 
hello taglib

转载于:https://www.cnblogs.com/nakedou/archive/2012/12/16/2820199.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/576154.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ArrayList源码阅读

private static void extracted() {ArrayList<StudentVO> arrayList new ArrayList<StudentVO>();arrayList.add(new StudentVO("张三", 23));arrayList.add(new StudentVO("李四", 24));arrayList.add(new StudentVO("王五", 24))…

常用的JS小功能整理

<a href"#" onclick "this.style.behaviorurl(#default#homepage);this.sethomepage(http://www.mingrisoft.com)" style" color:Black; font-size: 9pt; font-family: 宋体; text-decoration :none;" >设置主页</a> <a href&quo…

类的加载过程

类的加载过程 代码 public class Father{private int i test();private static int j method();static{System.out.print("(1)");}Father(){System.out.print("(2)");}{System.out.print("(3)");)public int test(){System.out.print("(…

微软企业库调用Oracle分页存储过程

存储过程&#xff1a;CREATE OR REPLACE PACKAGE pkg_tableTypeIS procedure FY( TableName varchar2, -- 表名getFields varchar2, -- 字段名(全部字段为*) OrderField varchar2, -- 排序字段(必须!支持多字段) whereCondition varchar2, -- 条件语句(不用加where) pageSize i…

Windows服务无法引用.dll的错误

项目中需要使用.NET开发Windows服务来检测MSMQ&#xff0c;但一直无法引用.dll(特别是.dll引用了其它的.dll)&#xff0c;最后google找到了答案&#xff1a; Every window service project, by default targets to .netClient version (which is not full version of .net and …

TC第一次成为room leader

虽然第二题竟然最后没通过system test&#xff0c;用递归的方法超时了 还好challenge 3个&#xff0c;以微弱优势胜过第二名 happy&#xff01; 继续努力转载于:https://www.cnblogs.com/fstang/archive/2012/12/21/2827345.html

[C/C++]BKDRHash

将字符串Hash成整型存储经常用到BKDRHash算法 uint64_t BKDRHash(const char *pszKey) {uint64_t seed 131;register uint64_t uCode0;while(pszKey[0]){uCode uCode *seed (unsigned char)pszKey[0];pszKey;}return uCode; }选择了64位的key&#xff0c;减少冲突的概率。转…

教你如何开发一个 SpringBoot starter

从前从前&#xff0c;有个面试官问我一个 SpringBoot Starter 的开发流程&#xff0c;我说我没有写过 starter&#xff0c;然后就没有然后了&#xff0c;面试官说我技术深度不够。 我想说这东西不是很简单吗&#xff0c;如果要自己写一个出来也是分分钟的事情。至于就因为我没…

nginx 搭建http协议拖动播放 FLV 视频播放服务器

原创作品&#xff0c;允许转载&#xff0c;转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://deidara.blog.51cto.com/400447/235562所需要的 播放器&#xff0c;我用的开源的 JW FLV Media Player我把我的上传到了blog 大家可以下载…

两分钟彻底让你明白Android Activity生命周期(图文)!

转&#xff1a;http://blog.csdn.net/qyf_5445/article/details/8290232 首先看一下Android api中所提供的Activity生命周期图(不明白的&#xff0c;可以看完整篇文章&#xff0c;在回头看一下这个图&#xff0c;你会明白的): Activity其实是继承了ApplicationContext这个类&am…

spring4和spring5的aop执行顺序区别?

spring4单切面 spring4多切面 spring4 spring5

jquery datepicker 点击日期控件不会自动更新input的值

页面代码&#xff1a;<link href"http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel"stylesheet" type"text/css"/> <link href"/static/css/main.css" rel"stylesheet" type"text/css"/…

ArcGIS API for Silverlight中legend控件显示图例问题

转自http://www.gisall.com/html/34/9534-5141.html 在使用ArcGIS API for Silverlight进行地图展示应用的时候&#xff0c;我们都会设置地图图层列表的图例&#xff08;该图例包含有图层名称和图层符号&#xff09;&#xff0c;但是在使用API时却出现了图例无法正常显示&#…

一个b+树库存放多少索引记录

MySQL中InnoDB页的大小默认是16k。也可以自己进行设置。&#xff08;计算机在存储数据的时候&#xff0c;最小存储单元是扇区&#xff0c;一个扇区的大小是 512 字节&#xff0c;而文件系统&#xff08;例如 XFS/EXT4&#xff09;最小单元是块&#xff0c;一个块的大小是 4KB。…

移动发布手机病毒警示信息 发现六种新型病毒

4月17日消息&#xff0c;近期&#xff0c;中国移动监测发现“伪系统杀毒”、“捆绑恶魔”和“伪软件管家”等六款新型手机病毒&#xff0c;造成客户后台自动联网、点播手机游戏类业务、发送垃圾短信并屏蔽10086短信提醒&#xff0c;严重侵害客户权益。 中国移动介绍&#xff0c…

检索函数retrieve

转载于:https://www.cnblogs.com/flowjacky/archive/2012/12/28/2836729.html

Android 安全机制概述

1 Android 安全机制概述 Android 是一个权限分离的系统 。 这是利用 Linux 已有的权限管理机制&#xff0c;通过为每一个 Application 分配不同的 uid 和 gid &#xff0c; 从而使得不同的 Application 之间的私有数据和访问&#xff08; native 以及 java 层通过这种 sandbox …

mysql8.0为啥移除查询缓存

1&#xff0c;对于经常更新的表缓存容易过期不容易控制 2&#xff0c;sql要完全一样才能命中缓存 3&#xff0c;为了节省空间 4&#xff0c;mysql缓存在分库分表的情况下是不起作用的 5&#xff0c;执行sql时候有触发器&#xff0c;自定义函数&#xff0c;缓存也是不起作用…

继承专题

【1】继承及访问权限 &#xff08;1&#xff09;理论知识 <1> 基类与派生类。基类、父类、超类均是指被继承的类&#xff1b;派生类、子类是指继承于基类&#xff08;父类、超类&#xff09;的类。 <2> 在C中使用冒号表示继承。如下代码片段&#xff1a; 1 class A…