h:commandLink with f:param doesn't work
I created a simple commandLink with a param. But clicking the link does nothing, not even a page reload.
<h:commandLink id="guess_100" value="100" action="#{NumberGuessBean.checkGuess}">
<f:param id="guessParam_100" name="userGuess" value="100"/>
</h:commandLink>
NumberGuessBean is in request scope. checkGuess is defined as public String checkGuess() {}. This method isn't being called when the link is clicked.
I'm using Glassfish with JavaServer Faces implementation 1.2_04-b10-p01.
Am I missing something here?
Thanks in advance.
[603 byte] By [
1-1-1a] at [2007-11-27 3:54:18]

# 4
Looking at the generated html, i see this in the onclick event of the link:
document.forms['guessInputForm']['guess'].value='0'
Because there isn't a form field called "guess", a JavaScript error occurs.
Shouldn't JSF create the hidden field?
index.jsp:
<f:view>
<h:form id="guessInputForm">
<h:panelGrid id="userNumbersPanel" binding="#{NumberGuessBean.userNumbersPanel}"/>
</h:form>
</f:view>
getUserNumbersPanel() in NumberGuessBean.java:
public HtmlPanelGrid getUserNumbersPanel(){
FacesContext facesContext = FacesContext.getCurrentInstance();
Application application = facesContext.getApplication();
if(userNumbersPanel == null){
userNumbersPanel = new HtmlPanelGrid();
}
userNumbersPanel.setColumns(gameRandomNumber.getMaxNumber() + 1);
List<UIComponent> children = userNumbersPanel.getChildren();
HtmlCommandLink link;
UIParameter param
for (int i = 0; i <= gameRandomNumber.getMaxNumber(); i++) {
link = (HtmlCommandLink)application.createComponent(HtmlCommandLink.COMPONENT_TYPE);
link.setActionExpression(application.getExpressionFactory()
.createMethodExpression(facesContext.getELContext(),
"#{NumberGuessBean.checkGuess}",
String.class,
new Class<?>[0])
);
link.setValue(String.valueOf(i));
param = new UIParameter();
param.setName("guess");
param.setValue(String.valueOf(i));
link.getChildren().add(param);
children.add(link);
}
return userNumbersPanel;
}
# 7
Thanks for the help.
Yes, I'm creating links in the backing bean. And using session scope has no effect.
However, in trying to debug this issue, I created a simple form with a commandLink and a param. I still get the javascript error... the onclick event is still trying to set a variable that doesn't exist.
Without the f:param, the link works and checkGuess is called.
Is there a setting in web.xml I need to specify?
index.jsp
<f:view>
<h:form id="guessInputForm">
<h:commandLink id="link1" action="#{NumberGuessBean.checkGuess}">
<h:outputText value="Link = 1000"/>
<f:param name="guess" value="1000" />
</h:commandLink>
</h:form>
</f:view>
and checkGuess():
public String checkGuess() {
System.out.println("checkGuess...");
return null;
}