aspose file tools*
The moose likes JSF and the fly likes How to create BINDING of command link with backing bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to create BINDING of command link with backing bean" Watch "How to create BINDING of command link with backing bean" New topic
Author

How to create BINDING of command link with backing bean

ram gaurav
Ranch Hand

Joined: Mar 29, 2006
Posts: 208
Hi

How to create BINDING of command link with backing bean

Thanks
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
h:commandLink is an UICommand � HtmlCommandLink component. Check the API doc.
Richard Blaha
Ranch Hand

Joined: Dec 13, 2007
Posts: 34
Okay. A few days have passed since that advice was given. I am working with JSF and Java 1.6.0_14 using Netbeans 6.7.1. I am building my HtmlPanelGrid dynamically in the following way.

public void addItemNumbers(ValueChangeEvent changeEvent)
{
FacesContext facesContext = FacesContext.getCurrentInstance();
Application application =
facesContext.getApplication();
List<UIComponent> children = itemPanel.getChildren();
children.clear();
srchDesc= "";
srchItemno = (String)changeEvent.getNewValue();
String[][] itemArray = getItemsByItemno( srchItemno);

HtmlCommandLink outputItemno;
UIParameter id;

for ( int count = 0; count < itemArray.length; count++)
{
// ID REQUIRED
id = new UIParameter();
id.setName("id" + itemArray[count][0]);
id.setValue( itemArray[count][0]);
// ITEM NUMBER
outputItemno = (HtmlCommandLink) application.createComponent( HtmlCommandLink.COMPONENT_TYPE);
outputItemno.setValue( itemArray[count][0]);
String action = "#{ itemcountbean.listSetup}";
MethodExpression methodExpression =
FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
createMethodExpression(FacesContext.getCurrentInstance().getELContext(), action, String.class,
new Class<?>[0]);
outputItemno.setActionExpression( methodExpression);
ValueExpression valueEx =
application.getExpressionFactory().createValueExpression(facesContext. getELContext(),
"#{itemcountbean.itemno}", String.class);
outputItemno.setValueExpression( itemArray[count][0], valueEx );
outputItemno.setStyle( "color: blue");
outputItemno.getChildren().add(id);
children.add( outputItemno);

// ITEM DESCRIPTION
HtmlOutputText outputItemDesc = (HtmlOutputText) application
.createComponent( HtmlOutputText.COMPONENT_TYPE);
outputItemDesc.setValue( itemArray[count][1]);
outputItemDesc.setStyle( "color: blue");
outputItemDesc.getChildren().add(id);
children.add( outputItemDesc);
}
logger.log( Level.FINER, "HtmlCommandLink item panel populated");
}


This is displayed by the JSP page with the following code:


<h:panelGrid id="itemPanel"
binding="#{ item.itemPanel}"
columns="2" border="1" cellspacing="5" />



The itemcountbean has the following coding in itemcountbean:

public String listSetup() {
logger.log( Level.INFO, "itemcountbean.listSetup");
logger.log( Level.INFO, "itemcountbean.itemno ["+this.itemno+"]");
...
return "itemcount_list";
}

Everything appears to be working except transferring via the 'binding' that is suppose to occur using the

ValueExpression valueEx =
application.getExpressionFactory().createValueExpression(facesContext. getELContext(),
"#{itemcountbean.itemno}", String.class);
outputItemno.setValueExpression( itemArray[count][0], valueEx );

part of the code.

And my
logger.log( Level.INFO, "itemcountbean.listSetup");
logger.log( Level.INFO, "itemcountbean.itemno ["+this.itemno+"]");

log returns

itemcountbean.listSetup
itemcountbean.itemno [null]

I have read through the JavaDocs to attempt to understand establishing the binding of ' #{itemcountbean.itemno} ' in the correct manner. I have searched dozens of sites trying to determine my error(s). Some advice was as little as 'Read the JavaDocs', but most was on binding before the JSF and/or NBs requiring use of the ValueExpression and setValueExpression. Most of the answers are 2-3 years old.

Would someone please help me in completion of binding/transferring my value from my HtmlCommandLink to be used in the backing bean?

Thank you.
rdb

Richard Blaha
Ranch Hand

Joined: Dec 13, 2007
Posts: 34

OK. Here is what I found and what works. This is complete replacement code for what I presented in the previous post. This code is simpler and easier once I learned how to do it. I learned there is a huge difference between the introductory JSF code in chapter 1 and the Integrating of Data Grids in chapter 10 of JavaServerFaces In Action (Manning C 2005). Even in reading and using these examples (the first got me facing the problem I did and the second suggested I change my method) it still did not answer the question I needed answered. I ended up copying from a previous Visual ICEFaces project I had succesfully built. The final and key answer to my question I got from a blog I found searching for 'HtmlOutputText binding'. Passing component attributes from JSF to backing beans Again I have implemented all in the following replacement code.

itembean:

private String itemDataTableArray[][];


public void addItemNumbers(ValueChangeEvent changeEvent)
{
srchDesc= "";
srchItemno = (String)changeEvent.getNewValue();
itemDataTableArray = getItemsByItemno( srchItemno);
}

...

private HtmlDataTable itemDataTable = new HtmlDataTable();

public HtmlDataTable getItemDataTable() {
return itemDataTable;
}

public void setItemDataTable(HtmlDataTable hdt) {
this.itemDataTable = hdt;
}

private UIColumn column1 = new UIColumn();

public UIColumn getColumn1() {
return column1;
}

public void setColumn1(UIColumn uic) {
this.column1 = uic;
}
private UIColumn column2 = new UIColumn();

public UIColumn getColumn2() {
return column2;
}

public void setColumn2(UIColumn uic) {
this.column2 = uic;
}



This is displayed by the JSP page with the following code:


<h:dataTable id="itemDataTable" var="currentRow" styleClass="list_form" rowClasses="odd_row, even_row" border="1" cellspacing="5" binding="#{itembean.itemDataTable}" value="#{itembean.itemDataTableArray}">
<h:column binding="#{itembean.column1}" id="column1">
<f:facet name="header">
<hutputText value="Item Number" />
</f:facet>
<h:commandLink action="#{itemcountbean.listSetup}">
<hutputText id="outputText1" binding="#{itemcountbean.itemnoHtmlOutputText}" value="#{currentRow[0]}" >
<f:attribute name="itemText" value="#{currentRow[0]}" />
</hutputText>
</h:commandLink>
</h:column>
<h:column binding="#{itembean.column2}" id="column2">
<f:facet name="header">
<hutputText value="Item Desc" />
</f:facet>
<hutputText id="outputText2" value="#{currentRow[1]}" />
</h:column>
</h:dataTable>



The itemcountbean has the following coding in itemcountbean:

private String item = null;
private HtmlOutputText itemHtmlOutputText;

...

/**
* @return the itemHtmlOutputText
*/
public HtmlOutputText getItemHtmlOutputText() {
return itemHtmlOutputText;
}

/**
* @return the itemHtmlOutputText
*/
public String getItemHtmlOutputTextValue() {
return (String) itemHtmlOutputText.getAttributes().get("itemText");
}

/**
* @param itemHtmlOutputText the itemHtmlOutputText to set
*/
public void setitemHtmlOutputText(HtmlOutputText itemHtmlOutputText) {
this.itemHtmlOutputText = itemHtmlOutputText;
}


// Setup methods
public String listSetup() {
item = getItemHtmlOutputTextValue();
return "itemcount_itemlist";
}





 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create BINDING of command link with backing bean