springboot简单搭建Web项目

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

1.创建springboot项目

 1.1 创建springboot web 步骤

  此处选择Devtools热启动,方便改动文件不用每次都重启。

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

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

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

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等静态资源是否能用

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

至此WEB项目运行成功! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值