【发布时间】:2016-09-11 10:50:24
【问题描述】:
当我在 tomcat 8 中渲染这个应用程序时,我一直得到一个空白页面。我看到的唯一错误是:
o.s.boot.context.web.ErrorPageFilter: Cannot forward to error page for
request [/] as the response has already been committed.
因此我的问题是我无法呈现 index.html 页面。
我拿了一个静态页面并在其中添加了一个 spring-boot 模块。自从我进行更改后,我就无法再次呈现该页面。我在templates下有 index.html 文件
我在控制器中添加了 cmets 以查看它们是否被击中,而它们没有:
@Controller
public class ApplicationController {
@Autowired
ContactService contactService;
@RequestMapping(value="/", method= RequestMethod.GET)
public String contactForm() throws IOException {
System.out.println("Inside GET in Application Controller");
//model.addAttribute("contact", new Contact());
return "index";
}
@RequestMapping(value="/contact", method= RequestMethod.POST)
public String contactSubmit() throws IOException {
System.out.println("Inside POST in Application Controller");
return "index";
}
}
我使用的是 Thymeleaf 但决定不使用它,这就是方法中的参数为空白的原因。
我已将该项目上传到 GIT,以便其他人在需要时更容易四处浏览和下载。
----------------更新1----------------------
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
private static final Logger LOGGER = Logger.getLogger(WebConfig.class);
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/");
registry.addResourceHandler("/images/**").addResourceLocations("/resources/images/");
registry.addResourceHandler("/image/**").addResourceLocations("/resources/image/");
registry.addResourceHandler("/javascripts/**").addResourceLocations("/resources/javascripts/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
真的没有什么可以证明我能想到的了。如果您也愿意,也可以在 GIT 上查看其他文件。
【问题讨论】:
-
不要将 index.html 文件放入模板。它通常在 webapp 目录中或在
static目录下提供。请参阅spring.io/blog/2013/12/19/…、stackoverflow.com/questions/27583278/…。 docs.spring.io/spring-boot/docs/current/reference/html/… -
@AntonNovopashin 我也试过了。我只是将它移到静态文件夹中,它仍然具有相同的行为。
-
请分享您的配置。控制器不够用。 Spring MVC 有几个配置参数
spring.mvc.view.prefix: /WEB-INF/jsp/spring.mvc.view.suffix: .jsp。但通常对于静态页面,您不必这样做。引导示例 github.com/spring-projects/spring-boot/tree/master/… 或 github.com/spring-projects/spring-boot/tree/master/… -
@AntonNovopashin 请参阅更新 1
-
您的 Git 中心链接不再有效。
标签: spring-boot