SpringBoot项目html加载时间超长,影响使用感受
场景描述: 1.项目在本地测没问题,但是部署到服务器,发现跳页的时候,要等很长的时间,有时甚至有一分钟 2.无论是thymeleaf,还是静态html,都是慢
最后就是使用@RestController,将整个页面返回 将你需要的值,直接拼接到lable的value中
@RestController
@RequestMapping(value = "/data-pipeline")
public class HtmlController {
@ModelAttribute
public void setResponseHeader(HttpServletResponse response) {
response.setHeader("Access-Control-Allow-Origin", "*");
}
@RequestMapping(value = "reg")
public String getRegHtml(String termId){
return "<html xmlns:th=\"http://www.thymeleaf.org\"\n" +
" xmlns=\"http://www.w3.org/1999/html\">\n" +
"\n" +
"<html>\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Data Pipeline</title>\n" +
"\n" +
" <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->\n" +
" <script src=\"../static/js/bootstrap.min.js\"></script>\n" +
"</head>\n" +
"\n" +
"<style type=\"text/css\">\n" +
"\n" +
"</style>\n" +
"<script type=\"text/javascript\">\n" +
"\n" +
" window.onload = function () {\n" +
" console.log('get windows username1')\n" +
"\n" +
" fetch('https://xxxx/auth', {\n" +
" method: 'GET',\n" +
" credentials: 'include',\n" +
" headers: {\n" +
" 'Access-Control-Allow-Credentials': 'true',\n" +
" 'content-type': 'application/json'\n" +
" }\n" +
" })\n" +
" .then(response => {\n" +
" return response.json()\n" +
" })\n" +
" .then(json => {\n" +
" var token = json.token;\n" +
" var content = parseJwt(token);\n" +
" var name = content.username;\n" +
"\n" +
" displayContent(name);\n" +
" setTimeout(formCommit(), 10000);\n" +
" })\n" +
" .catch((e) => {\n" +
" //console.log(e)\n" +
" alert(\"Failed to obtain user name. Do not use traceless browser!\")\n" +
" });\n" +
"\n" +
" }\n" +
"\n" +
" function displayContent(content) {\n" +
" userForm.username.value = content\n" +
" }\n" +
"\n" +
" function formCommit() {\n" +
" var form = document.getElementById('userForm');\n" +
" form.submit();\n" +
" }\n" +
"</script>\n" +
"<body>\n" +
"<div>\n" +
" <img src=\"../static/img/logo.svg\" style=\"height: 65px;width: 170px\">\n" +
"</div>\n" +
"<div style=\"height: 35%\">\n" +
"\n" +
"</div>\n" +
"<div align=\"center\">\n" +
" <img src=\"../static/img/load.gif\" style=\"height: 55px;width: 110px\">\n" +
"</div>\n" +
"<div hidden>\n" +
" <form method=\"post\" action=\"/data-pipeline/createRegIssueByForm\" id=\"userForm\" name=\"userForm\">\n" +
" <b>Active User: </b><input type=\"text\" name=\"username\" id=\"username\"><br>\n" +
" <b>TermId: </b><input type=\"text\" name=\"termid\" id=\"termid\" value=\""+termId+"\"><br>\n" +
" </form>\n" +
"</div>\n" +
"\n" +
"</body>\n" +
"</html>\n"
;
}
|