简体   繁体   English

有没有简单的JSF commandButton示例可用?

[英]Any simple JSF commandButton samples available?

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). 我一直在努力让我的commandButtons执行一个动作(但是,奇怪的是,我没有问题从bean中提取数据以包含在我的页面中)。 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? 谁能给我指出一个项目的一些非常简单/基本的示例代码,该项目具有一个可以成功调用动作的commandButton?

A common cause among starters is that the <h:form> is been forgotten. 启动程序之间的常见原因是<h:form>被遗忘了。 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. 根据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. 对于此问题的其他可能原因,请检查下面列表中的第一个链接。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM