Java Tutorial/JSP/Custom Tag
Содержание
- 1 Custom Tag Accepting Fragments
- 2 Custom Tag As Template
- 3 Custom Tag Dynamic Attributes
- 4 Custom Tag For Display message
- 5 Custom Tag For Iteration
- 6 Custom Tag For Outputing Message To Console
- 7 Custom Tag For Shuffling Color
- 8 Custom Tag Set Bean Value
- 9 Custom Tag Setup Environment
- 10 Custom Tag Support
- 11 Custom Tag To Get Parameter From PageContext
- 12 Custom Tag To Insert Text
- 13 Custom Tag Using Beans
- 14 Custom Tag Web URI Definition
- 15 Custom Tag Web XML Tag Lib
- 16 Custom Tag With Attributes
- 17 Custom Tag Without TLD
- 18 Custom Tag With Property
- 19 Extends Tag Support DoEndTag
- 20 Jars For Custom Tag Development
- 21 Reference Tag Using Tag Dir
- 22 Repeat Tag
Custom Tag Accepting Fragments
Custom Tag As Template
Custom Tag Dynamic Attributes
Custom Tag For Display message
Custom Tag For Iteration
MyTag.java
package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport
{
private int counter = 0;
private String[] names = null;
public int doStartTag()
{
names = (String[]) pageContext.getAttribute("names");
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException
{
try{
pageContext.getOut().print(" " + names[counter] + "<BR>");
} catch(Exception e){
throw new JspException(e.toString());
}
counter++;
if(counter >= names.length) {
return SKIP_BODY;
}
return EVAL_BODY_AGAIN;
}
}
Custom Tag For Outputing Message To Console
MyTag.java
package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport
{
public int doStartTag() throws JspException
{
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException
{
System.out.println("Hello!");
return SKIP_BODY;
}
}
Custom Tag For Shuffling Color
Custom Tag Set Bean Value
Custom Tag Setup Environment
Custom Tag Support
HelloWorld.java
package beans;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
public class HelloWorld extends TagSupport
{
public int doStartTag() throws JspException
{
try
{
JspWriter out = pageContext.getOut();
HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
out.write("Hello world!");
}
catch(Exception e)
{
throw new JspException(e.getMessage());
}
return EVAL_PAGE;
}
}
Custom Tag To Get Parameter From PageContext
MyTag.java
package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport
{
private int counter = 0;
private String[] names = null;
public int doStartTag()
{
names = (String[]) pageContext.getAttribute("names");
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException
{
try{
pageContext.getOut().print(" " + names[counter] + "<BR>");
} catch(Exception e){
throw new JspException(e.toString());
}
counter++;
if(counter >= names.length) {
return SKIP_BODY;
}
return EVAL_BODY_AGAIN;
}
}
Custom Tag To Insert Text
MyTag.java
package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport
{
public int doEndTag() throws JspException {
try {
pageContext.getOut().print("Hello from JSP!");
} catch (Exception e) {
throw new JspException(e.toString());
}
return EVAL_PAGE;
}
}
Custom Tag Using Beans
Custom Tag Web URI Definition
Custom Tag Web XML Tag Lib
HelloWorld.java
package beans;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
public class HelloWorld extends TagSupport
{
public int doStartTag() throws JspException
{
try
{
JspWriter out = pageContext.getOut();
HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
out.write("Hello world!");
}
catch(Exception e)
{
throw new JspException(e.getMessage());
}
return EVAL_PAGE;
}
}
Custom Tag With Attributes
Custom Tag Without TLD
Custom Tag With Property
MyTag.java
package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport
{
String text;
public void setText(String s) {
text = s;
}
public int doEndTag() throws JspException {
try {
pageContext.getOut().print(text);
} catch (Exception e) {
throw new JspException(e.toString());
}
return EVAL_PAGE;
}
}
Extends Tag Support DoEndTag
MyTag.java
package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport
{
public int doEndTag() throws JspException {
try {
pageContext.getOut().print("Hello from JSP!");
} catch (Exception e) {
throw new JspException(e.toString());
}
return EVAL_PAGE;
}
}
Jars For Custom Tag Development
MyTag
package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport
{
public int doStartTag() throws JspException
{
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException
{
System.out.println("Hello!");
return SKIP_BODY;
}
}
Reference Tag Using Tag Dir
Jsp page with custom tag
<%@ taglib prefix="myTag" tagdir="/WEB-INF/tags" %>
<html>
<head>
<title>EL Type Conversion Examples</title>
</head>
<body>
<h1>EL Type Conversion Examples</h1>
<myTag:CharacterType val="this is it"/>
<myTag:CharacterType val="8"/>
<myTag:CharacterType val="3.0001"/>
<br/>
<myTag:DoubleType val=""/>
<myTag:DoubleType val="-3"/>
<br/>
<myTag:BooleanType val=""/>
<myTag:BooleanType val="true"/>
<myTag:BooleanType val="t"/>
<myTag:BooleanType val="3.1"/>
</body>
</html>
Repeat Tag
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package beans;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.HashMap;
import java.io.IOException;
/**
* SimpleTag handler that accepts a num attribute and
* invokes its body "num" times.
*/
public class RepeatSimpleTag extends SimpleTagSupport {
private int num;
public void doTag() throws JspException, IOException {
for (int i=0; i<num; i++) {
getJspContext().setAttribute("count", String.valueOf( i + 1 ) );
getJspBody().invoke(null);
}
}
public void setNum(int num) {
this.num = num;
}
}