我正在使用仅注释配置,服务器启动但是当我访问某些页面时,显示错误:
警告:在名称为 'org.springframework.web.servlet.DispatcherServlet-d7259e' 的 DispatcherServlet 中找不到带有 URI [/WEB-INF/view/main.jsp] 的 HTTP 请求的映射
控制器映射工作正常,但没有加载 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 文件。
谢谢。