在同一按钮上同时使用JSF ajax和setPropertyActionListener标记 [英] Use both JSF ajax and setPropertyActionListener tags on the same button

查看:71
本文介绍了在同一按钮上同时使用JSF ajax和setPropertyActionListener标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望当用户单击按钮2时发生:

I would like that when the user clicks on a button 2 things happen :

  1. 首先触发setPropertyActionListener,以更新我的bean的属性
  2. 然后进行ajax调用以从Bean执行方法
  1. First a setPropertyActionListener is triggered, to update a property of my bean
  2. Then an ajax call is made to execute a method from the bean

似乎我必须在f:setPropertyActionListener标记和f:ajax标记之间进行选择,才能嵌套在commandButton标记内.

It seems that I must choose between the f:setPropertyActionListener tag and the f:ajax tag to nest inside the commandButton tag.

是否可以同时执行ajax调用和属性更新?

Is there a way to execute both the ajax call and the property update at the same time?

非常感谢!

我忘了提我不想执行表单,我想使用ajax侦听器,这是我的代码:

EDIT : I forgot to mention I don't want a form execution, I want to use the ajax listener, here is my code :

<h:commandLink styleClass="blue-button" value="+">
  <f:setPropertyActionListener target="#{tvShowForm.selected}" value="#{tvShow.externalId}" />
  <f:ajax event="click" listener="#{tvShowForm.add}" />
</h:commandLink>

使用此代码,不会执行f:setPropertyActionListener.

With this code the f:setPropertyActionListener is not executed.

我使用动作表单方法而不是侦听器将代码修改为以下内容.现在,仅当我未在ajax标记上指定事件属性时,该方法才起作用,但是当我指定一个事件属性时,将不执行action方法.

I modified my code to the folowing, using action form method instead of listener. Now it works only if I don't specify an event attribute on the ajax tag, but when I specify one, the action method is not executed.

<h:commandLink action="#{tvShowForm.add}" styleClass="blue-button" value="+">
  <f:setPropertyActionListener target="#{tvShowForm.selected}" value="#{tvShow.externalId}" />
  <f:ajax event="click" />
</h:commandLink>

推荐答案

在Tomcat 7.0.19和Glassfish 3.1.1上与Mojarra 2.1.3一起为我工作.

Works just for me with Mojarra 2.1.3 on Tomcat 7.0.19 and Glassfish 3.1.1.

我使用了这一相关视图:

I used this relevant piece of the view:

<h:form>
    <h:commandButton value="submit" action="#{bean.submit}">
        <f:setPropertyActionListener target="#{bean.value}" value="foo" />
        <f:ajax />
    </h:commandButton>
</h:form>

与此相关的一块豆子:

private String value;

public void submit() {
    System.out.println("Value: " + value);
}

// Getter/setter.

在ajax提交上打印以下内容:

which prints the following on the ajax submit:

值:foo

Value: foo

您的问题是由您遗漏了其他问题引起的.

Your problem is caused by something else which you left out the question.

更新:根据您的更新

我忘了提我不想执行表单,我想使用ajax侦听器,这是我的代码:

我真的不明白为什么你不想要表单执行".您正在提交表单.您只剩下了action方法,而是在ajax事件监听器中完成了业务工作.动作侦听器方法无意执行任何业务工作,它们最多用于某些自包含的处理或日志记录工作.依赖模型的业务工作应该在操作方法中完成,这是它的用途.

I really don't see why you don't want "a form execution". You are submitting the form. You've only left out the action method and you're doing the business job in the ajax event listener instead. The action listener methods aren't intented to do any business job, they're for at highest some self-containing processing or logging job. The model-dependent business job should be done in the action method, there it is for.

这篇关于在同一按钮上同时使用JSF ajax和setPropertyActionListener标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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