Hello All.
I am trying to build a custom UIComponent as per the example in the given link :
http://www.theserverside.com/tt/articles/article.tss?l=BuildingCustomJSF.
The example is working but it is creating an extra text field [and a hidden field] in the end.
I think I am missing something from the basic architecture of JSF.
Below is the code
==
public class HtmlInputUI extends UIInput
{
public void encodeBegin(FacesContext context) throws IOException
{
ResponseWriter writer = context.getResponseWriter();
String clientId = getClientId(context);
encodeInputField(writer, clientId+".inputFld");
encodeSubmit(writer, clientId+".submitBtn");
encodeOutputText(context);
}
public void encodeEnd(FacesContext context) throws IOException {
super.encodeEnd(context);
}
public void decode(FacesContext context) {
super.decode(context);
String clientId = getClientId(context);
Map requestMap = context.getExternalContext().getRequestParameterMap();
String submitted_Value = (String) requestMap.get(clientId+".inputFld");
setSubmittedValue(submitted_Value);
setValid(true);
}
private void encodeInputField(ResponseWriter writer, String clientId) throws IOException {
writer.startElement("input", this);
writer.writeAttribute("type", "text", null);
writer.writeAttribute("name", clientId, "clientId");
Object v = getValue();
if (v != null)
writer.writeAttribute("value", v.toString(), "value");
writer.writeAttribute("size", "6", null);
writer.endElement("input");
}
private void encodeSubmit(ResponseWriter writer, String clientId) throws IOException {
writer.startElement("input", this);
writer.writeAttribute("type", "submit", null);
writer.writeAttribute("name", clientId, "clientId");
writer.writeAttribute("value", "Get Salary", null);
writer.endElement("input");
}
private void encodeOutputText(FacesContext context) throws IOException
{
ResponseWriter writer = context.getResponseWriter();
String designation = (String)getAttributes().get("value");
String salary = "0.00";// For designation get the salary
if(!StringUtils.isEmpty(designation)){
try
{
SalaryServiceService salaServLoc = new SalaryServiceServiceLocator();
SalaryService salaServ = salaServLoc.getSalaryService();
salary = ""+salaServ.getSalary(designation, 1);
}catch (Exception e) {
salary = "";
}
}
writer.startElement("p", this);
writer.writeText("The current Salary for " + designation + " is: " + salary+ ".", null);
writer.endElement("p");
}
public String getFamily() {
return super.getFamily();
}
}
=====
public class HtmlInputUITag extends UIComponentTag {
public String designation = null;
public String getComponentType() {
return "htmlInputUI";
}
public String getRendererType() {
return null;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
protected void setProperties(UIComponent component)
{
super.setProperties(component);
if (designation!= null)
{
if (isValueReference(designation))
{
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
ValueBinding vb = app.createValueBinding(designation);
component.setValueBinding("designation", vb);
}
else
component.getAttributes().put("designation", designation);
}
}
public void release()
{
super.release();
designation = null;
}
}