我使用嵌入式Tomcat的Spring Boot。 当它启动时,它会登录到控制台:
s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped" {[/ home]}"到公共java.lang.String com.vlad.pet.contactlist.webapp.controller.SampleController.helloWorld(org.springframework.ui.Model)
所以我猜这个URL被映射到控制器。 但是http://localhost:8090/home给了我一个错误404。
出现意外错误(type = Not Found,status = 404)。 没有可用的消息
Application.java
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
//overrides the configure() method to point to itself, so Spring can find the main configuration.
@Override
protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
return application.sources(Application.class);
}
@Bean
public ViewResolver getViewResolver() {
final TilesViewResolver resolver = new TilesViewResolver();
resolver.setViewClass(TilesView.class);
return resolver;
}
@Bean
public TilesConfigurer getTilesConfigurer() {
final TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions("WEB-INF/tiles/tiles.xml");
configurer.setCheckRefresh(true);
return configurer;
}
}
SampleController.java
@Controller
public class SampleController {
@RequestMapping (value = "/home")
public String helloWorld(Model model) {
model.addAttribute("pageTitle", "home");
return "base";
}
}
tiles.xml
<tiles-definitions>
<definition name="base" template="/WEB-INF/tiles/basic/basic-template.jsp">
<put-attribute name="head" value="/WEB-INF/tiles/basic/head.jsp" />
</definition>
</tiles-definitions>
答案 0 :(得分:1)
您正在从控制器方法返回'home',但我没有在您的项目结构中看到home.jsp。
答案 1 :(得分:1)
我通过启用默认servlet解决了这个问题:
@Configuration
@EnableWebMvc
public class Config extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
答案 2 :(得分:0)
您可以使用@RestController
代替@Controller
。
@RestController
public class SampleController {
...
答案 3 :(得分:0)
就我而言,我的Eclipse项目显示了一个错误。我必须从Eclipse安装mvn,更新maven依赖项,然后从eclipse启动服务器。