Bom dia pessoal,
Estou com um problema em JSF ao tentar criar uma tag customizada.
Criei a minha classe estendendo UIComponentBase e coloquei o atributo: “value”.
Por padrão o atributo “id” já existe, e para recupera-lo é simples.
Mas quando tento recuperar o valor de “value” da minha tag, ele sempre aparece como nulo, mesmo estando preenchido no meu xhtml.
Segui alguns tutoriais da internet e consegui fazer a maior parte, a única coisa que falta é mesmo recuperar os valores dos atributos.
Configurei os seguintes arquivos:
[list]web.xml[/list]
[list]minhatag.taglib.xml[/list]
[list]faces-config.xml[/list]
Abaixo segue o código da minha classe que estende a UIComponentBase e que monta o componente da maneira que eu quero.
package teste.taglib;
import java.io.IOException;
import javax.faces.application.Application;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
@FacesComponent("teste.taglib.DateSelectTag")
public class DateSelectTag extends UIComponentBase {
public static final String COMPONENT_TYPE = "teste.taglib";
public static final String COMPONENT_FAMILY = "teste.taglib";
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String getFamily() {
return COMPONENT_FAMILY;
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
Application app = context.getApplication();
String valor = (String)app.evaluateExpressionGet(context, getAttributes().get("value").toString(), String.class);
writer.startElement("input", this);
writer.writeAttribute("id", this.getId(), null);
writer.writeAttribute("value", valor, null);
writer.writeAttribute("type", "text", null);
writer.endElement("input");
writer.startElement("script", this);
writer.writeAttribute("id", getId() + "_s", null);
writer.writeAttribute("type", "text/javascript", null);
writer.write("$(function() {");
String separatorChar = Character.toString(UINamingContainer.getSeparatorChar(context));
String componentIdSelector = "'#" + getId().replace(separatorChar, "\\\\:") + "'";
writer.write("$(" + componentIdSelector + ").datepicker();");
writer.write("});");
writer.endElement("script");
super.encodeBegin(context);
}
}
Outra coisa, eu sei que já existem diversas taglibs que provavelmente já fazem o que o meu exemplo acima faz, mas no momento preciso aprender como funciona pra poder criar outras tags q precisarei futuramente.
No mais, agradeço se alguém tiver como me ajudar.
[]'s