• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does one read the value associated with the HtmlCommandLink in the Action Method?  RSS feed

 
Richard Blaha
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been reading JavaServer Faces In Action without being able to find the answer to this question. I have searched the web for any help. I cannot find a simple answer to creating an HtmlCommand link for a table:

// ID REQUIRED
UIParameter id = new UIParameter();
id.setName("id");
id.setValue( numListArray[count][0]);

HtmlCommandLink link = (HtmlCommandLink) application.createComponent( HtmlCommandLink.COMPONENT_TYPE);
link.setValue( numListArray[count][0]);
String action = "#{ bean.listSetup}";
MethodExpression methodExpression =
FacesContext.getCurrentInstance().getApplication().getExpressionFactory().
createMethodExpression(FacesContext.getCurrentInstance().getELContext(), action, String.class,
new Class<?>[0]);
link.setActionExpression( methodExpression);
link.setStyle( "color: blue");
link.getChildren().add(id);
children.add( link);

Here I have a UIParameter id with a name and value. I have a link action of '#{bean.listSetup}' and link value assigned.

In the action I have

public String listSetup()
{
String numEntry = ? // I can find nothing to show me how to retrieve the UIParameter value or the link value.
}

Can someone PLEASE help me with the answer to this???!!! It can't be that hard, only hard to find.

 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If I had something like this:


I could pull it out with something like this:



Make sure you get the parameter you want, and not just the last parameter associated with the last link on the page.

Alternatively, you could bind the parameter to a UIParameter object in a backing bean.

I'm not a big fan of using params myself, although like many things I'm not proud of, I have used them in the past.

Good luck!

-Cameron McKenzie
 
Richard Blaha
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate your quick response. As you saw in my code I dynamically create my HtmlPanelGrid. This is built in an ActionEvent handler triggered by a valueChangeListener.

<h:panelGrid columns="2">
<hutputText value="ItemNumber:"/>
<h:inputText id="srchNum" value="#{itembean.srchNum}" title="Number" onkeyup="DynaFaces.fireAjaxTransaction(this, { execute: 'srchNum', render: 'outputListPanel' });" valueChangeListener="#{itembean.addNumbers}" />
</h:panelGrid>


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




It appears that because the outputListPanel is created dynamically when the HtmlCommandLink is clicked the parameters passed are not those associated with the HtmlCommandLink, but the original JSF JSP it was in. I concluded this because I tried your suggestion and the only keys I got were from the original JSF JSP.

Next step?

 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's what I was worried about when I mentioned them being associated with the 'last parameter link' or something.

I think a single form can only be associated with a parameter with a particular name. So if four parameters exist with the same name (different values) I think only the last one gets picked up, or something like that. Now, if each html link and associated parameter was in a different form, then you wouldn't have that problem, because each form is logically separate. It's just an option, but not a great one.

-Cameron McKenzie
 
Richard Blaha
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish to thank you again Cameron. This is my first experience with writing JSF manually so to speak though I am still using Netbeans 6.7.1 to do so. The visual JSF handled so many of these items for me. But this is my first attempt at JSF w/Ajax.


I used the following line in the action method:

logger.log( Level.INFO, "fc.getExternalContext().getRequestParameterMap().toString() ["+fc.getExternalContext().getRequestParameterMap().toString()+"]");

I find no reference to the UIParamenter or anything related to the HtmlCommandLink. I guess I am hoping that I am not the first to have encountered this situation.

I will continue plugging along. I am still open to more suggestions.

Rick Blaha
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you doing this within a custom tag?

If so, make sure you call processDecodes() on the link at some point, otherwise the link won't work properly. Just an idea?

-Cameron McKenzie
 
Richard Blaha
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not using a custom tag as far as I know. If I am inadvertantly using one I just stumbled across it using examples from books and tutorials.

I have a fuller copy of my question in a JSF Subject 'How to create BINDING of command link with backing beans' http://www.coderanch.com/t/212476/JSF/java/create-BINDING-command-link-with. No one had replied so I decided to take it in steps.

I guess at this point I have a couple of questions.

1. What is the purpose of ValueExpression since other means of binding to a HtmlCommandLink instance has been deprecated to 'link.setValueExpression'?

2. With the concept of FacesContext in place and the MethodExpression actually working performing the action assigned to it, why aren't all the values assigned to the HtmlCommandLink passed with the action?

 
Richard Blaha
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer has come. Thank you for your help. I needed to make changes to my methodology and learn just a little bit more. The link in my preceding post has the complete changes I needed to make. Here I present the changes I needed to make that apply to the question I posed in this thread.

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";
}

 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the attribute tag on the outputText element of a commandLink? Man, that's a very sneaky way of getting things done.

Very creative!

-Cameron McKenzie
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!