jsf的validator问题,在线等
开发环境为myeclipes5+tomcat5,最简单的jsf开发。用validator,不知道为什么message就是显示不到页面上,请大虾帮忙看看代码。
页面代码:
<%@ page contentType= "text/html " language= "java " pageEncoding= "gb2312 "%>
<%@ taglib prefix= "f " uri= "http://java.sun.com/jsf/core "%>
<%@ taglib prefix= "h " uri= "http://java.sun.com/jsf/html "%>
<%@ taglib prefix= "ig " uri= "http://www.infragistics.com/faces/netadvantage "%>
<html>
<head>
<title> Infragistics NetAdvantage For Java Server Faces - Demo </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<meta name= "description "
content= "Infragistics NetAdvantage for Java Server Faces - Demo ">
<link href= "../../resources/default.css " rel= "stylesheet "
type= "text/css ">
</head>
<body class= "rightPanel ">
<f:view>
<h:form>
<h:messages id= "userMessages " showDetail= "true " layout= "table "/>
<h:panelGroup styleClass= "main ">
<!-- TITLE -->
<h:panelGroup styleClass= "section ">
<h:outputText styleClass= "title " value= "请输入查询条件:(输入日期格式为yyyy-mm-dd) " />
<h:panelGrid columns= "3 ">
<h:outputLabel for= "queryTime_1 " rendered= "true " value= "起始时间 "> </h:outputLabel>
<h:inputText id= "queryTime_1 " required= "false " rendered= "true " value= "#{dcmlogBean.queryTime_1} ">
<f:validator vlidatorId= "abs.obs.controller.log.date "/>
</h:inputText>
<h:message style= "color: red; text-decoration: overline " id= "queryTime_1Error " for= "queryTime_1 "/>
<h:outputLabel for= "queryTime_2 " rendered= "true " value= "截止时间 "> </h:outputLabel>
<h:inputText id= "queryTime_2 " required= "false " rendered= "true " value= "#{dcmlogBean.queryTime_2} ">
<f:validator validatorId= "abs.obs.controller.log.date "/>
</h:inputText>
<h:message style= "color: red; text-decoration: overline " id= "queryTime_2Error " for= "queryTime_2 "/>
<h:outputLabel for= "dcmlogText " rendered= "true " value= "日志内容 "> </h:outputLabel>
<h:inputText size= "40 " id= "dcmlogText " required= "false " rendered= "true " value= "#{dcmlogBean.queryText} "> </h:inputText>
<h:commandButton style= "display: block; margin: 10; " id= "query " action= "#{dcmlogBean.querydcmlog} " rendered= "true " value= "查询 "> </h:commandButton>
</h:panelGrid>
</h:panelGroup>
</h:panelGroup>
<h:panelGroup styleClass= "section ">
<h:dataTable id= "items " value= "#{dcmlogBean.dcmlogModel} "
border= "2 " var= "dcmlog " rowClasses= "oddRow, evenRow " rendered= "true " style= " " cellspacing= "2 " cellpadding= "2 " styleClass= "orders "
headerClass= "logsHeader " columnClasses= "evenColumn,oddColumn ">
<f:facet name= "header " >
<h:outputText value= "日志查询结果 " />
</f:facet>
<h:column >
<f:facet name= "header ">
<h:outputText value= "日志内容 " />
</f:facet>
<h:commandLink >
<h:outputText value= "#{dcmlog.text} " />
</h:commandLink>
</h:column>
<h:column >
<f:facet name= "header ">
<h:outputText value= "日志记录时间 " />
</f:facet>
<h:commandLink >
<h:outputText value= "#{dcmlog.record_time} " />
</h:commandLink>
</h:column>
</h:dataTable>
<h:commandLink action= "#{dcmlogBean.firstPage} " >
<h:graphicImage value= "../../image/arrow-first.gif " />
</h:commandLink>
<h:commandLink action= "#{dcmlogBean.forwardPage} ">
<h:graphicImage value= "../../image/arrow-fr.gif " />
</h:commandLink>
<h:commandLink action= "#{dcmlogBean.nextPage} ">
<h:graphicImage value= "../../image/arrow-ff.gif " />
</h:commandLink>
<h:commandLink action= "#{dcmlogBean.lastPage} ">
<h:graphicImage value= "../../image/arrow-last.gif " />
</h:commandLink>
</h:panelGroup>
</h:form>
</f:view>
</body>
</html>
***************************
validator类的代码如下:
package abs.obs.controller.log;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import java.util.*;
import java.text.*;
public class dateValidator implements Validator {
public void validate(FacesContext context,
UIComponent component,
Object obj)
throws ValidatorException {
//System.out.println( "Validator ");
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd ");
String date_str = (String) obj;
try{
Date date = format.parse(date_str);
if (!(date.equals(format.format(date_str)))) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"输入日期格式不正确 ",
"输入日期格式应为yyyy-mm-dd ");
throw new ValidatorException(message);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
*************************************
faces-config.xml中相关的设置为:
<validator>
<validator-id>
abs.obs.controller.log.date
</validator-id>
<validator-class>
abs.obs.controller.log.dateValidator
</validator-class>
</validator>
************************
不知道为什么,验证异常能打印出来,就是通过message显示到页面上,不知道是不是message用的不太对。
[解决办法]
关注
[解决办法]
格式错了
[解决办法]
格式
[解决办法]
xml