springboot简单搭建Web项目

springboot介绍就不用多说了,程序员就喜欢直接进入正题

1.创建springboot项目

 1.1 创建springboot web 步骤springboot简单搭建Web项目

springboot简单搭建Web项目 springboot简单搭建Web项目 此处选择Devtools热启动,方便改动文件不用每次都重启。

Web不用多说了,WEB项目必用。。。

 这里选thymeleaf 是因为用的是HTML5的静态页面,需要用Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

1.2 项目建好如下,还添加了几个后面要用的package

springboot简单搭建Web项目

2.添加springboot静态资源的路径

2.1添加WEB静态资源

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/components/**").addResourceLocations("classpath:/components/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

此处代码的components是用来放外部插件比如Vue.js。

2.2添加controller主页映射

​
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * User: Young
 * Date: 2018/11/29
 * Time: 15:12
 */
@Controller
public class HomeController {


    @GetMapping("/index")
    public String index(){
        return "example/index";
    }
}

​

2.3 在index里面写上能触发的事件,测试js等静态资源是否能用

springboot简单搭建Web项目

springboot简单搭建Web项目

最后一步启动项目运行..

springboot简单搭建Web项目

springboot简单搭建Web项目

springboot简单搭建Web项目

至此WEB项目运行成功!