java 任何简单的 JSF commandButton 示例可用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3132888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any simple JSF commandButton samples available?
提问by Brian Knoblauch
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 project that has a commandButton that is able to successfully call an action?
我一直在努力让我的 commandButtons 执行一个操作(但奇怪的是,我从 bean 中提取数据以包含在我的页面中没有问题)。甚至在别处张贴了我的代码并让其他人对其进行了。到目前为止,没有运气。所以,我在想,也许应该采取不同的策略。任何人都可以向我指出一个项目的一些非常简单/基本的示例代码,该项目具有能够成功调用操作的 commandButton?
回答by BalusC
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.
初学者的一个常见原因<h:form>是被遗忘了。根据 HTML 规范,任何用于向服务器端提交内容的提交按钮都应放置在 HTML<form>元素中。
Here's a simple Hello World how to do that in JSF:
这是一个简单的 Hello World 如何在 JSF 中执行此操作:
JSF page
JSF页面
<!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.
对于此问题的其他可能原因,请检查下面列表中的第一个链接。

