您的位置:首页 > Web前端 > JavaScript

JSP 自定义标签

2017-12-29 21:08 387 查看
WEB-INF/tags/date.tag

<%@ tag import="java.util.Date" import="java.text.DateFormat"%>
<%
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
Date now = new Date(System.currentTimeMillis());
out.println(dateFormat.format(now));
%>


WEB-INF/tags/text.tag

<%@ attribute name="input" required="true" %>
<%!
//定义方法
private String encodeHtmlTag(String tag) {
if (tag==null)
return null;
int length = tag.length();
StringBuffer encodedTag = new StringBuffer(2 * length);
for (int i=0; i<length; i++) {
char c = tag.charAt(i);
if (c=='<')
encodedTag.append("<");
else if (c=='>')
encodedTag.append(">");
else
encodedTag.append(c);

}
return encodedTag.toString();
}
%>
<%--根据传入的参数input调用方法encodeHtmlTag--%>
<%=encodeHtmlTag(input)%>
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%--引入自定义标签库--%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="t"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%--使用自定义标签--%>
<t:date/>
<t:text input="<br/><font color='red'>means changing line</font>"></t:text>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: