//用servlet进行初始化
//在web.xml中配置<load-on-startup>3</load-on-startup>
/**
title=hello world
body=welcome
**/
import java.io.FileNotFoundException;
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 InitServlet2 extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
Properties pro = new Properties();
try {
ServletContext context = config.getServletContext();
InputStream in = context.getResourceAsStream("/WEB-INF/message.properties");
//pro.load(new FileInputStream ("message.properties"));
pro.load(in);
in.close();
//将properties对象放置到application范围内供其他类使用
context.setAttribute("ps", pro);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//自定义标签
package tag;
import java.util.Properties;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
public class MyTag2 extends TagSupport
{
private static final long serialVersionUID = 1L;
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@Override
public int doStartTag() throws JspException {
try {
Properties pro = (Properties)this.pageContext.getServletContext().getAttribute("ps");
String message = pro.getProperty(key);
this.pageContext.getOut().print(message);
}catch(Exception e) {
e.printStackTrace();
}
return EVAL_BODY_INCLUDE;
}
@Override
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
}
在WEB-INF下面定义:
myTag.tld文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.1</jsp-version>
<short-name>myTag</short-name>
<uri>/myTag</uri>
<tag>
<name>firstTag</name>
<tag-class>tag.MyTag</tag-class>
</tag>
<tag>
<name>message</name>
<tag-class>tag.MyTag2</tag-class>
<attribute>
<name>key</name>
<required>true</required>
</attribute>
</tag>
</taglib>
//在jsp页面中使用标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="bb" uri="/myTag"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyTag2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<bb:message key="title"></bb:message>
<bb:message key="body"></bb:message>
</body>
</html>
jsp自定义标签
最新推荐文章于 2023-06-25 23:32:20 发布