Primefaces - 命令按钮不起作用 [英] Primefaces - commandButton does not work

查看:21
本文介绍了Primefaces - 命令按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 commandButton 的问题,它仅在提交类型时才起作用.有人可以看看并告诉我是否有解决方案吗?下面的代码非常简单,并且确实有说明我需要的建议.不执行方法 test().方法 runSubmit 执行成功.

我需要在没有提交的情况下执行测试方法,因为原始页面确实有在提交期间执行的验证,必须在没有提交的情况下执行 test() 方法,因为它是提交之前的初步操作.

我正在使用 PrimeFaces 4.0、JDK 7、Tomcat 6 和 JSF 2.0 (Apache),但我认为它也在 Mojarra 中发生.

 会话:包 com.andre.bean;公共类 AndreBean {公共无效运行提交(){System.out.println("提交执行");}公共字符串测试(){System.out.println("未提交执行");返回真";}}

<小时>

XHTML<html xmlns="http://www.w3.org/1999/xhtml"xmlns:h="http://java.sun.com/jsf/html"xmlns:f="http://java.sun.com/jsf/core"xmlns:p="http://primefaces.org/ui"><h:头></h:head><h:form id="test"><p:commandButton id="ns" value="not submit" type="button" action="#{andreBean.test}" ajax="false"></p:commandButton><p:commandButton id="s" value="submit" action="#{andreBean.runSubmit}"></p:commandButton></h:form>

<小时>

非常感谢安德烈

解决方案

发生了什么?

你得到的是正确的行为.在 PrimeFaces 中,带有 type="button" 的按钮就像在基本 HTML 中一样工作 - 它不会引起任何请求.正如 PrimeFaces 用户指南所说:

<块引用>

按钮用于执行自定义 javascript 而不会导致ajax/非ajax请求.创建一个按钮集类型为按钮".

<p:commandButton type="button" value="Alert" onclick="alert('Prime')"/>

如果你想和 bean对话",你需要使用 type="submit"(这是 p:commandButton 的默认设置).然而……与 HTML 中的提交按钮行为相反,在 PrimeFaces 中,此类提交不会强制重定向到新页面,但所有通信都将由底层 AJAX 请求处理.

因此只有您的第二个按钮会执行 bean 的方法:

<p:commandButton id="s" value="submit" action="#{andreBean.runSubmit}"/>

您可能想获得什么?

如果您不想将所有表单发送到 bean,您可以限制使用 p:commandButton 的process"属性处理的组件的范围:

<p:inputText value="#{andreBean.value}"/><p:commandButton id="s" value="submit" action="#{andreBean.runSubmit}" process="@this"/></h:form>

使用以下 bean,您将看到不同之处:

public class AndreBean {私有字符串值;公共无效运行提交(){System.out.println("提交执行");}公共字符串 getValue() {System.out.println("getValue");返回值;}公共无效集值(字符串值){System.out.println("setValue:" + value);this.value = 值;}}

如果您不限制在控制台中执行的组件,您会得到:

getValue设置值:foobar提交执行

...并且组件仅限于process="@this",您只能获得:

提交执行

希望有所帮助.

I am facing a problem with commandButton, it is working only when the type is submit. Can someone take a look and let me know if there is a solution for that? The code below is very simple and does have the the propose to illustrate what I need. The method test() is not executed. Method runSubmit is executed successfully.

I need that test method is executed without a submit as the original page does have validations that are executed during the submit, test() method must be executed without a submit as it is a preliminary operation before of the submit.

I am using PrimeFaces 4.0, JDK 7, Tomcat 6 and JSF 2.0 (Apache), however I think it is happening in Mojarra as well.

    SESSION:

package com.andre.bean;

public class AndreBean {

public void runSubmit() {
System.out.println("Submit executed");
}

public String test() {
System.out.println("Not submit executed");
return "true";
}

}


XHTML

<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:p="http://primefaces.org/ui">

<h:head> 
</h:head>

<h:form id="test">
<p:commandButton id="ns" value="not submit" type="button" action="#{andreBean.test}" ajax="false"></p:commandButton>
<p:commandButton id="s" value="submit" action="#{andreBean.runSubmit}"></p:commandButton>

</h:form> 

</html>


Thank you very much Andre

解决方案

What's going on?

What you get is correct behaviour. In PrimeFaces button with type="button" works as it does in basic HTML - it doesn't cause any request. As PrimeFaces user's guide says:

Push buttons are used to execute custom javascript without causing an ajax/non-ajax request. To create a push button set type as "button".

<p:commandButton type="button" value="Alert" onclick="alert('Prime')" />

If you want to "talk to" bean, you need to use type="submit" (which is default in p:commandButton). However... contrary to submit buttons behaviour in HTML, in PrimeFaces such submission will not force redirection to new page but all communication will be handled by underlying AJAX requests.

Therefore only your second button will execute beans' method:

<p:commandButton id="s" value="submit" action="#{andreBean.runSubmit}" />

What probably you wanted to obtain?

If you don't want to send all your form to bean you can limit the scope of components that are processed with "process" attribute of p:commandButton:

<h:form id="test">
    <p:inputText value="#{andreBean.value}"/>
    <p:commandButton id="s" value="submit" action="#{andreBean.runSubmit}" process="@this" />
</h:form>

With the following bean you will see the difference:

public class AndreBean {

    private String value;

    public void runSubmit() {
        System.out.println("Submit executed");
    }

    public String getValue() {
        System.out.println("getValue");
        return value;
    }

    public void setValue(String value) {
        System.out.println("setValue: " + value);
        this.value = value;
    }

}

If you don't limit executed components in console you get:

getValue

setValue: foobar

Submit executed

...and with components limited only to process="@this" you get only:

Submit executed

Hope that helps.

这篇关于Primefaces - 命令按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆