|
||||||||||
前のクラス 次のクラス | フレームあり フレームなし | |||||||||
概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド |
java.lang.Objectorg.springframework.web.servlet.handler.SimpleMappingExceptionResolver
jp.terasoluna.fw.web.rich.springmvc.servlet.handler.SimpleMappingExceptionResolverEx
public class SimpleMappingExceptionResolverEx
例外クラス名とビュー名のマッピングを行うException resolverの拡張クラス。
Springが提供する SimpleMappingExceptionResolverの
属性exceptionMappingsは、設定項目の順序性の保持が出来ない。
本クラスは、SimpleMappingExceptionResolverを拡張し、
代わりに順序性の保持が可能な属性linkedExceptionMappingsを提供するものである。
リクエストコントローラで発生した例外をハンドリングし、
Bean定義ファイルに従いスローされた例外型に対応するViewインスタンスと
Modelインスタンスを返却する。
Modelインスタンスに情報を格納する処理は、ExceptionResolveDelegator
実装クラスに委譲する。
Viewインスタンスで、Modelインスタンスに格納された情報を利用して
エラーレスポンスのレンダリングを行うことを想定している。
レスポンスに例外が発生したことを通知するヘッダを設定する。
ヘッダに情報を設定する処理は、ExceptionResolveDelegator
実装クラスに委譲する。
本クラスを利用する場合、本クラスのBean定義を行うこと。 また、以下のプロパティを必ず設定すること。
属性名 | 必須 | 説明 |
---|---|---|
linkedExceptionMappings | ○ |
例外クラス名とView名のマッピング(map形式)。 entryのキーは、例外クラス名を設定する。 entryの値は、使用する ExceptionResolveDelegator 実装クラスのjavadocを参考に設定すること。
|
exceptionResolveDelegatorClass | ○ | ExceptionResolveDelegator実装クラス |
exceptionResolveDelegatorParams |
ExceptionResolveDelegator実装クラスが利用する情報のマッピング。 entryのキーは、情報名。 entryの値は、情報値。 |
|
outputErrorLogHandledException |
ハンドリングした例外の情報をエラーログ出力するかをあらわすboolean値。 trueの場合、エラーログ出力を行う。 デフォルトはtrueである。 プロジェクトごとのログ監視の要件などで ハンドリングした例外の情報をエラーログ出力したくない場合のみ設定すること。 通常は本設定を利用する必要はない。 |
Bean定義ファイル
の設定例】
<bean id="handlerExceptionResolver"
class="jp.terasoluna.fw.web.rich.springmvc.servlet.handler.SimpleMappingExceptionResolverEx">
<property name="linkedExceptionMappings">
<map>
<entry key="jp.terasoluna.fw.web.rich.exception.UnknownRequestNameException">
<value>exception,kind01,8004C003</value>
</entry>
<entry key="org.springframework.validation.BindException">
<value>bindException,kind02</value>
</entry>
<entry key="jp.terasoluna.fw.service.rich.exception.SystemException">
<value>systemException,kind03</value>
</entry>
<entry key="jp.terasoluna.fw.service.rich.exception.ServiceException">
<value>serviceException,kind04</value>
</entry>
<entry key="java.lang.Exception">
<value>exception,kind05,8004C999</value>
</entry>
</map>
</property>
<property name="exceptionResolveDelegatorClass" value="jp.terasoluna.fw.web.rich.springmvc.servlet.handler.ExceptionResolveDelegatorImpl" />
<property name="exceptionResolveDelegatorParams">
<map>
<entry key="errorTypeHeaderName">
<value>errorType</value>
</entry>
</map>
</property>
</bean>
たとえば、上記の設定を行った上で、
UnknownRequestNameExceptionがスローされてきた場合、
「exception」という名称のビューと、
スローされた例外インスタンスと、エラーコード文字列「8004C003」を
格納したモデルを返却する。
レスポンスヘッダには、「errorType」をキーとして「kind01」を設定する。
SimpleMappingExceptionResolverExは、スローされた例外の型と linkedExceptionMappingsのキーを設定ファイルに記述された順に比較する。 設定ファイルに記述された例外型が、スローされた例外型の 同一の型、または親の型だった場合に、対応する値の名称のViewを生成し返却する。 スローされた型と一致する設定が複数あった場合は、 より先に記述した設定が採用される。 最後に全ての例外の親クラスとなるjava.lang.Exceptionの設定を行うことで、 予期せぬ例外が発生した場合でも必ず、java.lang.Exceptionの設定で エラーレスポンスのレンダリングが出来る。
ここで処理可能な例外は、リクエストコントローラで発生した例外のみである。 ViewやFilter等、DispacherServlet外で発生した例外のハンドリングは、 本クラスは責務を持たない。 それらの例外をハンドリングするためには、Servletが提供している エラーページの機能を利用すること。 例外の型ごとに任意の固定電文をレスポンスにレンダリングすることが出来る。
【web.xml
の設定例】
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error/unknown-error.jsp</location>
</error-page>
※ あらかじめ固定のエラー電文を記述した/error/unknown-error.jspを用意しておくこと。
ExceptionResolveDelegator
フィールドの概要 | |
---|---|
protected String |
defaultErrorView
スローされた例外に対応する設定が無かった場合に実行するViewの名称。 |
protected Integer |
defaultStatusCode
例外が発生した場合にレスポンスに設定するエラーコード。 |
protected Class<? extends ExceptionResolveDelegator> |
exceptionResolveDelegatorClass
ExceptionResolveDelegator実装クラスの型 |
protected LinkedHashMap<String,ExceptionResolveDelegator> |
exceptionResolveDelegatorMap
例外の型をキーにExceptionResolveDelegatorを格納するMap |
protected Map<String,String> |
exceptionResolveDelegatorParams
ExceptionResolveDelegatorのパラメーターのキーと値を格納するMap |
protected Map<String,Object> |
linkedExceptionMappings
順序性を保持した例外とView名(&エラー情報)のマッピング。 |
protected Set |
mappedHandlers
本クラスが処理するべきハンドラのセット。 |
protected boolean |
outputErrorLogHandledException
ハンドリングした例外の情報をエラーログ出力するか |
クラス org.springframework.web.servlet.handler.SimpleMappingExceptionResolver から継承されたフィールド |
---|
DEFAULT_EXCEPTION_ATTRIBUTE, logger |
インタフェース org.springframework.core.Ordered から継承されたフィールド |
---|
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE |
コンストラクタの概要 | |
---|---|
SimpleMappingExceptionResolverEx()
|
メソッドの概要 | |
---|---|
void |
afterPropertiesSet()
本クラスがインスタンス化された直後に呼ばれるメソッド。 |
org.springframework.web.servlet.ModelAndView |
resolveException(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Object handler,
Exception ex)
スローされた例外に対応するViewとModelを返却する。 |
void |
setDefaultErrorView(String defaultErrorView)
スローされた例外に対応する設定が無かった場合に実行Viewの名称を設定する。 |
void |
setDefaultStatusCode(int defaultStatusCode)
例外が発生した場合にレスポンスに設定するエラーコードを設定する。 |
void |
setExceptionMappings(Properties mappings)
推奨されていません。 代わりにlinkedExceptionMappingsを利用すること。 |
void |
setExceptionResolveDelegatorClass(Class<? extends ExceptionResolveDelegator> exceptionResolveDelegatorClass)
例外とビュー名(&エラー情報)のマッピングを保持するクラスを設定する |
void |
setExceptionResolveDelegatorParams(Map<String,String> exceptionResolveDelegatorParams)
ExceptionResolveDelegatorのパラメーターのキーと値を格納するMapを設定する |
void |
setLinkedExceptionMappings(Map<String,Object> linkedExceptionMappings)
順序性を保持する順序性を保持した例外とView名(&エラー情報)の マッピングを設定する。 |
void |
setMappedHandlers(Set mappedHandlers)
mappedHandlersを設定する。 |
void |
setOutputErrorLogHandledException(boolean outputErrorLogHandledException)
ハンドリングした例外の情報をエラーログ出力するかのboolean値を設定する。 |
クラス org.springframework.web.servlet.handler.SimpleMappingExceptionResolver から継承されたメソッド |
---|
applyStatusCodeIfPossible, buildLogMessage, determineStatusCode, determineViewName, doResolveException, findMatchingViewName, getDepth, getModelAndView, getModelAndView, getOrder, logException, setExceptionAttribute, setMappedHandlerClasses, setOrder, setWarnLogCategory, shouldApplyTo |
クラス java.lang.Object から継承されたメソッド |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
フィールドの詳細 |
---|
protected Map<String,Object> linkedExceptionMappings
protected Set mappedHandlers
処理すべきコントローラインスタンスを設定する。
resolveException(HttpServletRequest, HttpServletResponse, Object, Exception)
メソッド内で使用される。コントローラオブジェクトの比較を行うため、
コントローラをシングルトン設定にしなければならない。
protected Integer defaultStatusCode
protected String defaultErrorView
protected Class<? extends ExceptionResolveDelegator> exceptionResolveDelegatorClass
protected LinkedHashMap<String,ExceptionResolveDelegator> exceptionResolveDelegatorMap
protected Map<String,String> exceptionResolveDelegatorParams
protected boolean outputErrorLogHandledException
コンストラクタの詳細 |
---|
public SimpleMappingExceptionResolverEx()
メソッドの詳細 |
---|
public void afterPropertiesSet()
org.springframework.beans.factory.InitializingBean
内の afterPropertiesSet
public void setDefaultErrorView(String defaultErrorView)
org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
内の setDefaultErrorView
defaultErrorView
- スローされた例外に対応する設定が無かった場合に実行Viewの名称public void setExceptionResolveDelegatorClass(Class<? extends ExceptionResolveDelegator> exceptionResolveDelegatorClass)
exceptionResolveDelegatorClass
- 例外とビュー名(&エラー情報)のマッピングを保持するクラスpublic void setExceptionResolveDelegatorParams(Map<String,String> exceptionResolveDelegatorParams)
exceptionResolveDelegatorParams
- ExceptionResolveDelegatorのパラメーターのキーと値を格納するMappublic void setMappedHandlers(Set mappedHandlers)
org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
内の setMappedHandlers
mappedHandlers
- mappedHandlerspublic void setDefaultStatusCode(int defaultStatusCode)
org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
内の setDefaultStatusCode
defaultStatusCode
- HTTPステータスコード値@Deprecated public void setExceptionMappings(Properties mappings)
org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
内の setExceptionMappings
mappings
- 例外とView名のマッピングpublic void setLinkedExceptionMappings(Map<String,Object> linkedExceptionMappings)
linkedExceptionMappings
- 順序性を保持した例外とView名(&エラー情報)のマッピングpublic void setOutputErrorLogHandledException(boolean outputErrorLogHandledException)
デフォルトはtrueなのでプロジェクトごとのログ監視の要件などで ハンドリングした例外の情報をエラーログ出力したくない場合のみ本メソッドを利用すること。 通常は本メソッドを利用する必要はない。
outputErrorLogHandledException
- falseならば出力しない。public org.springframework.web.servlet.ModelAndView resolveException(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler, Exception ex)
org.springframework.web.servlet.HandlerExceptionResolver
内の resolveException
org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
内の resolveException
request
- HTTPリクエストresponse
- HTTPレスポンスhandler
- ハンドラex
- スローされた例外
|
||||||||||
前のクラス 次のクラス | フレームあり フレームなし | |||||||||
概要: 入れ子 | フィールド | コンストラクタ | メソッド | 詳細: フィールド | コンストラクタ | メソッド |