per*_*son 5 error-handling jsf
我想对用户在几个输入字段中输入无效输入时出现的任何JSF错误消息(例如验证,转换消息)进行编号.例如,当用户发送表单时:
-----形式------
(1)姓名:John 13 Doe
年龄:30岁
(2)电子邮件:myemail @ domain
我怎样才能做到这一点?
最好的祝福
对于消息部分,只需使用您设置<h:messages/>
的常用方式显示它们list-style-type: decimal;
.这将显示数字而不是子弹.
下一步是对标签进行编号.这需要更多的工作,因为JSF没有提供任何内置工具来解决这个问题.基本上,您需要确定相关标签是否有关联的消息以及消息排队的顺序.您可以将此信息收集到Map<String, String>
以下内容中:
private Map<String, String> messageIndexes;
public Map<String, String> getMessageIndexes() {
FacesContext context = FacesContext.getCurrentInstance();
if (messageIndexes == null && context.getRenderResponse()) {
messageIndexes = new HashMap<String, String>();
Iterator<String> clientIds = context.getClientIdsWithMessages();
for (int i = 1; clientIds.hasNext(); i++) {
messageIndexes.put(clientIds.next(), String.format("(%d)", i));
}
}
return messageIndexes;
}
Run Code Online (Sandbox Code Playgroud)
然后按如下方式使用它:
<h:form id="form">
<h:outputText value="#{bean.messageIndexes['form:input1']}" />
<h:outputLabel for="input1">Label 1</h:outputLabel>
<h:inputText id="input1" value="#{bean.input1}" required="true" />
<br />
<h:outputText value="#{bean.messageIndexes['form:input2']}" />
<h:outputLabel for="input2">Label 2</h:outputLabel>
<h:inputText id="input2" value="#{bean.input2}" required="true" />
<br />
...
</h:form>
Run Code Online (Sandbox Code Playgroud)
如果地图中不存在客户端ID,则不会呈现任何内容.
要将所有作业移出bean,您可以考虑a PhaseListener
(在beforePhase()
其中RENDER_RESPONSE
)或自定义标签组件.
归档时间: |
|
查看次数: |
2895 次 |
最近记录: |