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

springboot

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

springboot

spring boot

简化spring应用的处死搭建以及开发过程

创建springboot

如果卡住了可以按照红色的改

勾三个就可以了

接口返回Json

1.先创建,一个user含有name,age‘属性的get(),set()方法。
2.创建一个class文件TestController,代码如下

@Controller
public class TestController {
    //简化使用spring springMVC
    //包括响应头 和body
    // 接口请求返回数据类型
    //1.html     2.String-》json
    @RequestMapping("test")
    @ResponseBody
    public   User getString(){
        //对象转化为String(json)
        //集成了json依赖包 自动转换
        User user =new User();
        user.setAge(20);
        user.setName("zhangsan");
        return user;
    }

3.运行之后,浏览器的目录名为http://localhost:8080/test就可以出现Json接口。

热更新

所谓热更新就是不需要重新启动服务,只需要更新代码就能显示出来。
1、首先进行配置,点击setting,按如图勾选。

2、再将资源实时更新,否则就无用了

3、再将项目重启。

修改项目和端口号

在application.properties文件里进行编写代码,注意不能改文件名,否则就找不到路径。

server.port=8088
server.servlet.context-path=/springboot

在浏览器中localhost:8088/springboot/test就完成了。
最后显示

静态资源文件

1.在application.properties文件里进行编写代码

server.port=8088
server.servlet.context-path=/springboot
spring.web.resources.static-locations=classpath:/templates

2.再将图片复制到templates目录里。
3.在浏览器中输入http://localhost:8088/springboot/img.png,img.png是照片名。

server.port=8088
server.servlet.context-path=/springboot
spring.web.resources.static-locations=classpath:/templates,file:d:/imgs/1.png
模板Thymeleaf

先加入依赖包

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
简单参数

1.在TestController文件里编写以下代码

    @RequestMapping("gethtml")
    public String gethtml(Model model){
        model.addAttribute("name","test1234");
        return "test.html";

    }

2.在test.html里编写代码,注意name对应TestController里的addAttribute第一个参数

在浏览器http://localhost:8088/springboot/gethtml展示出来的如图

遍历循环

1.在TestController文件里编写以下代码,注意*model.addAttribute(“userlist”,userList)*这个和下面的test.html中的参数对应

@RequestMapping("gethtml")
    public String gethtml(Model model){
        List userList=new ArrayList<>();
        for(int i=0;i<10;i++){
            User user =new User();
            user.setAge(20);
            user.setName("zhangsan:"+i);
            userList.add(user);

        }
        model.addAttribute("userlist",userList);
        return "test.html";

2.在test.html里编写代码,



http://localhost:8088/springboot/gethtml中显示

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322849.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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