JSF2.0 复合组件 ID 生成

发布于 2024-10-09 12:31:28 字数 1368 浏览 0 评论 0原文

我正在 Weblogic 11g 上使用 JSF2.0 创建一个新的 Web 应用程序。我正在使用 JSF Ajax 进行表单提交。我对 JSF 还很陌生,所以仍在学习中。主页有 3 个表单,每个表单都位于 jQuery 选项卡上。原始代码有很多重复的输入字段,如用户名和密码等(id 前面带有表单 id,即 myform1:userName)。我认为对重复字段使用复合组件是个好主意。这是我的组件代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:composite="http://java.sun.com/jsf/composite"
  xmlns:h="http://java.sun.com/jsf/html">

<composite:interface>
    <composite:attribute name="value" required="true"/>
</composite:interface>

<composite:implementation>

<label for="#{cc.id}">User Name</label>
<h:inputText
        id="#{cc.id}"
        size="15"
        value="#{cc.attrs.value}"
        required="true"
        label="User Name">
</h:inputText>
</composite:implementation>

</html>

这是对 xhtml 中组件的调用:

<p>
    <cc:userName id="userName" value="#{soapTestingBackingBean.userName}"/>
</p>

现在的问题是我所有的 id 都添加了一个元素,因此变成了 myForm1:userName:userName。虽然这不是一个显示阻止程序,但它确实意味着我的 javascript 现在必须引用长 id,并且在我的 Ajax 调用中也是相同的(我通常执行 @form 但渲染特定元素)。我正在努力使代码尽可能具有可读性和可维护性。因此,如果有人知道是否有一个优雅的解决办法,或者我错过了一些神奇的属性来关闭它,我将不胜感激。我确实尝试了 tomahawk 库中的 forceID 属性,但这只会导致我的 Ajax 调用出现其他问题。谢谢。

I'm in the process of creating a new Web Application using JSF2.0 on Weblogic 11g. I'm using JSF Ajax for form submission. I'm quite new to JSF so still learning the ropes. The main page has 3 forms each on a jQuery tab. The original code had a lot of duplicate Input Fields like userName and password etc (id prepended with the form id i.e. myform1:userName). I decided it would be a good idea to use Composite Components for the duplicated fields. Here is my Conponent code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:composite="http://java.sun.com/jsf/composite"
  xmlns:h="http://java.sun.com/jsf/html">

<composite:interface>
    <composite:attribute name="value" required="true"/>
</composite:interface>

<composite:implementation>

<label for="#{cc.id}">User Name</label>
<h:inputText
        id="#{cc.id}"
        size="15"
        value="#{cc.attrs.value}"
        required="true"
        label="User Name">
</h:inputText>
</composite:implementation>

</html>

And here is the call to the Component in the xhtml:

<p>
    <cc:userName id="userName" value="#{soapTestingBackingBean.userName}"/>
</p>

The problem now is that all my ids have an added element so have become myForm1:userName:userName. Although this isn't a show stopper it does mean that my javascript now has to reference the long ids and is also the same in my Ajax calls (I generally execute @form but render specific elements). I'm trying to make the code as readable and maintainable as possible. So would appreciate it if anyone knew if there were an elegant work around for this or maybe some magic attribute I missed to turn it off. I did experiment with the forceID attribute from the tomahawk library but this just caused additional problems on my Ajax calls. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺孤伤于心 2024-10-16 12:31:28

一些java脚本库jQuery对jsf 2.0 ids form:elementId的新格式有问题 - 所以

$(function() {
    $( "#datepicker" ).datepicker();
});

我不得不使用而不是使用

$(function() {
    $(document.getElementById('addform:datepicker')).datepicker();
});

some java script libraries jQuery are having problem with the new format for jsf 2.0 ids form:elementId - so instead of using

$(function() {
    $( "#datepicker" ).datepicker();
});

I had to use

$(function() {
    $(document.getElementById('addform:datepicker')).datepicker();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文