springboot 项目的创建配置静态资源

springboot简介:

Spring Boot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目。它使用习惯优于配置的理念让你的项目快速运行起来,使用Spring Boot很容易创建一个独立运行(运行jar,内置Servlet容器,Tomcat、jetty)、准生产级别的基于Spring框架的项目,使用SpringBoot你可以不用或者只需要很少的配置文件。

我这里用的是idea创建的如图:

 

这里直接选择spring initializr 其它默认就好,一直点击下一步就好,这里挺简单的

如下图继续点击下一步,包名jdk这些自己随意

 

 

 如下图,这里需要选择依赖,我们就以springweb为例,平常我们也用这个最多

 

 继续下一步,选择项目名称,和项目位置,点击finish 即可,创建好后,如下图所示

 

 这里备注一下,各个文件的含义:

java 文件目录 是java开发的代码存放目录, 注意的是 程序的 main 方法的执行类,需要放到其他的被spring扫码的类的上一级目录,如果是同级或者更低级目录,会导致其它类不能被扫描到

resources 项目静态资源的目录,springboot 配置静态资源路径(默认是/static 、/public 、 /resources 和/META-INF/resources) 开启自定义配置后,默认不生效

application.yml  配置文件,spring创建好的项目默认是application.properties 文件,我这里是改了后缀用的 yml 文件

写个controller 执行个hello world 吧!

package com.example.controller;

import com.example.exception.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RequestMapping("/home")
public class HomeController {


    @GetMapping("/getHome")
    private String getHome () {
        System.out.println("进入getHome");

        return "/home";
    }


}

这里请求转发到页面 home ,默认是templates 作为根目录,当然这里需要配置一下

server:
  port: 8080
spring:
  resources:
#    配置静态资源路径(默认是/static 、/public 、 /resources 和/META-INF/resources) 开启自定义配置后,默认不生效
    static-locations: classpath:templates/
  mvc:
#    mvc 返回页面路径时,加上.html (返回页面路径就不用加 .html)
    view:
      prefix: /
      suffix: .html

一个helloworld就好了

 

 

 



 
posted @ 2020-04-14 10:25  朝阳花  阅读(1648)  评论(0编辑  收藏  举报