Java 在日历中设置开始日期
我需要在日历组件中设置开始日期。我在ManagedBean中有一个名为Java 在日历中设置开始日期,java,jsf,calendar,richfaces,Java,Jsf,Calendar,Richfaces,我需要在日历组件中设置开始日期。我在ManagedBean中有一个名为dateInitial的变量,该变量有一个日期(即2013-07-07是下限日期),我配置了日历组件: <rich:calendar id="dateToEvaluate" popup="true" required="true" requiredMessage="Required Field" buttonIcon="#{request.contextPath}/resources/images/c
dateInitial
的变量,该变量有一个日期(即2013-07-07是下限日期),我配置了日历组件:
<rich:calendar id="dateToEvaluate" popup="true"
required="true" requiredMessage="Required Field"
buttonIcon="#{request.contextPath}/resources/images/calendar.gif"
value="#{valueMB.dateToEvaluate}"
datePattern="dd/MM/yyyy">
</rich:calendar>
但是我需要验证它的值是否大于我的变量dateInitial
,如果不是的话。。然后启动带有错误的消息验证程序
谢谢你的进步!我很抱歉我的英语不好 如果要限制用户选择的日期不早于初始日期,可以使用rich:calendar的
dayDisableFunction
属性
<rich:calendar isDayEnabled="disableBeforeInitialDay" …>
您可以使用javascript函数,如
function disableBeforeInitialDay(day) {
Date initialDay = new Date(2014,1,27); //equals to 27th Feb 2014
return (initialDay.getTime() - day.date.getTime() < 0);
}
函数在初始日期之前禁用(天){
日期初始日期=新日期(2014,1,27);//等于2014年2月27日
返回(initialDay.getTime()-day.date.getTime()<0);
}
上述代码禁用了所有早于初始日期的日期
<rich:calendar isDayEnabled="disableBeforeInitialDay" …>
如果您想在服务器端执行此操作,请编写自己的验证程序类
<rich:calendar .....>
<f:validator validatorId="myDateValidator" />
</rich:calendar>
@FacesValidator("myDateValidator")
public class MyDateValidator implements Validator {
@override
public void validate(FacesContext context, UIComponent comp, Object value) throws
ValidatorException {
Date date = (Date) value;
Date refDate = new Date(2014,1,27);
if (date.before(refDate )) {
String message = "Date is earlier than initial date.";
throw new ValidatorException(new FacesMessage(message));
}
}
}
@FacesValidator(“myDateValidator”)
公共类MyDateValidator实现验证器{
@凌驾
公共无效验证(FacesContext上下文、UIComponent comp、对象值)抛出
验证器异常{
日期=(日期)值;
日期参考日期=新日期(2014年1月27日);
如果(参考日期之前的日期)){
String message=“日期早于初始日期。”;
抛出新的ValidatorException(新的FacesMessage(消息));
}
}
}
如果您使用的是richfaces 4.X,则属性应为dayDisableFunction
希望这能有所帮助感谢您的回复Srikanth,我正在做您的第二个选择,但我有一个问题,我如何从ManagedBean或其他组件UI获取初始日期??。因为我测试了你的例子,效果很好!但我需要从MB或ComponentUI以友好方式获取日期(在您的示例中为refDate)。谢谢您可以使用@Candres作为请求参数,我觉得更好的方法是将属性添加到日历中。您可以这样做,作为rich:calendar的子标记。您可以在验证器中以String param=(String)component.getAttributes().get(“initialdate”)的形式访问它;在这种方法中,无需使用FacesContext。非常感谢您!它就像一个符咒!!我添加了f:attribute,正如您在上面所指出的,我可以通过component.getAttributes().get(“initialdate”)轻松获取参数。。再次感谢你,伙计!