springboot整合freemarker模板


Freemarker作用於表現層,能更好的實現mvc分離。

 

1、首先,在pom文件里加入freemarker依賴

<!-- 整合freemarker -->

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-freemarker</artifactId>

</dependency>

 

2、在配置文件application.properties中加入freemarker配置

# freemarker靜態資源配置

# 設定ftl文件路徑

spring.freemarker.tempalte-loader-path=classpath:/templates

# 關閉緩存,及時刷新,上線生產環境需要修改為true

spring.freemarker.cache=false

spring.freemarker.charset=UTF-8

spring.freemarker.check-template-location=true

spring.freemarker.content-type=text/html

spring.freemarker.expose-request-attributes=true

spring.freemarker.expose-session-attributes=true

spring.freemarker.request-context-attribute=request

spring.freemarker.suffix=.ftl

 

3、在public目錄下建立freemarker文件夾
4、建立login.ftl文件
[#ftl]
<html>
<head>
<meta charset="GBK">
<title>杭州天縱智能管理平台</title>
<link rel="stylesheet" href="../css/layui.css" rel="stylesheet">
<link rel="stylesheet" href="../css/modules/layer/default/layer.css?v=3.1.1" rel="stylesheet">
<style>
html,body{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
align-items: center; /*定義body的元素垂直居中*/
justify-content: center; /*定義body的里的元素水平居中*/
}
</style>
</head>
<body>
<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">用戶名</label>
<div class="layui-input-inline">
<input type="text" name="userName" lay-verify="required" autocomplete="off" placeholder="請輸入用戶名稱" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密碼</label>
<div class="layui-input-inline">
<input type="password" name="passWord" lay-verify="required" autocomplete="off" placeholder="請輸入密碼" class="layui-input"/>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"></label>
<div class="layui-input-inline">
<button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">登錄</button>
</div>
</div>
</form>
<script src="../js/layui.js"></script>
<script src="../js/jquery-1.12.4.js"></script>
<script>
layui.use(['form'], function(){
var form = layui.form
,layer = layui.layer
form.on('submit(demo1)', function(data){
$.ajax({
url: "/manage/login",
type : "get",
data: data.field,
async: false,
success: function(data){
if(data>0){
alert("登錄成功");
window.location.href="/index.html";
event.preventDefault();
}else{
alert("登錄失敗");
}
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert(XMLHttpRequest.status);//狀態碼
alert(XMLHttpRequest.readyState);//狀態
alert(textStatus);//錯誤信息
}
})
});
});
</script>
</body>
</html>
5、寫controller方法

@RequestMapping(value = "/manage")

@Controller

public class login(){

  @RequestMapping(value="login")

  public String loginFtl(HttpServletRequest request, HttpServletResponse response){

    return "/freemarker/login";

  }

}

6、訪問localhost:8080/manage/login


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM