Reputation: 99
This is my controller:
@RestController
public class GraphController {
@GetMapping("/displayBarGraph")
public String barGraph(Model model) {
Map<String, Integer> surveyMap = new LinkedHashMap<>();
surveyMap.put("Java", 40);
surveyMap.put("Dev oops", 25);
surveyMap.put("Python", 20);
surveyMap.put(".Net", 15);
model.addAttribute("surveyMap", surveyMap);
return "barGraph";
}
@GetMapping("/displayPieChart")
public String pieChart(Model model) {
model.addAttribute("pass", 50);
model.addAttribute("fail", 50);
return "pieChart";
}
This is my file.proprieties
:
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.enabled=false
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.html
Those are my HTML files:
but it always redirect me like this:
Any solution please?
Upvotes: 2
Views: 4688
Reputation: 13943
Setting spring.thymeleaf.enabled
to false
disables Thymeleaf for Spring MVC/Web. Therefore, the controller just returns the name of the Thymeleaf template instead of going through the Thymeleaf view resolution.
To solve this, remove the spring.thymeleaf.enabled
property or set it to true
which is the default for Thymeleaf auto-configuration:
spring.thymeleaf.enabled=true
Use @Controller
instead of @RestController
when working with a templating engine and you don't want the return value to be bound to the response body:
@Controller
public class GraphController {
@GetMapping("/displayBarGraph")
public String barGraph(Model model) {
// ...
return "barGraph";
}
// ...
}
Your configured template path needs to match the actual path where the templates are stored. So either change setspring.thymeleaf.prefix=classpath:/template/
or rename the template
directory to templates
.
Remove the two properites spring.mvc.view.prefix
and spring.mvc.view.suffix
from your configuration. This would be necessary if you want to use JSP.
Upvotes: 2
Reputation: 15878
Couple of changes to make this work.
Change @RestController to @Controller so that it returns page instead of response in the body.
Rename folder to templates or change the path in properties file for view resolver.
no need of thymeleaf enabled property.
Upvotes: 1
Reputation: 3250
Rename your
src/main/resources/template
folder to
src/main/resources/templates
You are missing "s" in the folder name.
Upvotes: 0