第一种方式:直接将页面流输出到页面(/**意思所有index后面匹配的路径都走index)
@RequestMapping(value = "/index/**")
public void index(HttpServletRequest request, HttpServletResponse response) {
try {
String url = request.getSession().getServletContext().getRealPath("/") + "/WEB-INF/jsp/index.jsp";
File file = ResourceUtils.getFile(url);
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.getOutputStream().write(file);
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
}
}
第二种方式:如果页面是jsp页面,采用ModelAndView方式渲染到页面
配置页面前缀和后缀,如果是thymleaf可以采用springboot默认的方式
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
控制层直接返回到页面:
@RequestMapping(value = "/index/**")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView("index");
return modelAndView;
}