现在公司的很多项目都是使用Spring Boot框架,针对web项目该如何搭建呢,我相信大家都能找到很多的博客讲解搭建步骤,我只是按我的思路来把我的代码贴出来,分享给大家。

首先我们看下我的项目结构:

【Spring Boot】 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、然后启动服务进行验证:

【Spring Boot】 Spring Boot 搭建Web项目

【Spring Boot】 Spring Boot 搭建Web项目

 

【Spring Boot】 Spring Boot 搭建Web项目

【Spring Boot】 Spring Boot 搭建Web项目

 

针对静态资源,我们在属性配置中已经设置,在页面中我配置了一个,可以看一下效果

<!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>

【Spring Boot】 Spring Boot 搭建Web项目

 

大致就这些内容,具体的一些问题还需要我们自己根据项目的需求来看。

相关文章:

  • 2019-10-25
  • 2022-03-04
  • 2022-12-23
  • 2021-04-03
  • 2021-11-08
  • 2021-09-16
  • 2021-10-17
  • 2021-10-20
猜你喜欢
  • 2021-07-16
  • 2021-07-11
  • 2021-11-22
  • 2021-11-15
相关资源
相似解决方案