JSF/Seam中的动态ID [英] Dynamic Id's in JSF/Seam

查看:101
本文介绍了JSF/Seam中的动态ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的Seam应用程序出现了一个小问题,我想知道是否有人知道如何解决这个问题.我的应用程序中有一个表单,该表单使用AJAX来显示某些输入框,具体取决于下拉框中的项目.该代码工作正常,除了在我的输入框中设置ID之外.看来JSF不允许我通过变量设置ID.标签中的其他属性(例如"for")也可以.这是一些解释我的意思的代码:

Got a little problem with a Seam application I'm working on and I was wondering if anyone knows a way round it. I've got a form in my application that uses AJAX to show certain input boxes depending on an item in a dropdown box. The code works fine except for setting the ID's in my input boxes. It looks like JSF doesn't let me set an ID via a variable. Other attributes like "for" in labels are fine. Here's some code explaining what I mean:

<ui:repeat value="#{serviceHome.instance.serviceSettings}" var="currSetting" >
  <li>
    <!-- Imagine the below works out as "settingABC" -->
    <c:set var="labelKey" value="setting#{jsfUtils.removeWhitespace(currSetting.key.name)}" />

    <!-- Labelkey is correctly added into this input so for = "settingABC" -->
    <h:outputLabel for="#{labelKey}" styleClass="required generated" value="#{currSetting.key.name}:"/>

    <s:decorate styleClass="errorwrapper">

      <!-- Labelkey ISN'T correctly added into this input. Instead we just get "setting" -->
      <h:inputText id="#{labelKey}" value="#{currSetting.value}"/>

      <a4j:outputPanel ajaxRendered="true">
        <h:message for="#{labelKey}" styleClass="errormessage" />
      </a4j:outputPanel>
    </s:decorate>
  </li>
</ui:repeat>

有人知道我该如何克服吗?

Does anyone have any idea how I can get past this?

推荐答案

您会明白为什么他们不让您设置ID,对吗? JSF接管了id的创建,因为您处于一个重复的组件循环中,如果让他们只设置id,那么最终将得到重复的ID,反正还是无济于事.

You see why they don't let you set the ID, right? JSF takes over id creation because you're in a repeated loop of components and, if they let you just set the id, you'd end up with duplicate IDs, which wouldn't help you anyway.

在不知道为什么要显式设置ID的情况下,很难为您提供解决方法.如果是JavaScript,则可以按照Grant Wagner的建议进行操作,并让JSF给出ID形式的内容.您还可以看一下生成的HTML,并查看ID的格式.JSF通常使用

Without knowing WHY you want to set the ID explicitly, it's hard to give you a workaround. If it's for JavaScript, you can do what Grant Wagner suggests, and let JSF give you what it put as the id. You can also take a peek at the generated HTML and see what format the id is in. JSF usually uses

"form_id:loop_id:loop_index:component_id" 

作为它为表单/重复项中的组件生成的ID.您必须确定并在表单和ui:repeat标签中提供ID,以了解它们是什么.

as the id it generates for components in a form/repeat. You'd have to be sure and give id's to your form and ui:repeat tags to know what they were.

好吧,您回答您想在循环内为特定的inputText使用h:message标签,这很容易.

Ok, you answered that you want to have an h:message tag for a specific inputText inside the loop, that's easy.

<h:inputText id="myInput" .... />
<h:message for="myInput" ... />

现在,为输入生成的消息将显示在消息中,并且JSF将修改"for"属性(尽管不是HTML生成的),就像在inputText中显示"id"属性一样,因此匹配.

Now, messages generated for the input will be displayed in the message, and JSF will mangle the "for" attribute (though that isn't generated to HTML) just like it will the "id" attribute in the inputText so they match.

您甚至可以在处理程序代码中创建自己的OWN消息以转到特定的h:message,但是您需要使用对clientId的调用来获取消息的目标(给定后备bean(不是值)支持组件).

You can even make your OWN messages in your handler code to go to the specific h:message, but you'll need to use a call to clientId to get the target of the message, given the backing bean (not the value backing bean) of the component in question.

这篇关于JSF/Seam中的动态ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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