1.依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
2.配置
注意:不再使用web
application.properties
Notice:目前所有页面和静态资源都在Resources下面
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.mvc.static-path-pattern=/static
3.Controller
3.1使用model
@RequestMapping(value = "req")
public String redirectHtmlReq(Model model,String termId) {
model.addAttribute("termId", termId);
return "request";
}
@RequestMapping(value = "/createRegIssueByForm", method = RequestMethod.POST)
public String createRegIssueByForm(HttpServletRequest request,Model model) {
String name = request.getParameter("username");
String termId = request.getParameter("termid");
String url = null;
try {
url = dataPipelineController.xxxxx(termId, name);
} catch (Exception e) {
url = ExceptionUtils.getStackTrace(e);
}
model.addAttribute("url", url);
return "buffer";
}
3.2使用ModelAndView
@RequestMapping(value = "/user")
public ModelAndView test(HttpServletRequest req) {
UserEntity user = new UserEntity();
user.setLoginName(request.getParameter("username"));
user.setId(request.getParameter("id"));
ModelAndView mv = new ModelAndView();
mv.addObject("user", user);
mv.setViewName("/user/show.html");
return mv;
}
4.页面接受数据
4.1 用js接收 var results = [[${url}]];
<script type="text/javascript" th:inline="javascript">
var results = [[${url}]];
let reg=RegExp('^http.*')
if (results.match(reg)) {
window.location.href = results;
}else {
alert(results)
}
</script>
4.2 页面接受 th:value="${termId}"
<body>
<div>
<form method="post" action="/xxxxx" id="userForm" name="userForm">
<b>Active User: </b><input type="text" name="username" id="username"><br>
<b>TermId: </b><input type="text" name="termid" id="termid" th:value="${termId}"><br>
</form>
</div>
</body>
|