SoFunction
Updated on 2025-03-08

Springboot reads templates file html code instance

SpringBoot does not have the WebContent (WebApp) we used to develop in regular web, it only has the src directory

There are two folders under src/main/resources. static and templates springboot are placed in static by default, while dynamic pages are placed in templates.

Without using third-party jar packages, Springboot cannot directly access the static page under templates, and needs to add other jar package dependencies.

The code is as follows

import org.;
import org.;
import ;
import ;
import ;

import ;
import ;
import ;
import .*;

@Controller
public class PageController {
  private static ClassLoader classLoader = ();
  private static final Logger log = ();

  @RequestMapping("/")
  public void root(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ("/").forward(request, response);
  }

  @RequestMapping("**.html")
  public void page(HttpServletRequest request, HttpServletResponse response) throws IOException {
    InputStreamReader streamReader = null;
    try {
      String servletPath = ();
      (() + " " + servletPath);
      String filePath = "templates" + servletPath;

      InputStream inputStream = (filePath);
      if (inputStream == null) {
        (404);
        return;
      }
      streamReader = new InputStreamReader(inputStream);
      PrintWriter printWriter = ();
      ("text/html");

      int readChar;
      while ((readChar = ()) != -1) {
        (readChar);
      }
      ();
    } finally {
      if (streamReader != null) ();
    }
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.