前提・実現したいこと
URLが条件に沿わない場合などに404のエラー画面(Tomcatのデフォルトではなく自分で作ったエラー画面)に遷移させたい。現在は、Tomcatの
エラーメッセージ
Caused by: java.lang.IllegalStateException: レスポンスをコミットした後でフォワードできません at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:326) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:316) at org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler.handleRequest(DefaultServletHttpRequestHandler.java:127) at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:53) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) ... 27 more
該当のソースコード
TestInterceptor.java
java
1//条件判定実施。boolean flg = 条件判定処理; flgがfalseなら404に 2if (!flg) { 3 responce.sendError(404); 4 return true; 5}
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/META-INF/spring/applicationContext.xml </param-value> </context-param> <!-- Spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring MVC front controller --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Error pages --> <error-page> <error-code>400</error-code> <location>/WEB-INF/html/error/badRequest.html</location> </error-page> <error-page> <error-code>403</error-code> <location>/WEB-INF/html/error/NoAuthentication.html</location> </error-page> <error-page> <error-code>404</error-code> <location>/WEB-INF/html/error/notFound.html</location> </error-page> <error-page> <error-code>405</error-code> <location>/WEB-INF/html/error/methodNotAllowed.html</location> </error-page> <error-page> <error-code>406</error-code> <location>/WEB-INF/html/error/notAcceptable.html</location> </error-page> <error-page> <error-code>415</error-code> <location>/WEB-INF/html/error/unsupportedMediaType.html</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/html/error/internalServerError.html</location> </error-page> <error-page> <error-code>503</error-code> <location>/WEB-INF/html/error/serviceUnavailable.html</location> </error-page> </web-app>
補足情報(FW/ツールのバージョンなど)
Java8、Tomcat
SpringのInterceptorクラスで作成しています。
回答3件
あなたの回答
tips
プレビュー