custom component

Hi

I am trying to learn how to build custom components and running into problem while accessing the attribute hellomsg as defined in this file HtmlHelloWorld1.java which extends the UIComponentBase. The attribute hellomsg is always null. Here are my files

Base class extension

package com.custComponent;

import java.io.IOException;

import javax.el.ValueExpression;

import javax.faces.component.UIComponentBase;

import javax.faces.context.FacesContext;

import javax.faces.context.ResponseWriter;

public class HtmlHelloWorld1 extends UIComponentBase {

/** general catogory of which this component is a member e.g. UIInput comp/javax.faces.Input family **/

public String getFamily() {

return null;

}

/** Embedded renderer in this case: renders itself to client **/

public void encodeBegin(FacesContext context) throws IOException {

ResponseWriter writer = context.getResponseWriter();

String hellomsg = (String) getAttributes().get("hellomsg");

writer = context.getResponseWriter();

writer.startElement("div", null);

writer.writeAttribute("style", "color : red", null );

writer.writeText(hellomsg + " HelloWorld! today is: ",null);

writer.endElement("div");

}

}

--

Here is my tag file

--java tag file--

package com.tag;

import javax.el.ELContext;

import javax.el.ExpressionFactory;

import javax.el.ValueExpression;

import javax.faces.application.Application;

import javax.faces.component.UIComponent;

import javax.faces.context.FacesContext;

import javax.faces.webapp.UIComponentELTag;

public class HelloWorldTag1 extends UIComponentELTag {

private String hellomsg=null;

public String getComponentType() {

// Associates tag with UI component registered in faces-config.xml

return "HtmlHelloWorld1";

}

public String getRendererType() {

// Since the renderer is embedded in the component, can return null.

return null;

}

public int getIndexOfNextChildTag() {

return 0;

}

protected void setProperties(UIComponent component) {

// This call to super.setProperties() is VITALLY IMPORTANT!

// omitting it will cause the component to break entirely!

super.setProperties(component);

FacesContext context = FacesContext.getCurrentInstance();

ELContext elContext = context.getELContext();

Application app = context.getApplication();

try {

ExpressionFactory ef = app.getExpressionFactory();

ValueExpression vb = ef.createValueExpression(elContext, getHellomsg(), String.class);

component.setValueExpression(getHellomsg(), vb);

} catch (Exception ex) {

}

}

public String getHellomsg(){

return hellomsg;

}

public void setHellomsg(String hellomsg) {

this.hellomsg = hellomsg;

}

}

-

-taglib descriptor file

<?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>

<short-name>htmlhelloworld1</short-name>

<uri>/WEB-INF/tlds/HtmlHellWorld1</uri>

<tag>

<description>Hello World1 Tag</description>

<name>helloworld1</name>

<tag-class>

com.tag.HelloWorldTag1

</tag-class>

<body-content>empty</body-content>

<variable>

<name-from-attribute>hellomsg</name-from-attribute>

</variable>

<attribute>

<description>a custom hello message</description>

<name>hellomsg</name>

<required>true</required>

<deferred-value>

<type>java.lang.String</type>

</deferred-value>

</attribute>

</tag>

</taglib>

part of index.jsp file creates faces context-

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<h1>JSP Page</h1>

<a href="./faces/welcomeJSF.jsp">JavaServer Faces Welcome Page</a>

<jsp:forward page="/faces/HelloWorld1.jsp" />

</body>

</html>

--

--HelloWorld1.jsp file where I am using the component--

<%@ page contentType="text/html"%>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<%@ taglib uri="/WEB-INF/tlds/HtmlHellWorld1.tld" prefix="jcr"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<h1>JSP Page</h1>

<f:view>

<html>

<head>

<title>hello</title>

</head>

<body>

<h2>Custom Helloworld component</h2>

<jcr:helloworld1 hellomsg="Hello JSF!"/>

</body>

</html>

</f:view>

</body>

</html>

--

--faces-config file

<faces-config version="1.2"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

<component>

<component-type>HtmlHelloWorld1</component-type>

<component-class>

com.custComponent.HtmlHelloWorld1

</component-class>

</component>

</faces-config>

finally web.xml file -

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>

<param-name>com.sun.faces.verifyObjects</param-name>

<param-value>false</param-value>

</context-param>

<context-param>

<param-name>com.sun.faces.validateXml</param-name>

<param-value>true</param-value>

</context-param>

<context-param>

<param-name>javax.faces.STATE_SAVING_METHOD</param-name>

<param-value>client</param-value>

</context-param>

<servlet>

<servlet-name>Faces Servlet</servlet-name>

<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>/faces/*</url-pattern>

</servlet-mapping>

<session-config>

<session-timeout>

30

</session-timeout>

</session-config>

<welcome-file-list>

<welcome-file>

index.jsp

</welcome-file>

</welcome-file-list>

</web-app>

[7627 byte] By [kochnama] at [2007-11-26 18:37:39]
# 1

I could not understand your setProperties mathod, its should be look like this:

protected void setProperties( UIComponent component )

{

super.setProperties( component );

setValueBinding( component, ATTRIBUTE_NAME, "attribute value" );

}

public void setValueBinding( UIComponent component, String attributeName, String attributeValue )

{

if( attributeValue != null )

{

if( UIComponentTag.isValueReference(attributeValue) )

{

FacesContext context = FacesContext.getCurrentInstance();

Application app = context.getApplication();

ValueBinding vb = app.createValueBinding( attributeValue );

component.setValueBinding( attributeName, vb );

//System.out.println( "vb: " + vb.getValue(FacesContext.getCurrentInstance()) );

}

else

{

component.getAttributes().put( attributeName, attributeValue );

}

}

}

Y_NOTa at 2007-7-9 6:11:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for the reply.

I can not use UIComponentTag or isValueReference because they are deprecated in netbean 5.5 which I am using. I need to use UIComponentELtag or UIComponentTagBase. Also setValueBinding is deprecated and is replaced by setValueExpression. Any other ideas. Thanks again.

kochnama at 2007-7-9 6:11:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...