I've been struggling with trying to get my commandButtons to perform an action (yet oddly, I have no problem pulling data from beans to include in my page). Have even posted up my code elsewhere and had it reviewed by others. So far, no luck. So, I'm thinking perhaps a different tact is in order. Can anyone point me to some very simple/basic sample code of a 开发者_StackOverflow社区project that has a commandButton that is able to successfully call an action?
A common cause among starters is that the <h:form>
is been forgotten. As per the HTML specification, any submit button which is intented to submit something to the server side should be placed inside a HTML <form>
element.
Here's a simple Hello World how to do that in JSF:
JSF page
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Hello World</title>
</h:head>
<h:body>
<h:form>
Enter your name
<h:inputText value="#{bean.input}" />
<h:commandButton value="submit" action="#{bean.submit}" />
</h:form>
<h:outputText value="#{bean.output}" />
</h:body>
</html>
Bean:
package mypackage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class Bean {
private String input;
private String output;
public void submit() {
output = String.format("Hello %s!", input);
}
public String getInput() {
return input;
}
public String getOutput() {
return output;
}
public void setInput(String input) {
this.input = input;
}
}
That's all :)
For other possible causes of this problem, check the 1st link in the list below.
See also:
h:commandLink
doesn't work- JSF 1.2 tutorial with Eclipse and Tomcat
- JSF 2.0 tutorial at CoreServlets.com
- JSF 2.0 tutorial in
SunOracle Java EE 6 tutorial
精彩评论