Primefaces-commandButton不起作用 [英] Primefaces - commandButton does not work

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

问题描述

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

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.

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

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.

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

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>


非常感谢你 安德烈(Andre)


Thank you very much Andre

推荐答案

发生了什么事?

您得到的是正确的行为.在PrimeFaces中,带有 type ="button" 的按钮的工作原理与基本HTML相同-它不会引起任何请求.正如 PrimeFaces用户指南所述:

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:

按钮用于执行自定义javascript ,而不会引起 ajax/non-ajax请求.要创建按钮集,请输入"button".

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')" />

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

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.

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

Therefore only your second button will execute beans' method:

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

您可能想获得什么?

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

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>

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

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

...并且仅将组件限制为 process ="@ this" ,您只会得到:

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

Submit executed

希望有帮助.

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

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