我有一个带有一堆字段的简单表单.每个都是必需的,每个都有不同的名称:
提交表单时,我检查每个字段是否为空,并为上下文添加唯一的消息,如:
我不能简单地在jsp上使用required = true属性,因为消息将是通用的,而这不是我们需要的.
我对jsf相当新,所以请告诉我一个更好的方法吗?
要么使用requiredMessage属性
<h:inputSomething required="true" requiredMessage="Foo is required" />
Run Code Online (Sandbox Code Playgroud)
或使用label属性并提供自定义所需的消息模板.
<h:inputSomething label="Foo" required="true" />
Run Code Online (Sandbox Code Playgroud)
用CustomMessages.properties其中包含自定义消息模板类路径
javax.faces.component.UIInput.REQUIRED = {0} is required.
Run Code Online (Sandbox Code Playgroud)
的{0}将与的值被取代label属性.您可以在JSF规范中找到所有键的概述(例如JSF 2.0规范 - 第2.5.2.4章).将消息属性文件声明faces-config.xml为message-bundle:
<application>
<message-bundle>com.example.CustomMessages</message-bundle>
</application>
Run Code Online (Sandbox Code Playgroud)
(假设它在包装中com.example,你可以随意命名)
有关更多消息模板键,请检查JSF规范.
| 归档时间: |
|
| 查看次数: |
985 次 |
| 最近记录: |