您当前的位置: 首页 >  spring

程序员一灯

暂无认证

  • 3浏览

    0关注

    152博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

通过maven项目来搭建SpringBoot

程序员一灯 发布时间:2021-12-02 23:04:46 ,浏览量:3

一、选中maven,创建项目

二、添加POM文件父依赖
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.1
    
 三、支持SpringMVC
    
        org.springframework.boot
        spring-boot-starter-web
    
四、创建启动类、rest接口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class MyApplication {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}
五、可执行jar包配置

    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    
六、添加thymeleaf模板
    
      org.springframework.boot
      spring-boot-starter-thymeleaf
    
七、添加配置

创建templates文件夹

# 应用名称
spring.application.name=thymeleaf
# 应用服务 WEB 访问端口
server.port=8080
# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=false
# 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template=true
# 检查模板位置是否正确(默认值 :true )
spring.thymeleaf.check-template-location=true
#Content-Type 的值(默认值: text/html )
spring.thymeleaf.content-type=text/html
# 开启 MVC Thymeleaf 视图解析(默认值: true )
spring.thymeleaf.enabled=true
# 模板编码
spring.thymeleaf.encoding=UTF-8
# 要被排除在解析之外的视图名称列表,⽤逗号分隔
spring.thymeleaf.excluded-view-names=
# 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
spring.thymeleaf.mode=HTML5
# 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
spring.thymeleaf.prefix=classpath:/templates/
# 在构建 URL 时添加到视图名称后的后缀(默认值: .html )
spring.thymeleaf.suffix=.html
八、添加controller
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {

    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();

        mv.setViewName("index");

        return mv;
    }

}
九、添加html



    
    index


index

十、访问

http://localhost:8080/index

关注
打赏
1645367472
查看更多评论
立即登录/注册

微信扫码登录

0.0382s