taglib


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
 <servlet>
   <servlet-name>hello</servlet-name>
   <servlet-class>com.example.part1</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>hello</servlet-name>
   <url-pattern>/servlet/hello</url-pattern>
 </servlet-mapping>
 <jsp-config>
   <taglib>
     <taglib-uri>http://www.javaroad.jp/tags/br</taglib-uri>
     <taglib-location>/WEB-INF/br.tld</taglib-location>
   </taglib>
 </jsp-config>
</web-app>

br.tld
<?xml version="1.0" ?>
<taglib 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"
       version="2.0">
 <tlib-version>1.0</tlib-version>
 <jsp-version>2.0</jsp-version>
 <short-name>brTag</short-name>
 <tag>
   <name>brTag</name>
   <tag-class>com.example.part1.BrTag</tag-class>
   <body-content>empty</body-content>
   <attribute>
     <name>name</name>
     <required>false</required>
     <rtexprvalue>true</rtexprvalue>
   </attribute>
 </tag>
</taglib>

BrTag.java
package com.example.part1;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class BrTag implements Tag {
private PageContext pageContext;
private Tag parentTag;
private String name;
public void setPageContext(PageContext pageContext) {
	this.pageContext = pageContext;
}
public void setParent(Tag parentTag) {
	this.parentTag = parentTag;
}
public Tag getParent() {
	return this.parentTag;
}
public void setName(String name) {
	this.name = name;
}
public int doStartTag() throws JspException {
	try {
		JspWriter out = pageContext.getOut();
		out.print("Hello " + name);
	} catch (Exception e) {
		throw new JspException(e.getMessage());
	}
	return SKIP_BODY;
}
public int doEndTag() throws JspException {
	return EVAL_PAGE;
}
public void release() {
}
} 

JSP
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.javaroad.jp/tags/br" prefix="br" %>
</html>
<head></head>
<body>
 <br>
 <br:brTag name="java" />
 <br>
</body>
</html>