我正在实现一个自定义验证器。消息的详细信息存储在资源中。下面是一个消息示例:Value is required for {0}
。{0}应该包含组件的标签。
@FacesValidator("customValidator")
public class CustomValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (validatorCondition()) {
String summary = Res.getString("error");
String detail = ... format detail here
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail));
}
}
}
如何格式化validate方法中显示的消息?
使用MessageFormat#format()
String template = "Value is required for {0}.";
String label = component.getAttributes().get("label"); // Don't forget to handle nulls. JSF defaults to client ID.
String message = MessageFormat.format(template, label);
// ...