Friday, February 16, 2007

moving it here
http://nerdynotes.wordpress.com/


Marquee in HTML is defined using the marquee tag


<marquee>hello world!</marquee>

We can define marquee inside the JSF page within verbatain tag,

<f:verbatim><marquee>hello world!</marquee></f:verbatim>

which is a mixup of html and jsf tags. If we don't want to mix html and JSF together , the only option is creating a custom component which replaces the above with

<mycomp:marquee value="Hello World !!" />

cool huh ? :)


Creating a JSF component is simple and straight forward, hoping that you have basic understanding about JSF and web environment. For developing a custom component in JSF all we need to have is

1. Component class.
2. Tag class.
3. Renederer ( optional ) . I am not using the Renderer for this component
4. Define a TLD file for the component tags.
5. Register the component in faces-config.

Component Class

package com.mycomponent;



import java.io.IOException;
import javax.faces.component.UIComponentBase;

import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

public class Marquee extends UIComponentBase {

private String value;

public String getFamily() {
return null;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public void encodeBegin(FacesContext context) throws IOException {

ResponseWriter writer = context.getResponseWriter();
writer.startElement("marquee",this);
writer.write(getValue());
writer.endElement("marquee");
}
}


Tag Class

package com.mycomponent;

import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentTag;


public class MarqueeTag extends UIComponentTag {

private String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public MarqueeTag() {
super();
}

public String getComponentType() {
// this String constant should match the component-type defined in faces-config.xml
return "com.mycomponent.MarqueeComp";
}

public String getRendererType() {
return null;
}

protected void setProperties(UIComponent component) {

super.setProperties(component);
Marquee marquee = (Marquee) component;

if(null != value){
marquee.setValue(value);
}
}

}


TLD file for the marquee component


Give this a name (marquee.tld) and keep the file inside WEB-INF folder

<!DOCTYPE taglib PUBLIC"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN""http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">


<taglib> <tlib-version>
1.0</tlib-version>

<jsp-version>
1.2</jsp-version>
<uri>
/jsf-marquee</uri>
<tag>
<name>
marquee</name>
<tag-class>
com.mycomponent.MarqueeTag</tag-class>
<body-content>
empty</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag></taglib>


faces-config.xml

Edit your faces-config.xml to register the new component.

<component>
<component-type>com.mycomponent.MarqueeComp</component-type>

<component-class>com.mycomponent.Marquee</component-class>
<property>
<property-name>value</property-name>
<property-class>String</property-class>
</property>

</component>

JSP

I'm using XML markup for my jsp , if you are using HTML markup, the syntax for defining the marquee namespace will be different.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:f="http://java.sun.com/jsf/core" version="2.0"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:mycomp="/jsf-marquee" // this should match the uri defined in TLD file
xmlns:hx=
http://www.ibm.com/jsf/html_extended>



<comp:marquee value="Hello World !!" />

That's it !! In-case you are stuck at somewhere let me know, i would be happy to help you out ..

Happy coding !