Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Sunday, December 2, 2007

java code for Logo Tag

package com.java2s;


///Custom tag
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TryCatchFinally;

/**
* This tag generates a thumbnail image using HTML img tag, next to text
* message. The user specifies the content of the message and the Heading level
* (i.e.,
*

-
*

)
*/

public class LogoTag extends BodyTagSupport implements TryCatchFinally {

private String heading = null;

private String image = null;

//stamp.gif, 42 x 54
private String width = null;

private String height = null;

public int doStartTag() throws JspException {

//this method assumes that attribute properties have been set.
try {

int h = new Integer(heading).intValue();

if (!(h > 0 && h < 7))
throw new JspException(
"The 'heading' attribute value must between 1 and 6 inclusive.");

} catch (Exception e) {
throw new JspException(e.getMessage());
}

return EVAL_BODY_BUFFERED;

}

public int doEndTag() throws JspException {

JspWriter out = pageContext.getOut();
String imgDir = ((HttpServletRequest) pageContext.getRequest())
.getContextPath()
+ "/images/";
String message = getBodyContent().getString().trim();
try {
out.println(new StringBuffer(" image).append("\" width=\"").append(width).append(
"\" height=\"").append(height).append("\" align=\"left\">")
.append("").append(message)
.append("
").toString());

} catch (java.io.IOException io) {
}

return EVAL_PAGE;
}

public void doCatch(Throwable t) {

try {

pageContext.getOut().println(t.getMessage() + "
");

} catch (java.io.IOException io) {
}
}

public void doFinally() {

//do nothing here, since we don't have any resources open
//like database connections

}

public void setHeading(String level) {

this.heading = level;

}

public void setImage(String name) {

this.image = name;

}

public void setWidth(String width) {

this.width = width;

}

public void setHeight(String height) {

this.height = height;

}

public void release() {

heading = null;
image = null;
width = null;
height = null;

}

}
//
package com.java2s;

import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
* This tag generates a thumbnail image using HTML img tag, next to text
* message. The user specifies the content of the message and the Heading level
* (i.e.,
*

-
*

)
*/

public class SimpleLogoTag extends SimpleTagSupport {

private String heading = null;

private String image = null;

private String width = null;

private String height = null;

public void doTag() throws JspException, IOException {

JspContext jspContext = getJspContext();

//this method assumes that attribute properties have been set.
try {

int h = new Integer(heading).intValue();

if (!(h > 0 && h < 7))
throw new JspException(
"The 'heading' attribute value must between 1 and 6 inclusive.");

} catch (Exception e) {
throw new JspException(e.getMessage());
}

JspWriter out = jspContext.getOut();

String imgDir = (String) jspContext.findAttribute("imgDir");

if (imgDir == null || "".equals(imgDir))
throw new JspException(
"No attribute provided specifying the application's image directory.");

out.println(new StringBuffer(" .append(image).append("\" width=\"").append(width).append(
"\" height=\"").append(height).append(
"\" align=\"left\">").append(" .append(">").toString());
getJspBody().invoke(null);
out.println(new StringBuffer("")
.toString());

}

public void setHeading(String level) {

this.heading = level;

}

public void setImage(String name) {

this.image = name;

}

public void setWidth(String width) {

this.width = width;

}

public void setHeight(String height) {

this.height = height;

}

}
//logo.tag
<%@ tag body-content="scriptless" description="Writes the HTML code for inserting a logo." %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ attribute name="heading" required="true" rtexprvalue=
"true" description="The heading level for the logo."%>

<%@ attribute name="image" required="true" rtexprvalue=
"true" description="The image name for the logo."%>

<%@ attribute name="width" required="true" rtexprvalue=
"true" description="The image width for the logo."%>

<%@ attribute name="height" required="true" rtexprvalue=
"true" description="The image height for the logo."%>

"${width}" height="${height}" align="left">




//logoTest.jsp
<%@ taglib uri="java2s.com.tags" prefix="cbck" %>



<% session.setAttribute("imgDir",(request.getContextPath() + "/images/")) %>
Thanks for visiting

Here's all the other stuff this page contains...



//myTag.tld




1.0
2.0
cbck
java2s.com.tags
Cookbook custom tags


com.java2s.ReqListener



logo
com.java2s.LogoTag
scriptless
This tag writes a logo inside the JSP.

heading
true
true
The heading level for the logo; 1 through 6.



image
true
true
The image name for the logo.



width
true
true
The image width for the logo.



height
true
true
The image height for the logo.



No comments: