1

您好我正在尝试使用 Spring MVC Java 配置和内容视图解析器。

文件:-

public class WebInitializer implements WebApplicationInitializer {

/*
 * (non-Javadoc)
 * 
 * @see
 * org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet
 * .ServletContext)
 */
@Override
public void onStartup(ServletContext servletContext)
        throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(SpringMvcConfig.class);
    ctx.setServletContext(servletContext);
    ctx.refresh();
    Dynamic servlet = servletContext.addServlet("dispatcher",
            new DispatcherServlet(ctx));
    servlet.addMapping("/");
    servlet.setLoadOnStartup(1);
    servlet.setAsyncSupported(false);
}

}

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.sambhav.mvc.controller" })
public class SpringMvcConfig extends WebMvcConfigurerAdapter {

    /*
     * @Bean public UrlBasedViewResolver setupViewResolver() {
     * UrlBasedViewResolver resolver = new UrlBasedViewResolver();
     * resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp");
     * resolver.setViewClass(TilesView.class); return resolver; }
     */

    @Bean
    public TilesViewResolver getTilesViewResolver() {
        TilesViewResolver tilesViewResolver = new TilesViewResolver();
        /*
         * tilesViewResolver.setPrefix("/WEB-INF/jsp/");
         * tilesViewResolver.setSuffix(".jsp");
         */
        tilesViewResolver.setViewClass(TilesView.class);
        return tilesViewResolver;
    }

    @Bean
    public TilesConfigurer getTilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setCheckRefresh(true);
        tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/tiles.xml" });
        return tilesConfigurer;
    }

    @Override
    public void configureContentNegotiation(
            ContentNegotiationConfigurer configurer) {
        configurer.defaultContentType(MediaType.TEXT_HTML)
                .mediaType("json", MediaType.APPLICATION_JSON)
                .mediaType("xml", MediaType.APPLICATION_XML)
                .favorPathExtension(true); // default is true. just for clarity
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/*").addResourceLocations(
                "/WEB-INF/css/*");
        registry.addResourceHandler("/js/*").addResourceLocations(
                "/WEB-INF/js/*");
        registry.addResourceHandler("/img/*").addResourceLocations(
                "/WEB-INF/img/*");
    }

}

尝试运行应用程序时,我没有注册调度程序 servlet - 没有出现相关信息日志。在带有断点的调试模式下运行 WebInitializer 也表明它没有被调用。

Jul 12, 2013 5:06:57 PM com.springsource.tcserver.serviceability.rmi.JmxSocketListener init
INFO: Started up JMX registry on 127.0.0.1:6969 in 68 ms
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 573 ms
Jul 12, 2013 5:06:57 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jul 12, 2013 5:06:57 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: VMware vFabric tc Runtime 2.8.2.RELEASE/7.0.35.B.RELEASE
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor D:\Installations\sts3.2\vfabric-tc-server-developer-2.8.2.RELEASE\base-instance\conf\Catalina\localhost\spring-mvc-config.xml
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.SetContextPropertiesRule begin
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:spring-mvc-config' did not find a matching property.
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory D:\Installations\sts3.2\vfabric-tc-server-developer-2.8.2.RELEASE\base-instance\webapps\manager
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory D:\Installations\sts3.2\vfabric-tc-server-developer-2.8.2.RELEASE\base-instance\webapps\ROOT
Jul 12, 2013 5:06:57 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 311 ms

这导致 404 at / 或任何其他路径。

我想知道这里出了什么问题。

谢谢你的帮助。

4

2 回答 2

1

我知道它的老问题。使用更新的 Spring Boot,现在配置任何 Spring 应用程序要容易得多。请在此处参考 Spring Boot 入门项目:-

Spring Boot 博客

于 2014-11-07T14:17:37.197 回答
0

可能的原因

  1. 网页.xml。我建议您完全删除它并使用版本高于 3.0 的 servlet-api。

  2. 控制器中的 @RequestMapping 不正确。请给他们看。

最后,您可以在这里找到带有 java 配置的基本 Spring MVC 应用程序的良好分步指南: http ://www.javacodegeeks.com/2013/03/spring-mvc-creation-of-a-simple-controller -with-java-based-config.html

于 2014-02-13T10:28:34.293 回答