在jsf中用required,取道的消息是英文!如果使其为中文!

pqds 2006-12-02 09:09:10
请问如何自定义消息!像struts中的validate一样!
...全文
497 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
leojay1 2006-12-04
  • 打赏
  • 举报
回复
//书上有个例子:

package onlyfun.caterpillar;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

public class UserBean {
....

public void validate(FacesContext context,
UIComponent component,
Object obj)
throws ValidatorException {
String password = (String) obj;

if(password.length() < 6) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"字元長度小於6",
"字元長度不得小於6");
throw new ValidatorException(message);
}

if(!password.matches(".+[0-9]+")) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"密碼必須包括字元與數字",
"密碼必須是字元加數字所組成");
throw new ValidatorException(message);
}
}
}  
接著可以在頁面下如下使用驗證器:

.....
<h:inputSecret value="#{user.password}"
required="true"
validator="#{user.validate}"/>
jyh149129 2006-12-04
  • 打赏
  • 举报
回复
对于required在Message_zh_CN.properties中定义:
javax.faces.component.UIInput.REQUIRED
的转意就可以
系统中已经预定义的都可以在资源文件中转义。
e_snow 2006-12-03
  • 打赏
  • 举报
回复
不懂,顶
pqds 2006-12-03
  • 打赏
  • 举报
回复
我是用的validator加上validatorTag!
和你实现的方式大致相同!
但是我的实现不了required,如果为空,我的就是不相应!其他的长度等都可以相应!
我挺想知道!除了这种自己构造是否还有其他比较方便的方法!
谢谢楼上的!
vacuumboy 2006-12-03
  • 打赏
  • 举报
回复
把消息定义在properties文件,然后在要校验的组件里加一个attribute,比如<f:attribute name="fieldRef" value="Error" />
然后定义一个phase-listener在faces-config文件里:<lifecycle>
<phase-listener>com.dconx.swx.web.commons.FieldReferenceListener</phase-listener>
</lifecycle>
listener大概如下:
public class FieldReferenceListener implements PhaseListener {

public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}

public void beforePhase(PhaseEvent e) {
FacesContext fc = e.getFacesContext();
UIViewRoot root = fc.getViewRoot();
Iterator i = fc.getClientIdsWithMessages();
while (i.hasNext()) {
String clientId = (String) i.next();
if (clientId !=null) {
UIComponent c = root.findComponent(clientId);
String fieldRef =
(String) c.getAttributes().get("fieldRef");
if (fieldRef != null && c != null) {
Iterator j = fc.getMessages(clientId);
while (j.hasNext()) {
FacesMessage fm = (FacesMessage) j.next();
fm.setDetail(fieldRef + ": " + fm.getDetail());
}
}
}
}
}

public void afterPhase(PhaseEvent e) {
}
}
然后自己写一个用来显示错误消息的panelGrid,rendered="#{!empty facesContext.maximumSeverity}"
然后不要设置required="true",而是在后台校验,如果为空则用如下语句显示错误消息:
FacesContext.getCurrentInstance().addMessage("mainForm:permissionError",
new FacesMessage(bundle.getString("noPermissionCopyTag")));
return null;
bundle是一个ResourceBundle,用来获取你定义的错误消息。

不知道是否有更方便的方法。
凉凉二点凉 2006-12-03
  • 打赏
  • 举报
回复
没用过,学习,,帮顶。。。。
tcmis 2006-12-02
  • 打赏
  • 举报
回复
jsf没用过,学习,,帮顶。。。。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧