您的位置:首页 > 编程语言 > Java开发

springboot简单搭建Web项目

2018-12-03 09:58 766 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/aa25117/article/details/84747133

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

1.创建springboot项目

 1.1 创建springboot web 步骤

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

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

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

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

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

2.1添加WEB静态资源

[code]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主页映射

[code]​
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项目运行成功! 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: