2

Spring 3.2.1 を使用して、アノテーション ベースのカスタム HTTP 404 エラー ページを実装することは可能ですか? さまざまなフォーラムで方法を探しましたが、明確な答えが見つかりませんでした。

web.xml を使用して構成も試みましたが、マップされていない URL にアクセスすると機能しません。何か助けてください。

ログ出力

  7259 [DEBUG] org.springframework.web.servlet.DispatcherServlet  - DispatcherServlet with name 'spring-test' processing GET request for [/spring-test/ss]
  7261 [DEBUG] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Looking up handler method for path /ss
  7262 [DEBUG] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Did not find handler method for [/ss]
  7262 [WARN ] org.springframework.web.servlet.PageNotFound  - No mapping found for HTTP request with URI [/spring-test/ss] in DispatcherServlet with name 'spring-test'
  7262 [DEBUG] org.springframework.web.servlet.DispatcherServlet  - Successfully completed request

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="rest" version="3.0" metadata-complete="true">

    <!-- The definition of the Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>main.java.net.bornil.config</param-value>
    </context-param>   

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>main.java.net.bornil.config</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file />
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/errors/404</location>
    </error-page>
</web-app>

コントローラ

@Controller
@RequestMapping(value = "/errors")
public class CommonExceptionHandler {

    private static Logger log = Logger.getLogger(CommonExceptionHandler.class.getName());

    @RequestMapping(method = RequestMethod.GET, value = "/{code}")
    public ModelAndView handleException(@PathVariable int code) {

        if (log.isDebugEnabled()) {
            log.debug("ERROR CODE IS: " + code);
        }

        return new ModelAndView("errors/404");
    }

}
4

1 に答える 1

0

同じ問題がありました。私は単純な webapp、単一のページを持っており、Spring フォーラムで朝を過ごした後 (長さが 3 文字しかないため、「404」を検索することはできません)、Google はこれが私が見つけた最良のソリューションです。index.jspあなたが aと aを持っていると仮定すると404.jsp、私@Configurationの ' は:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
    registry.addViewController("/*").setViewName("404");
}
于 2013-05-15T09:45:17.317 に答える