SpringBoot Integration Series-Integration of SpringMVC

SpringBoot整合Spring MVC

step

The first step: add necessary dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

Step 2: Add the necessary configuration

Step 3: Add the necessary configuration classes

SpringBoot integrates SpringMVC without the necessary configuration classes, only add some configuration classes that implement the WebMvcConfigurer interface when you want to customize it.

@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {
    
        // 添加针对swagger的处理,避免swagger404
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
    }    //...自定义实现WebMvcConfigurer中的若干默认方法}

Step 4: Integrate the template engine

Integrate Freemarker

The first step: add the necessary dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

Step 2: Add the necessary settings (emphasis)

#Freemarker-config# 设置模板前后缀名#spring.freemarker.prefix=spring.freemarker.suffix=.ftl
spring.freemarker.enabled=true# 设置文档类型spring.freemarker.content-type=text/html
spring.freemarker.request-context-attribute=request# 设置ftl文件路径spring.freemarker.template-loader-path=classpath:/templates/# 设置页面编码格式spring.freemarker.charset=UTF-8# 设置页面缓存spring.freemarker.cache=false

Step 3: Add the necessary configuration classes

Step 4: Add controllers and dynamic pages

@Controller@RequestMapping("base")@Log4j2@Api(hidden = true)public class Base {
    
        @RequestMapping("/book")    @ApiOperation(value = "测试",hidden = true)    public String toBookIndexPage(ModelMap model){
    
    
        log.info("进来啦!!!");
        model.put("name","浩哥");        return "/book/index";
    }
}

resources/book/index.ftl

<#assign base = request.contextPath/><!DOCTYPE HTML><HTML><HEAD>
    <TITLE>测试首页</TITLE>
    <base id="base" href="${base}">
    <link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
    <script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
    <script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script></HEAD><BODY>${
    
    name}<a class="getBook" ="dianji()">点击</a><br/><button ="dianji()">点击</button></BODY><SCRIPT>
    function dianji() {
    
    
        $.ajax({
    
                url: "/account/g/1",            type: "GET",            success: function (data) {
    
    
                alert(data);
            }
        })
    }    var base = document.getElementById("base").href;    // 与后台交互
    _send = function(async,url, value, success, error) {
    
    
        $.ajax({
    
                async : async,            url : base + '/' + url,            contentType : "application/x-www-form-urlencoded; charset=utf-8",            data : value,            dataType : 'json',            type : 'post',            success : function(data) {
    
    
                success(data);
            },            error : function(data) {
    
    
                error(data);
            }
        });
    };</SCRIPT></HTML>

Integrate Thymeleaf

The first step: add the necessary jar package

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Step 2: Add the necessary configuration

spring.thymeleaf.cache=falsespring.thymeleaf.encoding=UTF-8spring.thymeleaf.enabled=truespring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.html
spring.thymeleaf.servlet.content-type=text/html

Except for the first one, none of the above configurations can be configured. The above values ​​are also default values. Configure them when you need to modify them.

Step 3: Add the necessary configuration classes

Step 4: Add controllers and dynamic pages

@Controllerpublic class BaseController {
    
        @RequestMapping("index")    public String toIndex(ModelMap model){
    
    
        model.put("name","首页啊");        return "index";
    }
}

resources/index.html

<!Doctype html>
<html>

<head>
    <title>下一页</title>
</head>

<body>
<h1 th:text="${name}">Hello World</h1>
</body>

</html>

Integrate WebJar

The first step: add the necessary jar package

<!--导入bootstrap-->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.3.7-1</version>
</dependency>

<!--导入Jquery-->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.1.1</version>
</dependency>

Step 2: Use WebJar to develop front-end pages

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Dalaoyang</title>
    <link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
    <script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
    <script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>
</head>

<body>
<div class="container"><br/>
    <div class="alert alert-success">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
        Hello, <strong>Dalaoyang!</strong>
    </div>
</div>
</body>

</html>

Conclusion:
This concludes this article! If you have friends who want to learn Java, want to improve their skills, and want to find a job, you can click to enter, the code: cspp , there are a lot of learning materials, tutorials, and real interview questions. Free!
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_45270667/article/details/109289055