[英]Trouble rendering thymeleaf with springboot
Hello everyone i am trying to run a simple springboot application. 大家好,我正在尝试运行一个简单的springboot应用程序。 And trying to view a basic html but have trouble rendering the page. 并尝试查看基本的html却无法呈现页面。 I can only see the string on browser but not the actual html contents. 我只能在浏览器上看到该字符串,而看不到实际的html内容。 Please tell me what i did wrong. 请告诉我我做错了。
TestController.java: TestController.java:
@Controller
public class TestController {
@RequestMapping(value="/person")
@ResponseBody
public String intro(){
System.out.println("HH");
return "index";
}
}
MainApplication.java: MainApplication.java:
@SpringBootApplication
@ComponentScan(basePackages = "pathToMyControllerFolder")
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
created index.html in /rsesources/template folder as was mentioned on spring doc: index.html: 如春天文档中所述在/ rsesources / template文件夹中创建index.html:index.html:
<!DOCTYPE html>
<head lang="en">
<title>Spring Framework Guru</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Hello</h1>
<h2>Fellow Spring Framework Gurus!!!</h2>
</body>
</html>
But i cannot vies the html contents but rather the string index. 但是我无法浏览html内容,而是查看字符串索引。 Earlier had whitePage label error and fixed it by adding the @ComponentScan but now only displaying index and not actual html contents. 之前有whitePage标签错误,并通过添加@ComponentScan进行了修复,但现在仅显示索引,而不显示实际的html内容。
And when i checked the log info on the console i noticed this: 当我在控制台上检查日志信息时,我注意到了这一点:
2015-11-15 13:22:51.442 INFO 11460 --- [lication.main()] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-11-15 13:22:51.442 INFO 11460 --- [lication.main()] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
Remove the @ResponseBody
annotation. 删除@ResponseBody
批注。 With that annotation whatever you return from the controller is the response body. 使用该注释,您从控制器返回的任何内容都是响应主体。 In your case you return the index
string, so that's the whole response body. 在您的情况下,您将返回index
字符串,因此这就是整个响应正文。 Removing the annotation will cause the index
string to be interpreted as a view name. 删除注释将导致index
字符串被解释为视图名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.