This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Eclipse Collections Categorically: Level up your programming game and have Donald Raab on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Custom UIInput

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}


 
Sidd Nakade
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was able to fix it.
I was calling the super method in encodeEnd. That was creating the extra text field.
 
Pay attention! Tiny ad!
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic