原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9984607.html

SpringBoot整合Spring MVC

步骤

第一步:添加必要依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

第二步:添加必要的配置

第三步:添加必要的配置类

SpringBoot整合SpringMVC没有必需的配置类,只有在想要自定义的时候添加一些实现了WebMvcConfigurer接口的配置类

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

第四步:整合模板引擎

整合Freemarker

第一步:添加必要的依赖
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-freemarker</artifactId>
  4. </dependency>
第二步:添加必要的设置(重点)
  1. #Freemarker-config
  2. # 设置模板前后缀名
  3. #spring.freemarker.prefix=
  4. spring.freemarker.suffix=.ftl
  5. spring.freemarker.enabled=true
  6. # 设置文档类型
  7. spring.freemarker.content-type=text/html
  8. spring.freemarker.request-context-attribute=request
  9. # 设置ftl文件路径
  10. spring.freemarker.template-loader-path=classpath:/templates/
  11. # 设置页面编码格式
  12. spring.freemarker.charset=UTF-8
  13. # 设置页面缓存
  14. spring.freemarker.cache=false
第三步:添加必要的配置类

第四步:添加控制器和动态页面
  1. @Controller
  2. @RequestMapping("base")
  3. @Log4j2
  4. @Api(hidden = true)
  5. public class Base {
  6. @RequestMapping("/book")
  7. @ApiOperation(value = "测试",hidden = true)
  8. public String toBookIndexPage(ModelMap model){
  9. log.info("进来啦!!!");
  10. model.put("name","浩哥");
  11. return "/book/index";
  12. }
  13. }

resources/book/index.ftl

  1. <#assign base = request.contextPath/>
  2. <!DOCTYPE HTML>
  3. <HTML>
  4. <HEAD>
  5. <TITLE>测试首页</TITLE>
  6. <base id="base" href="${base}">
  7. <link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
  8. <script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
  9. <script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>
  10. </HEAD>
  11. <BODY>
  12. ${name}
  13. <a class="getBook" onclick="dianji()">点击</a><br/>
  14. <button onclick="dianji()">点击</button>
  15. </BODY>
  16. <SCRIPT>
  17. function dianji() {
  18. $.ajax({
  19. url: "/account/g/1",
  20. type: "GET",
  21. success: function (data) {
  22. alert(data);
  23. }
  24. })
  25. }
  26. var base = document.getElementById("base").href;
  27. // 与后台交互
  28. _send = function(async,url, value, success, error) {
  29. $.ajax({
  30. async : async,
  31. url : base + '/' + url,
  32. contentType : "application/x-www-form-urlencoded; charset=utf-8",
  33. data : value,
  34. dataType : 'json',
  35. type : 'post',
  36. success : function(data) {
  37. success(data);
  38. },
  39. error : function(data) {
  40. error(data);
  41. }
  42. });
  43. };
  44. </SCRIPT>
  45. </HTML>

整合Thymeleaf

第一步:添加必要的jar包
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>
第二步:添加必要的配置
  1. spring.thymeleaf.cache=false
  2. spring.thymeleaf.encoding=UTF-8
  3. spring.thymeleaf.enabled=true
  4. spring.thymeleaf.mode=HTML
  5. spring.thymeleaf.prefix=classpath:/templates/
  6. spring.thymeleaf.suffix=.html
  7. spring.thymeleaf.servlet.content-type=text/html

以上配置中除了第一个之外,其余皆可不配置,上面的值也是默认值,需要修改的时候再进行配置

第三步:添加必要配置类

第四步:添加控制器和动态页面
  1. @Controller
  2. public class BaseController {
  3. @RequestMapping("index")
  4. public String toIndex(ModelMap model){
  5. model.put("name","首页啊");
  6. return "index";
  7. }
  8. }

resources/index.html

  1. <!Doctype html>
  2. <html>
  3. <head>
  4. <title>下一页</title>
  5. </head>
  6. <body>
  7. <h1 th:text="${name}">Hello World</h1>
  8. </body>
  9. </html>

整合WebJar

第一步:添加必要的jar包
  1. <!--导入bootstrap-->
  2. <dependency>
  3. <groupId>org.webjars</groupId>
  4. <artifactId>bootstrap</artifactId>
  5. <version>3.3.7-1</version>
  6. </dependency>
  7. <!--导入Jquery-->
  8. <dependency>
  9. <groupId>org.webjars</groupId>
  10. <artifactId>jquery</artifactId>
  11. <version>3.1.1</version>
  12. </dependency>
第二步:使用WebJar开发前端页面
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Dalaoyang</title>
  6. <link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
  7. <script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
  8. <script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>
  9. </head>
  10. <body>
  11. <div class="container"><br/>
  12. <div class="alert alert-success">
  13. <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
  14. Hello, <strong>Dalaoyang!</strong>
  15. </div>
  16. </div>
  17. </body>
  18. </html>

SpringBoot整合系列-整合SpringMVC的更多相关文章

  1. SpringBoot整合系列--整合MyBatis-plus

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/10125279.html SpringBoot整合MyBatis-plus 步骤 第一步: ...

  2. SpringBoot整合系列-整合MyBatis

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9971036.html SpringBoot整合Mybatis 步骤 第一步:添加必要的j ...

  3. SpringBoot整合系列-整合JPA

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9959865.html SpringBoot整合JPA进行数据库开发 步骤 第一步:添加必 ...

  4. SpringBoot整合系列-整合H2

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9959855.html SpringBoot整合H2内存数据库 一般我们在测试的时候习惯于 ...

  5. SpringBoot整合系列-整合Swagger2

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9959844.html SpringBoot整合Swagger2 步骤 第一步:添加必要的 ...

  6. SpringBoot整合系列-PageHelper分页插件

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9971043.html SpringBoot整合MyBatis分页插件PageHelper ...

  7. SpringBoot系列-整合Mybatis(注解方式)

    目录 一.常用注解说明 二.实战 三.测试 四.注意事项 上一篇文章<SpringBoot系列-整合Mybatis(XML配置方式)>介绍了XML配置方式整合的过程,本文介绍下Spring ...

  8. 零基础学习java------40---------Maven(maven的概念,安装,maven在eclipse中使用),springboot(spring整合springmvc(注解),spring整合mybatis(常见的配置文件)),前端页面(bootstrap软件)

    一 maven 1. Maven的相关概念 1.1 项目开发中遇到的问题 (1)都是同样的代码,为什么在我的机器上可以编译执行,而在他的机器上就不行? (2)为什么在我的机器上可以正常打包,而配置管理 ...

  9. springboot + mybatis + mycat整合

    1.mycat服务 搭建mycat服务并启动,windows安装参照. 系列文章: [Mycat 简介] [Mycat 配置文件server.xml] [Mycat 配置文件schema.xml] [ ...

随机推荐

  1. oracle 报错无法从套接字获取更多数据

    报错信息如下: ---查看_optimizer_join_elimination_enabled参数值 切换sys用户 select a.ksppinm name, b.ksppstvl value, ...

  2. Vue(二十九)页面加载过慢问题

    1.使用按需加载 2.路由懒加载

  3. 3.1circle

    就是括号匹配的题目,如果有交集就是NO #include<iostream> #include<cstring> #include<stdio.h> #includ ...

  4. Katalon Studio之请求响应中文乱码解决方法

    最近在用Katalon做接口测试过程中发现请求响应消息中返回的中文均为乱码,这是因为我们使用的系统环境在初始安装时选择的中文简体,导致windows系统默认编码格式为GBK,但是KS的编码格式是UTF ...

  5. angular学习笔记(三)

    1.安装npm install --save @angular/material@2.0.0-beta.72.安装http://chrome-extension-downloader.com安装aug ...

  6. python—迭代器、生成器

    1.迭代器(iteration) 迭代器协议:提供next()方法,该方法要么返回迭代的下一项,要么异常.(只能往后走) 可迭代对象:实现迭代器协议的对象. **字符串.列表.元祖.字典.集合.文件都 ...

  7. External Snapshot management

    External Snapshot management Symptom As of at least libvirt 1.1.1, external snapshot support is inco ...

  8. 解决 spring-cloud-starter-zipkin 启动错误

    应用场景:Spring Boot 服务添加 Zipkin 依赖,进行服务调用的数据采集,然后进行 Zipkin-Server 服务调用追踪显示. 示例pom.xml配置: <parent> ...

  9. Redis Cluster(集群)

    一.概述 在前面的文章中介绍过了redis的主从和哨兵两种集群方案,redis从3.0版本开始引入了redis-cluster(集群).从主从-哨兵-集群可以看到redis的不断完善:主从复制是最简单 ...

  10. [Java]LeetCode278. 第一个错误的版本 | First Bad Version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...