そのためには、春のSimpleExceptionMappingResolver
を使用できます。あなたのprojet-servlet.xml
<!-- ExceptionResolverMapping: generate exception specific view -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.rail.eseva.exception.UnexpectedException">/unexpectedExceptionView</prop>
</props>
</property>
<property name="defaultErrorView" value="/genericExceptionView"/>
</bean>
でこれを設定する必要があります今、あなたごとにこの例外をスローすることができます
public class UnexpectedException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String message;
private Date date;
public UnexpectedException(String message, Date date) {
this.date=date;
this.message=message;
}
/**
* @return the message
*/
@Override
public String getMessage() {
return message;
}
/**
* @return the date
*/
public Date getDate() {
return date;
}
@Override
public String toString() {
return message +" "+date.getTime();
}
}
と汎用的な例外ビューページ
<body>
<h2>Exception occured at: <fmt:formatDate value="${exception.date}" pattern="dd.MM.yyyy"/></h2>
<h2>Exception message :</h2> ${exception.message}
</body>
like-カスタム例外クラスを作成します要件:
throw new UnexpectedException(e.getMessage(), new Date());
こんにちは、お返事ありがとうございます。私の場合、誰かがURLを変更すると、そのデフォルトのエラーページを表示する方法。その場合、コントローラ名は見つかりません。次に、デフォルトのエラーページを表示する必要があります。 – suneel
これが有効なURLでない場合、ページは表示されません(ブラウザ固有の機能)。これが有効なURLであり、処理したくない場合は、コントローラのロジックを処理できます。 –