0

私は次のJSFを持っています:

<rich:calendar id="startDate"
                        value="#{myBean.startDate}" defaultTime="00:00"
                        disabled="#{myBean.disabled}"
                        popup="true" required="true" 
                        showWeeksBar="false"
                        dayDisableFunction="isDayEnabled"
                        dayClassFunction="getDisabledStyle"
                        requiredMessage="#{msg.errorStartDateNull}"
                        datePattern="#{myBean.datePattern}"
                        showApplyButton="false" cellWidth="24px" cellHeight="22px" todayControlMode="hidden"
                        style="width:1000px"
                        timeZone="#{myBean.defaultTimeZone}"
                        styleClass="noToday" showFooter="false" converter="calendarConverter" immediate="true">

ここに私のコンバーターがあります:

@FacesConverter("calendarConverter")
public class CalendarConverter extends DateTimeConverter
{
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {
        Date calDate = (Date) component.getAttributes().get("currentDate");

        if (calDate instanceof Date) {
            return super.getAsObject(context, component, value);
        }
        else {
            throw new ConverterException(new FacesMessage("Invalid Date Format"));
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value)
    {
        return super.getAsString(context, component, value);
    }
}

カレンダーの currentDate 属性を確認する必要がありますが、immediate="true" 属性をカレンダー コンポーネントに適用すると null になります。immediate 属性を削除すると、コンバーターは呼び出されず (JSF は独自の変換を使用しているようです)、独自のエラー メッセージを画面に送信します。

java.lang.NumberFormatException: For input string: "2012asd" 

currentDate 属性が日付でない場合、カスタム エラー メッセージを返すにはどうすればよいですか? (改ざん日Firefoxプラグインを介して改ざんされています)

アップデート

コンバーターにコンストラクターを提供して、フォームを送信するときにコンバーターが入ってくるかどうかを確認しました (immediate=true を削除した状態で)。コンストラクターに入りますが、まだ getAsObject メソッドを呼び出していません。

public CalendarConverter()
{
    super();
}

ありがとう

4

1 に答える 1

0

<error-page>web.xmlファイルに追加することで例外を処理できます。

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/errorpage.html</location>
</error-page>

これにより、Exceptionのインスタンスに対してerrorpage.htmlが表示されます。

このリンクからも詳細情報を入手できます。 http://docs.oracle.com/cd/B32110_01/web.1013/b25947/web_val.htm

これがお役に立てば幸いです

于 2012-04-16T10:25:30.233 に答える