通知JSF服务器端有关Jquery / Ajax对(DOM)客户端状态所做的更改 [英] Notify JSF Server side about changes on (DOM) client-state made by Jquery/Ajax

查看:86
本文介绍了通知JSF服务器端有关Jquery / Ajax对(DOM)客户端状态所做的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用基于组件的框架(如JSF),定义了一个复选框的组件:

Using a component based Framework like JSF, a component for a checkbox get defined:

<h:selectManyCheckbox value="#{user.favNumber1}">
    <f:selectItem itemValue="1" itemLabel="Number1 - 1" />
    <f:selectItem itemValue="2" itemLabel="Number1 - 2" />
    <f:selectItem itemValue="3" itemLabel="Number1 - 3" />
</h:selectManyCheckbox>

生成的(x-html)代码如下:

the generated (x-html) code looks like:

<table>
<tr>
  <td>
    <input name="j_idt6:j_idt10" id="j_idt6:j_idt10:0" value="1" type="checkbox" />
    <label for="j_idt6:j_idt10:0" class=""> Number1 - 1</label></td>
  <td>
    <input name="j_idt6:j_idt10" id="j_idt6:j_idt10:1" value="2" type="checkbox" />
    <label for="j_idt6:j_idt10:1" class=""> Number1 - 2</label></td>
  <td>
    <input name="j_idt6:j_idt10" id="j_idt6:j_idt10:2" value="3" type="checkbox" />
    <label for="j_idt6:j_idt10:2" class=""> Number1 - 3</label></td>
  <td>
</tr>
</table>

一个Jquery函数改变了这个组件的状态(比如说值为1的方框)一个事件(单击按钮):

a Jquery function changes the state of this component (let say the box with value "1") on one event(the click of "button"):

<script>  // or in $(document).ready(){}
   $('#button').click(function(){ 
       var $checkbox = $(this).find('#j_idt6:j_idt10:0');
       $checkbox.attr('checked', !$checkbox.attr('checked'));
   });
</script>

如何通知JSF端客户端状态发生了变化。在这个级别,JSF仍然认为
该复选框具有例如值0,而它现在具有值1.这导致状态不一致...

how do I notify the JSF-side that the client state changed. at this level JSF still thinks that the checkbox has e.g value 0 whereas it has now value 1. this leads to state inconsistency...

我的是什么选项?

我认为JSF在其他(Rich-,Primes-,MyFaces)上支持AJAX,但我仍然坚持使用它,知道
让我的组件更新,因为我正在构建一个具有许多功能的富客户端
(显示操作,Jquery UI小部件,Web远程处理,动态行为,Web服务,视觉效果等)。应该严重操纵的DOM客户端状态和Jquery是必须的.​​.....所以它不能放在一边。

I think of the AJAX- support of JSF on other (Rich-, Primes-, MyFaces) but I still relent to use it, knowing that to have my component to update , because I am building a rich client with many features (Display manipulation, Jquery UI widgets, Web remoting, dynamic behaviour, Web-services, visual effects , and so on ). the DOM client state in supposed to heavily manipulated and Jquery is something that is a must.. so it can't be left aside.

谢谢!

推荐答案

由于您使用的是JSF2,您应该使用 f:ajax (谷歌更多关于它)

Since you are using JSF2 you should use f:ajax (google some more about it)

像这样(在你的情况下不需要jquery)

Like this (no jquery needed in your case)

<h:selectManyCheckbox value="#{user.favNumber1}">
    <f:selectItem itemValue="1" itemLabel="Number1 - 1" />
    <f:selectItem itemValue="2" itemLabel="Number1 - 2" />
    <f:selectItem itemValue="3" itemLabel="Number1 - 3" />
    <f:ajax listener="#{user.someMethod}" />
</h:selectManyCheckbox>

用户 bean中的位置

public void someMethod(AjaxBehaviorEvent ev) {
    System.out.println(favNumber1);
}

这篇关于通知JSF服务器端有关Jquery / Ajax对(DOM)客户端状态所做的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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