java - JSF 2 单击按钮时执行操作

标签 java jsf jsf-2

我有这个 JSF 代码

<f:view>
    <h:form>                    
        <h:commandButton value="Generate License File" type="button" action="#{agreement.generateLicenseFile}"/>                    
    </h:form>
</f:view>

这就是 bean

@ManagedBean(name="agreement")
@RequestScoped
public class AgreementBean {

    private boolean generate=false; 

    public void generateLicenseFile(){
        generate=true;
    }
}

当我执行应用程序并单击按钮时,绝对没有任何反应。执行过程中没有错误。它根本没有任何作用。

有什么想法吗?

--编辑--

我的意思是 boolean 值没有被修改。

最佳答案

When I execute the app, and click on the button, absolutely nothing happens

幕后发生了很多事情!

  • 它将触发 POST向服务器发出的请求将由 Faces Servlet 处理。
  • 这将启动 JSF lifecycle您可以按照 this tutorial 进行调试。对于 JSF 生命周期,我将仅解释最后两个阶段(其他链接很好地解释了其他 4 个阶段,特别是 BalusC 的)。
  • 在调用应用程序阶段(第五阶段),服务器将执行 generateLicenseFile绑定(bind)到 <h:commandButton> 的方法在你的<h:form> .
  • 由于您在此阶段没有返回任何内容,因此在渲染响应阶段(第 6 阶段)将仅检索更新了 ViewState 的当前 View 。此外,由于您没有对 View 中的任何组件进行重大更新,因此您将看不到任何内容。

此外,由于 bean 配置为 @RequestScoped它将在每次提交表单时创建,并且 generate字段将始终 false根据请求创建时(因为您没有在 View 中更改其值,所以它将保留 false 值)。 bean 创建可能会根据声明的范围而有所不同。更多信息:Communication in JSF 2: Managed bean scopes .

如果您想在 View 中看到某些内容,我建议您稍微更改一下代码:

JSF 代码

<h:form>
    #{agreement.generate}
    <br />
    <h:commandButton value="Generate License File" type="button"
        action="#{agreement.generateLicenseFile}" />
</h:form>

托管bean

@ManagedBean(name="agreement")
@ViewScoped //removed RequestScoped, this WILL make the difference
public class AgreementBean {
    private boolean generate = false; 
    public void generateLicenseFile(){
        generate = !generate;
    }
    //getter and setter for generate field
}

关于java - JSF 2 单击按钮时执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17604073/

相关文章:

java - 强制带注释的类包含带注释的字段

java - 步骤之后和步骤之前 cucumber

jsf - f :facet - there is a list of predefined names attributes?

java - 如何获取用户界面:param value in Javabean

java - JSF 1.2,是否有任何实用程序可以学习它是如何工作的?

java - 如何重构 hibernate 实体?

Java Comprable 重写还是不重写compareTo?

mysql - 用于测试的 JSF 托管

java - 绑定(bind)到昂贵的 JSF 属性的正确方法是什么?

java - 为什么 Glassfish 拒绝我在 Netbeans 7.3.1 下的所有身份验证工作?