【发布时间】:2017-04-25 14:48:35
【问题描述】:
我正在尝试从基于 java 的 Spring 4 配置项目调度到 html(而不是 jsp)。
这是 ApplicationContextConfig
@Configuration
@EnableWebMvc
@ComponentScan("net.codejava.spring")
@EnableTransactionManagement
public class ApplicationContextConfig {
@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/html/");
viewResolver.setSuffix(".html");
return viewResolver;
}
这是 SpringWebAppInitializer
public class SpringWebAppInitializer extends WebMvcConfigurerAdapter implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(ApplicationContextConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/images/**").addResourceLocations("/images/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
}
}
这是 webapp 文件夹:
这是 Spring 控制器:
@Controller
public class HomeController {
@RequestMapping("/")
public ModelAndView showLogin(){
return new ModelAndView("login");
}
}
但是我收到了这个警告:
在名称为“SpringDispatcher”的 DispatcherServlet 中找不到带有 URI [/spring/html/login.html] 的 HTTP 请求的映射
而且 html 不显示。
【问题讨论】:
标签: java html spring spring-mvc