【发布时间】:2015-10-30 19:39:52
【问题描述】:
我正在通过spring-boot 启动一个嵌入式tomcat,并希望将静态index.html 页面作为正在运行的应用程序的一部分。
但以下不起作用:
@SpringBootApplication
public class HMyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@RestController
public class HomeContoller {
@RequestMapping("/")
public String index() {
return "index";
}
}
src/main/resources/static/index.html
结果:当我调用localhost:8080 时,我只看到“索引”一词,但没有看到我的 html 页面。为什么?
【问题讨论】:
-
这是因为
@RestController是@Controller和ResponseBody的元注释,这意味着它将“索引”写入响应输入流。您应该改用@Controller,以便将“索引”解析为视图名称。 -
天哪,你是对的,绝对的。我可能对并排开发
@RestController感到困惑。无论如何,我现在使用@Controller得到以下异常:javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'。而是返回index.html都不起作用。 -
我认为视图名称应该是
static/index.html -
static/index和static/index.html都不起作用。 -
在下面看我的答案,我解决了。
标签: java spring spring-mvc spring-boot