現在公司的很多項目都是使用Spring Boot框架,針對web項目該如何搭建呢,我相信大家都能找到很多的博客講解搭建步驟,我只是按我的思路來把我的代碼貼出來,分享給大家。
首先我們看下我的項目結構:
現在我來說下我的搭建步驟:
1、pom文件添加依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、application.properties文件添加配置:
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
spring.mvc.static-path-pattern =/**
3、在src/main/resources目錄下面創建templates和static目錄,然後按我上面說的創建頁面目錄及html文件
4、然後編寫controller類,直接貼代碼
package com.wzz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author wangzz
* @Date 2019/2/22 17:20
* @Description
*/
@Controller
@RequestMapping("/home")
public class HomeController {
@RequestMapping("/index")
public String index(){
return "index";
}
@RequestMapping("/login")
public String login(){
return "/login";
}
}
package com.wzz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author wangzz
* @Date 2019/2/22 17:05
* @Description
*/
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/helloUser")
public String helloUser(){
return "user1/helloUser";
}
}
package com.wzz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author wangzz
* @Date 2019/2/22 18:01
* @Description
*/
@Controller
@RequestMapping("/customer")
public class CustomerController {
@RequestMapping("/customerHello")
public String customerHello(){
return "customer/customerHello";
}
}
5、然後啓動服務進行驗證:
針對靜態資源,我們在屬性配置中已經設置,在頁面中我配置了一個,可以看一下效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/js/jquery.min.js"></script>
</head>
<body>
<h1>歡迎訪問本系統</h1>
</body>
</html>
大致就這些內容,具體的一些問題還需要我們自己根據項目的需求來看。