【问题标题】:No mapping found for HTTP request with URI with annotation config Spring MVC and Jetty找不到带有带有注释配置 Spring MVC 和 Jetty 的 URI 的 HTTP 请求的映射
【发布时间】:2014-06-22 20:28:36
【问题描述】:

我正在使用仅注释配置,服务器启动但当我访问某些页面时,显示错误:

警告:找不到带有 URI 的 HTTP 请求的映射 DispatcherServlet 中的 [/WEB-INF/view/main.jsp] 名称 'org.springframework.web.servlet.DispatcherServlet-d7259e'

控制器映射工作正常,但没有加载 JSP 页面。

我正在使用 Jetty 9.2.0.M0 和 Spring MVC 4.0.4-RELEASE。

码头配置:

private static final String CONTEXT_PATH = "/";
private static final String MAPPING_URL = "/*";

private void startServer(int port) throws Exception {
        Server server = new Server(port);
        server.addLifeCycleListener(new LifeCycleListener());
        server.setHandler(getServletContextHandler(getContext()));
        server.start();
        server.join();
    }

    private static ServletContextHandler getServletContextHandler(WebApplicationContext context) throws IOException {
        LOGGER.info("Preparing ServletContextHandler");
        ServletContextHandler contextHandler = new ServletContextHandler();
        contextHandler.setContextPath(CONTEXT_PATH);
        contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL);
        contextHandler.addEventListener(new ContextLoaderListener(context));

        return contextHandler;
    }

    private static WebApplicationContext getContext() {
        LOGGER.info("Preparing annotation context");
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();    
        context.register(SpringMVCConfig.class);
        return context;
    }

SpringMVC配置:

@Configuration
@ComponentScan(basePackages = "my.package.controller")
@EnableWebMvc
public class SpringMVCConfig extends WebMvcConfigurerAdapter {

    private static final Logger LOGGER = LogManager.getLogger(SpringMVCConfig.class);

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        LOGGER.info("Adding resource handlers");
        registry.addResourceHandler("/i/**").addResourceLocations("classpath:WEB-INF/images/");
        registry.addResourceHandler("/c/**").addResourceLocations("classpath:WEB-INF/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:WEB-INF/javascripts/");
    }

    @Bean
    public ViewResolver prepareViewResolver() {
        LOGGER.info("Returning view resolver");
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("WEB-INF/view/");
        resolver.setSuffix(".jsp");
        // resolver.setViewClass(JstlView.class);
        return resolver;
    }

}

没有 web.xml 也没有任何其他 xml 文件。

谢谢。

【问题讨论】:

    标签: jsp spring-mvc jetty-9


    【解决方案1】:

    在 GitHub 上使用这个骨架,这是一个工作示例 - https://github.com/jasonish/jetty-springmvc-thymeleaf-template

    【讨论】:

      猜你喜欢
      • 2014-12-09
      • 2018-09-27
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 2017-04-24
      • 2011-11-04
      • 1970-01-01
      • 2013-07-27
      相关资源
      最近更新 更多