springboot中如果想訪問html頁面,不每訪問一個頁面就寫一個Controller,可以統一寫一個公共的controller方法
代碼:
(1)引入hutool工具依賴
<!-- hutool工具包-->
<dependency>
<groupId>com.xiaoleilu</groupId>
<artifactId>hutool-all</artifactId>
<version>3.3.0</version>
</dependency>
(2)公共Controller
import com.xiaoleilu.hutool.util.StrUtil;
//html訪問控制器
/**
* @return
*/
@RequestMapping(value = "/view/**", method = RequestMethod.GET)
public String html(HttpServletRequest request) {
return StrUtil.removePrefix(request.getRequestURI(), request.getContextPath() + "/view");
}
(3)訪問頁面路徑
如頁面路徑在demo下,配置文件中servlet.context-path: /test
則lhttp://localhost:8081/test/view/demo/...../XXX.html