开发环境为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 validatorId="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:messages layout="table" style="color:red"/>
<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>
<ig:column style="background-color: #e6e6e6; border-right: 1px solid silver; text-align: center;">
<h:graphicImage url="../../image/world16.gif" />
</ig:column>
<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());
}
/*
public boolean isDate(String date){
try{
return java.text.DateFormat.getDateInstance().parse(date)!=null;
}catch(java.text.ParseException e){
return false;
}
}
*/
}
}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用的不太对。
页面代码:<%@ 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 validatorId="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:messages layout="table" style="color:red"/>
<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>
<ig:column style="background-color: #e6e6e6; border-right: 1px solid silver; text-align: center;">
<h:graphicImage url="../../image/world16.gif" />
</ig:column>
<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());
}
/*
public boolean isDate(String date){
try{
return java.text.DateFormat.getDateInstance().parse(date)!=null;
}catch(java.text.ParseException e){
return false;
}
}
*/
}
}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用的不太对。
<%@ 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 validatorId="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用的不太对。
<h:messages showDetail="true"/>这个试下
虽然可以,但是英文提示,建议你别用什么Validator,很麻烦,
直接在set方法里面调用ValidatorDate方法,有问题就提交错误,这样连注册都省了,而且jsp页面也很干净。
<h:inputText id="card" value="#{pb.card}"
validator="#{valid.validateEmail}" required="true">
</h:inputText>
对应的validateEmail:
public void validateEmail(FacesContext context, UIComponent toValidate,
Object value) {
String email = (String) value;
if (email.indexOf('@') == -1) {
String error = getResourceMessage(context, "email_error");
getMessage(context, toValidate, error);
}
}getResourceMessage:
public String getResourceMessage(FacesContext context, String message_desc) {
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle(
"message", context.getViewRoot().getLocale());
String error = bundle.getString(message_desc);
return error;
}
属性文件:
message.properties
[del][b][size=14px][/size][size=16px][/size[b][i][u]][/b][/b][/i][/u][/del]