如何打破clientID的暴政? [英] How do I break the tyranny of the clientID?

查看:23
本文介绍了如何打破clientID的暴政?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写 JSF 2.0 表单时,我最不喜欢的部分是处理各种输入元素的 id 属性.我永远无法从支持 bean 中对目标组件的 clientID 进行编码,特别是因为 PrimeFaces tabView 现在包含 p:tab 元素的 id 作为客户 ID.我浪费了大量时间编码、测试,然后重新编码这些客户端 ID.

My least favorite part of coding JSF 2.0 forms has to do with the handing of the id attributes of the various input elements. I am forever having trouble coding the clientID of the target component from within the backing bean, particularly since PrimeFaces tabView now includes the id of the p:tab element as part of the clientID. I waste tons of time coding, testing, then re-coding those clientIDs.

它让人想起旧式汇编语言编程,您必须为分支和循环生成大量标签名称.我这辈子做的已经够多了.

It is reminiscent of older-style assembly language programming where you have to generate tons of label names for your branches and loops. I've done of enough of that for a lifetime.

我尝试的一种方法是仅使用自动生成的 id 属性.例如,我的表单的一行可能看起来像这样.

One approach I am trying is to use only auto-generated id attributes. For example one line of my form might look like this.

<h:outputLabel value="Full Name:" />
<p:inputText value="#{editUser.user.fullName}"
             binding="#{editUser.compFullName}"/>
<p:message for="#{editUser.compFullName.clientId}" />

请注意,我没有明确的 id 属性.然后在支持 bean 中:

Note that I do not have an explicit id attribute. Then in the backing bean:

    String clientID = getCompFullName().getClientId();
    msg = new FacesMessage(FacesMessage.SEVERITY_INFO, 
            "Summary Message For Full Name", "Detail Message Full Name");
    FacesContext.getCurrentInstance().addMessage(clientID, msg);

这始终有效,即使组件具有复杂的 clientID,例如当 PrimeFaces 将 p:tab id 插入到 clientID 时.(它从 v 3 开始).重新排列表单永远不会破坏任何东西.

This always works, even if the component has a complex clientID, such as when PrimeFaces inserts the p:tab id into the clientID. (Which it does starting v 3). Rearranging the form never breaks anything.

然而,这很费力,因为我必须创建 UIComponent 属性、getter 和 setter,并将它们绑定到具有 binding 属性的表单中.任何人都可以提出更好的方法吗?

It is, however, laborious, since I have to create UIComponent properties, getters and setters, and bind them in the form with binding attributes. Can anyone suggest a better way of doing this?

推荐答案

因为我必须创建 UIComponent 属性、getter 和 setter,并将它们绑定到具有绑定属性的表单中.任何人都可以提出更好的方法吗?

如果您根本不使用它,则不需要将组件绑定到某个支持 bean.只需将其绑定到视图即可:

It's not required to bind the component to some backing bean if you don't use it in there at all. Just bind it to the view instead:

<p:inputText value="#{editUser.user.fullName}"
             binding="#{compFullName}"/>
<p:message for="#{compFullName.clientId}" />

为了使代码更加自文档化,我建议通过 faces-config.xml 在请求范围内放置一个 HashMap:

To make the code more self-documenting, I suggest to put a HashMap in the request scope by faces-config.xml:

<managed-bean>
    <description>Holder of all component bindings.</description>
    <managed-bean-name>components</managed-bean-name>
    <managed-bean-class>java.util.HashMap</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<p:inputText value="#{editUser.user.fullName}"
             binding="#{components.fullName}"/>
<p:message for="#{components.fullName.clientId}" />

添加消息应该由 ConverterValidator 完成,后者将其作为 ConverterExceptionValidatorException 分别.它将自动结束在正确的消息持有者中.或者,如果是非正式消息,只需将其添加到 UIComponent 的客户端 ID 上,该 ID 已经可作为方法参数使用.

Adding messages is supposed to be done by a Converter or a Validator which is trowing it as a ConverterException or ValidatorException respectively. It will automatically end up in the right message holder. Or if it are informal messages, just add it on the client ID of the UIComponent which is already available as method argument.

这篇关于如何打破clientID的暴政?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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