简体   繁体   中英

Trouble rendering thymeleaf with springboot

Hello everyone i am trying to run a simple springboot application. And trying to view a basic html but have trouble rendering the page. I can only see the string on browser but not the actual html contents. Please tell me what i did wrong.

TestController.java:

@Controller
public class TestController {

    @RequestMapping(value="/person")
    @ResponseBody
    public String intro(){
        System.out.println("HH");
        return "index";
    }
}

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:

<!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. Earlier had whitePage label error and fixed it by adding the @ComponentScan but now only displaying index and not actual html contents.

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. 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. Removing the annotation will cause the index string to be interpreted as a view name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM