Spring Web-用WebApplicationInitializer替换web.xml给了我404

时间:2014-08-19 07:08:24

标签: eclipse spring maven tomcat spring-mvc

我试图将我的webapp配置从面向基于Java的xml重写。带注释的配置文件与web.xml结合使用效果很好但是一旦我用WebApplicationInitializer类替换web.xml,我得到的是404 - 请求的资源(/WebApp/)不可用。

的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    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"
    version="3.0">

    <display-name>Servlet 3.0 Web Application</display-name>

    <servlet>
        <servlet-name>spring</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>
                webapp.config.AppConfig
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <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>webapp.config.PersistenceConfig</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <filter>
        <filter-name>encoding-filter</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>encoding-filter</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>

</web-app>

和我的 WebApplicationInitializer 对应于web.xml

package webapp.init;
import webapp.config.*;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class AppInitializer implements WebApplicationInitializer{

    @Override
    public void onStartup(ServletContext servletContext) {

        AnnotationConfigWebApplicationContext PeristenceContext = new AnnotationConfigWebApplicationContext();
        PeristenceContext.register(PersistenceConfig.class);

        servletContext.addListener(new ContextLoaderListener(PeristenceContext));

        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(AppConfig.class);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

我还发现如果我用dispatcher.addMapping("/");替换dispatcher.addMapping("/*");我没有获得404,我的应用程序将加载,但所有jsp页面都被浏览器包装到另一个{{1 - 不知道这是否表明了什么,但如果有人能够解释这一点我也会非常感激

和我的 pom.xml ,我怀疑这可能也是错误的

<html> and <body> tag

1 个答案:

答案 0 :(得分:2)

默认情况下,tomcat将其内部默认servlet影响到URL /。已知第一版的tomcat 7.0.x不允许以编程方式覆盖它。

也许您应该将tomcat升级到更新版本。