使用java进行Spring MVC配置

时间:2014-07-19 07:04:09

标签: java spring spring-mvc

我正在使用Apache Tomcat 7.0.54Spring MVC 4.0.1并尝试使用DispatcherServlet配置Java: 我已经编写了配置类:

public class AppInitializer extends AbstractDispatcherServletInitializer{

    @Override
    protected WebApplicationContext createServletApplicationContext() {
        XmlWebApplicationContext context = new XmlWebApplicationContext();
        context.setConfigLocation("/WEB-INF/her-servlet.xml");
        return context;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"*.her"};
    }

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}

并且它不起作用。但如果我写下面的话:

public class AppInitializer implements WebApplicationInitializer{


    @Override
    public void onStartup(ServletContext sc) throws ServletException {
        XmlWebApplicationContext context = new XmlWebApplicationContext();
        context.setConfigLocation("/WEB-INF/her-servlet.xml");
        ServletRegistration.Dynamic registration = sc.addServlet("her", new DispatcherServlet(context));
        registration.setLoadOnStartup(1);
        registration.addMapping("*.her");
    }

}

它会正常工作。我不明白这个问题。你能帮我解决一下吗?

0 个答案:

没有答案