JSF验证.可以简化吗? [英] JSF validation. can this be simplified?

查看:83
本文介绍了JSF验证.可以简化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表格,里面有一堆字段.每个都是必需的,并且每个都有不同的名称:

I have a simple form with a bunch of fields. each of them is required, and each has a different name:

  • 城市
  • 状态

提交表单时,我检查每个字段是否为空,并为每次验证添加唯一消息,例如:

when the form is submitted i check if each field is empty and add a unique message for each validation to the context like:

  • 需要城市
  • 状态为必填

我不能简单地在jsp上使用required = true属性,因为该消息将是通用的,而这不是必需的.

i cant simply use the required=true attribute on the jsp because the message will be generic and this is not what is needed.

我对jsf相当陌生,所以请告诉我一个更好的方法吗?

I am fairly new to jsf, so please tell me of a better way to do this?

推荐答案

可以使用requiredMessage属性

<h:inputSomething required="true" requiredMessage="Foo is required" />


使用label属性并提供自定义的必需消息模板.


Or use label attribute and supply a custom required message template.

<h:inputSomething label="Foo" required="true" />

在类路径中带有CustomMessages.properties,其中包含自定义消息模板

with a CustomMessages.properties in the classpath which contains the custom message template

javax.faces.component.UIInput.REQUIRED = {0} is required.

{0}将替换为label属性的值.您可以在JSF规范中找到所有键的概述(例如 JSF 2.0规范-第2.5.2.4章).在faces-config.xml中将消息属性文件声明为message-bundle:

The {0} will be substituted with the value of the label attribute. You can find an overview of all keys in the JSF specification (e.g. JSF 2.0 spec - chapter 2.5.2.4). Declare the message properties file in faces-config.xml as message-bundle:

<application>
    <message-bundle>com.example.CustomMessages</message-bundle>
</application>

(假设它在软件包com.example中,您可以随意命名)

(assuming that it's in the package com.example, you can name it whatever you want)

有关更多消息模板键的信息,请检查JSF规范.

For more message template keys, check the JSF specification.

这篇关于JSF验证.可以简化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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