inputText中指定名称属性

inputText中指定名称属性

本文介绍了如何在h:inputText中指定名称属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将h:inputText呈现为以下html输出:

I need to render h:inputText as following html output :

  <input id="yourName" type="text" name="name" />
  <input id="email" type="text" name="email" />

但是h:inputText呈现的name属性与组件的客户端ID相同.我想自己指定name属性,而不是在其中放置客户端ID,以便输入字段可以显示来自其他站点上相同字段类型的先前提交值的有意义的自动完成建议.例如当我在电子邮件的输入字段中使用name="email"时,会向用户显示他之前在其他网站上提交的电子邮件ID的建议.

But h:inputText renders the name attribute same as client id of the component. I want to specify the name attribute myself, instead of putting client id in that, so that the input field can show meaningful autocomplete suggestions from previously submitted values for same field type on other sites. For e.g. when I use name="email" with input field for email, user is shown suggestions of emails ids he submitted previously on other websites.

推荐答案

您无法使用<h:inputText>实现它.它的名称由JSF根据客户端ID(后者又基于组件ID及其所有命名容器父代)自动生成.

You can't achieve it using <h:inputText>. Its name is autogenerated by JSF based on the client ID (which is in turn based on component ID and all of its naming container parents).

无论如何,您基本上有2个选项可以实现具体的功能要求:

You've basically 2 options to achieve the concrete functional requirement anyway:

  1. 如果没有其他命名容器父项,请指示父表单不要在其ID之前加上前缀:

  1. If there are no other naming container parents, instruct the parent form to not prepend its ID:

<h:form prependId="false">

但是,这将导致<f:ajax>失败.

使用纯HTML元素而不是JSF组件:

Use plain HTML elements instead of JSF components:

<input name="name" value="#{bean.name}" />
<input name="email" value="#{bean.email}" />

您只需通过@ManagedProperty在请求范围内的bean上自己收集它们:

You only have to collect them yourself via @ManagedProperty on a request scoped bean:

@ManagedProperty("#{param.name}")
private String name;

@ManagedProperty("#{param.email}")
private String email;

您将错过JSF内置的验证/转换工具和ajax魔术.

And you'll miss JSF builtin validation/conversion facility and ajax magic.

但是有一个完全不同的替代方法:使用HTML5 <input type="email">.这样,浏览器将以相同类型的输入自动建议所有先前输入的电子邮件. <h:inputText>本机不支持此功能.但是,您可以使用自定义渲染工具包使其工作,如:

There's however a completely different alternative: use HTML5 <input type="email">. This way the browser will autosuggest all previously entered emails on inputs of the very same type. This is not natively supported by <h:inputText>. You can however use a custom render kit to get it to work, as answered in Adding custom attribute (HTML5) support to Primefaces (3.4):

<h:inputText type="email" ... />


更新,从JSF 2.2开始,您终于可以轻松声明传递属性,而无需自定义渲染工具包.


Update as of JSF 2.2 you can finally easily declare passthrough attributes without needing a custom render kit.

<... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
...
<h:inputText a:type="email" ... />

这篇关于如何在h:inputText中指定名称属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:46