栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot热部署、模板引擎

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot热部署、模板引擎

SpringBoot热部署、模板引擎 热部署 devtools
好处:修改代码后,无需重启服务器

全局属性文件:application.properties

#热部署    devtools
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java

idea更改工具配置
第一步File > Settings > Compiler > Build Project automatically

第二步:ctrl + shift + alt + / , 选择Registry , 勾上Compiler autoMake allow when app running

最后添加依赖pom.xml

		
			org.springframework.boot
			spring-boot-devtools
			runtime	
			true
		
模板引擎 Thymeleaf

全局属性文件application.properties

#模板引擎
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=utf-8
    #前缀
spring.thymeleaf.prefix=classpath:/templates/
    #后缀
spring.thymeleaf.suffix=.html
    #严格的语法检查
spring.thymeleaf.mode=HTML
    #缓存(建议:开发期为false,项目发布时true)
spring.thymeleaf.cache=false

注:static文件夹下为静态资源,可直接访问,除了文件夹名为img,springboot会默认拦截

测试:
hello.js

alert("hello.js")

style.css

body{
    background-color: green;
}

main.html




    
    首页
    
    


        静态网页
        


运行后:


------------------------------------------------------------------------------------------>>>成功!!!
动态网页:

创建一个controller测试传值

@Controller
public class HelloController {
    @GetMapping("/user")  //只支持get请求
    public String hello1(){
        //进前台页
        return "users/index";
    }
    @GetMapping("/admin")  //只支持get请求
    public String hello2(ModelMap map){
        //进后台页
        map.put("info","我传过来的数据");
        Users user1=new Users(1,"碰磕","666",new Date());
        map.put("users",user1);
        map.put("role","admin");
        //集合
        Users user2=new Users(2,"碰磕2","4444",new Date());
        Users user3=new Users(3,"Mr_xiao","444",new Date());
        List list=new ArrayList();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        map.put("userlist",list);
        return "admin/index";
    }
}

实体类users

public class Users {
    int uid;
    String uname;
    String upass;
    Date birthday;

    public Users(int uid, String uname, String upass, Date birthday) {
        this.uid = uid;
        this.uname = uname;
        this.upass = upass;
        this.birthday = birthday;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpass() {
        return upass;
    }

    public void setUpass(String upass) {
        this.upass = upass;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "nUsers{" +
                "uid=" + uid +
                ", uname='" + uname + ''' +
                ", upass='" + upass + ''' +
                ", birthday=" + birthday +
                '}';
    }
}

adminindex.html获取数据





    
    Title


    后台动态网页
    

基本数据

基本数据

识别标签 传对象:姓名:姓名密码:密码 生日:生日 生日: 去掉对象名: IF 标签+src标签 switch标签 管理员 老师 学生 循环标签
编号 姓名 密码 生日
ID NAME PASS BIRTHDAY

注:一定要记得引入命名空间

练习

作业:循环显示页码

PageInfo{

int pages=20; //总页
int cur=3; //当前页

int sz[]={1,2,3,4,5}; // 页码数组

}
首页 1 2 【3】 4 5 尾页
最终效果图:

创建实体类pageinfo

public class PageInfo {
    int pages=20;  //总页
    int cur=3;     //当前页
    int sz[]={1,2,3,4,5};  // 页码数组

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getCur() {
        return cur;
    }

    public void setCur(int cur) {
        this.cur = cur;
    }

    public int[] getSz() {
        return sz;
    }

    public void setSz(int[] sz) {
        this.sz = sz;
    }

    @Override
    public String toString() {
        return "nPageInfo{" +
                "pages=" + pages +
                ", cur=" + cur +
                ", sz=" + Arrays.toString(sz) +
                '}';
    }
}

controller

 @GetMapping("/user")  //只支持get请求
    public String hello1(ModelMap map){
        PageInfo page=new PageInfo();
        map.put("page",page);
        //进前台页
        return "users/index";
    }

前台页显示数据





    
    Title
    
        .btn{
            background-color: red;
        }
        .boder{
            float: left;
            border: 1px solid blue;
            margin: 5px;
            cursor: pointer;
        }
    


    前台动态网页
    分页
    
    
        
    
    


--------------------------------------------------->完成!!!
作业完成图:

总结

热部署、模板引擎参照上分进行配置即可

  • 模板引擎部分标签:
  • 文本标签:th:text 识别标签的文本标签:th:utext
  • 日期时间转换:th:text="${#dates.format(日期类型属性,‘yyyy-MM-dd’)}"
  • IF标签、src标签:th:if="${users!=null}"th:src="@{/image/chunyu01.png}"@表示绝对路径
  • switch标签:th:switch="${参数}"th:case="值"如果参数值与case值相等则会显示该标签的内容
  • 循环标签:{th:each="u:${参数}"th:text=${u.属性}}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459147.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号