新疆web工程:
Java代码:
package com.tld.bean;
public class UserInfo {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
tag要继承TagSupport类
package com.tld;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import com.tld.bean.UserInfo;
public class UserInfoTag extends TagSupport {
private static final long serialVersionUID = 1L;
private UserInfo user;
@Override
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
@Override
public int doStartTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
if (user == null) {
out.println("No UserInfo Found...");
return SKIP_BODY;
}
out.println("<div>");
out.println(user.getUsername());
out.println(" ");
out.println(user.getAge());
out.println("</div>");
} catch (IOException e) {
throw new JspException(e.getMessage());
}
return SKIP_BODY;
}
@Override
public void release() {
super.release();
user = null;
}
public UserInfo getUser() {
return user;
}
public void setUser(UserInfo user) {
this.user = user;
}
}
编写tld文件,文件名随便取。
UserInfo.tld:
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>cc</short-name>
<uri>/UserInfo</uri>
<tag>
<name>showUserInfo</name>
<tag-class>com.tld.UserInfoTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>user</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
在web.xml里进行配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>tld</display-name>
<jsp-config>
<taglib>
<taglib-uri>/UserInfo</taglib-uri>
<taglib-location>/WEB-INF/UserInfo.tld</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
jsp里进行引用:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="/UserInfo" prefix="ui" %>
<%@ page import="com.tld.bean.UserInfo;" %>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<%
UserInfo userInfo = new UserInfo();
userInfo.setUsername("yang");
userInfo.setAge(18);
pageContext.setAttribute("user", userInfo);
%>
<ui:showUserInfo user="${pageScope.user }"/>
</body>
</html>
这样就可以了。