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
上一篇: C# vb .NET生成QR二维码
下一篇: TCP方式的传输
推荐阅读
-
springboot用thymeleaf模板的paginate分页完整代码
-
springboot整合kaptcha验证码的示例代码
-
SpringBoot 2.x 整合Lombok的方法示例
-
activemq整合springboot使用方法(个人微信小程序用)
-
Springboot 整合 Dubbo/ZooKeeper 实现 SOA 案例解析
-
Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数示例代码
-
springboot如何使用thymeleaf模板访问html页面
-
springboot整合H2内存数据库实现单元测试与数据库无关性
-
springboot整合EHCache的实践方案
-
spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)