Getting ClassCastException with JSF 1.2 Custom Component and BEA 10.3
Posted
by Tobi
on Stack Overflow
See other posts from Stack Overflow
or by Tobi
Published on 2010-06-11T12:47:09Z
Indexed on
2010/06/12
15:42 UTC
Read the original article
Hit count: 430
Im getting a ClassCastException if i use Attributes in my Custom Headline Tag. Without Attributes rendering works fine.
Calling <t:headline value="test" />
gives a ClassCastException even before a Method in my HeadlineComponent or HeadlineTag-Class is called. <t:headline />
works fine.
I'm using MyFaces-1.2, on BEA 10.3
default.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@ taglib prefix="t" uri="http://www.tobi.de/taglibrary" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Tobi Test</title>
</head>
<body>
<f:view>
<t:headline value="test" />
</f:view>
</body>
</html>
HeadlineComponent.java
package tobi.web.component.headline;
import java.io.IOException;
import javax.el.ValueExpression;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
public class HeadlineComponent extends UIOutput {
private String value;
private Integer size;
@Override
public Object saveState(FacesContext context) {
Object values[] = new Object[3];
values[0] = super.saveState(context);
values[1] = value;
values[2] = size;
return ((Object)(values));
}
@Override
public void restoreState(FacesContext context, Object state) {
Object values[] = (Object[])state;
super.restoreState(context, values[0]);
value = (String)values[1];
size = (Integer)values[2];
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
// Wenn keine Groesse angegeben wurde default 3
String htmlTag = (size == null) ? "h3" : "h"+getSize().toString();
ResponseWriter writer = context.getResponseWriter();
writer.startElement(htmlTag, this);
if(value == null) {
writer.write("");
} else {
writer.write(value);
}
writer.endElement(htmlTag);
writer.flush();
}
public String getValue() {
if(value != null) {
return value;
}
ValueExpression ve = getValueExpression("value");
if(ve != null) {
return (String)ve.getValue(getFacesContext().getELContext());
}
return null;
}
public void setValue(String value) {
this.value = value;
}
public Integer getSize() {
if(size != null) {
return size;
}
ValueExpression ve = getValueExpression("size");
if(ve != null) {
return (Integer)ve.getValue(getFacesContext().getELContext());
}
return null;
}
public void setSize(Integer size) {
if(size>6) size = 6;
if(size<1) size = 1;
this.size = size;
}
}
HeadlineTag.java
package tobi.web.component.headline;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentELTag;
public class HeadlineTag extends UIComponentELTag {
private ValueExpression value;
private ValueExpression size;
@Override
public String getComponentType() {
return "tobi.headline";
}
@Override
public String getRendererType() {
// null, da wir hier keinen eigenen Render benutzen
return null;
}
protected void setProperties(UIComponent component) {
super.setProperties(component);
HeadlineComponent headline = (HeadlineComponent)component;
if(value != null) {
if(value.isLiteralText()) {
headline.getAttributes().put("value", value.getExpressionString());
} else {
headline.setValueExpression("value", value);
}
}
if(size != null) {
if(size.isLiteralText()) {
headline.getAttributes().put("size", size.getExpressionString());
} else {
headline.setValueExpression("size", size);
}
}
}
@Override
public void release() {
super.release();
this.value = null;
this.size = null;
}
public ValueExpression getValue() {
return value;
}
public void setValue(ValueExpression value) {
this.value = value;
}
public ValueExpression getSize() {
return size;
}
public void setSize(ValueExpression size) {
this.size = size;
}
}
taglibrary.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib
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-jsptaglibrary_2_1.xsd"
version="2.1">
<description>Tobi Webclient Taglibrary</description>
<tlib-version>1.0</tlib-version>
<short-name>tobi-taglibrary</short-name>
<uri>http://www.tobi.de/taglibrary</uri>
<tag>
<description>Eine Überschrift im HTML-Stil</description>
<name>headline</name>
<tag-class>tobi.web.component.headline.HeadlineTag</tag-class>
<body-content>empty</body-content>
<attribute>
<description>Der Text der Überschrift</description>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>Die Größe der Überschrift nach HTML (h1 - h6)</description>
<name>size</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
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"
version="1.2">
<component>
<description>Erzeugt eine Überschrift nach HTML-Stil</description>
<display-name>headline</display-name>
<component-type>tobi.headline</component-type>
<component-class>tobi.web.component.headline.HeadlineComponent</component-class>
<attribute>
<attribute-name>value</attribute-name>
<attribute-class>java.lang.String</attribute-class>
</attribute>
<attribute>
<attribute-name>size</attribute-name>
<attribute-class>java.lang.Integer</attribute-class>
<default-value>3</default-value>
</attribute>
</component>
</faces-config>
© Stack Overflow or respective owner