0

If i delete from pom.xml spring-boot-starter-thymeleaf then my @GetMapping cant return html page. [enter image description here](https://i.stack.imgur.com/NJvOn.png)

I tried:

  1. add @ResponseBody - not working (his return string in web site, not html page)
  2. Replace @Controller by @RestControllerin - not working
  3. Using ModelAndView - not working (modelAndView.setViewName("index" or "index.html" or "static/index.html" or ...)
Igor Makarov
  • 1
  • 2
  • 1

2 Answers2

1
  1. add @ResponseBody - not working (his return string in web site, not html page)

Because the browser will read your response header "Content-Type" to decide how to show the content. So that you need to specify the content type to html.

@GetMapping("/")
public void indexPage(HttpServletResponse response) throws IOException {
    response.setHeader("Content-Type", "text/html;charset=utf-8"); //specify the content is html
    PrintWriter out = response.getWriter();
    out.write("<form action='#' method='post'>");
    out.write("username:");
    out.write("<input type='text' name='username'><br/>");
    out.write("password:");
    out.write("<input type='password' name='password'><br/>");
    out.write("<input type='submit' value='login'>");
    out.write("</form>");
}
HKBN-ITDS
  • 609
  • 1
  • 6
0

Another way to response HTML page without Thymeleaf

@GetMapping("/home")
public String loadHomePage() {
    Resource resource = new ClassPathResource("templates/html/home.html");
    try {
        InputStream inputStream = resource.getInputStream();
        byte[] byteData = FileCopyUtils.copyToByteArray(inputStream);
        String content = new String(byteData, StandardCharsets.UTF_8);
        LOGGER.info(content);
        return content;
    } catch (IOException e) {
        LOGGER.error("IOException", e);
    }
    return null;
}
Al Mamun
  • 181
  • 1
  • 5