JSF无法为输入组件设置动态ID [英] JSF unable to set dynamic id for input component

查看:119
本文介绍了JSF无法为输入组件设置动态ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单如下:

当用户单击"+添加"时,将显示一个新的距离输入:

When the user clicks "+ Add" a new distance input shows up:

用户可以根据需要多次重复此操作,但是必须填写所有距离,并且必须为丢失的数据提供单独的消息:

The user can repeat this operation as many times as desired, but all distances must be filled in, and individual messages must be served for missing data:

所以我正在尝试为距离输入设置动态ID.但是,当我尝试传递动态值时,一切都会失败.到目前为止,我已经尝试了两种不同的技术:

So i'm trying to stablish dynamic ids for the distance inputs. However, when i try to pass dynamic values everything fails. up to now i've tried two different techniques:

  • 使用绑定的HtmlDataTable的rowIndex<-始终显示-1
  • 将id设置为Distance类的属性<-难看的异常

这是我的代码:

后备豆:

private List<Distance> distances;
private HtmlDataTable distancesUI;

public void addDistance(ActionEvent e) {
    distances.add(new Distance());
}

JSPX:

<h:dataTable
binding="#{Bean.distancesUI}"   
value="#{Bean.distances}"
var="distance"> 
    <h:column> 
        <h:inputText 
        id ="distance_#{Bean.distancesUI.rowIndex}" // <- always renders "distance_-1"
        value="#{distance.value}" />
    </h:column>
</h:dataTable>

我需要能够显示个别的必需"消息.有什么想法吗?

I need to be able to show individual "required" messages. Any ideas?

预先感谢

推荐答案

我需要能够显示单个必需"消息.有什么想法吗?

为此,您无需摆弄ID. JSF将为您做到这一点.随便

For that you don't need to fiddle with IDs. JSF will do it for you. Just do

<h:dataTable id="table" value="#{bean.distances}" var="distance"> 
    <h:column> 
        <h:inputText id="distance" value="#{distance.value}" required="true" />
        <h:message for="distance" />
    </h:column>
</h:dataTable>

这将最终生成如下所示的HTML:

This will end up in generated HTML like follows:

<table id="form:table">
    <tr><td><input id="form:table:0:distance" /></td></tr>
    <tr><td><input id="form:table:1:distance" /></td></tr>
    <tr><td><input id="form:table:2:distance" /></td></tr>
    ...
</table>

右键单击浏览器中的页面,然后选择查看源代码自己查看.

Rightclick the page in browser and choose View Source to see it yourself.

任何验证错误都将在与输入相同的行中显示.

Any validation error will just be shown in the same row as the input.

这篇关于JSF无法为输入组件设置动态ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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