栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot整合系列-整合SpringMVC

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合系列-整合SpringMVC

出处地址:https://www.cnblogs.com/V1haoge/p/9984607.html

SpringBoot整合Spring MVC步骤第一步:添加必要依赖

    org.springframework.boot
    spring-boot-starter-web
第二步:添加必要的配置

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

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

@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {    // 添加针对swagger的处理,避免swagger404
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/meta-INF/resources/");
    }    //...自定义实现WebMvcConfigurer中的若干默认方法}
第四步:整合模板引擎整合Freemarker第一步:添加必要的依赖

    org.springframework.boot
    spring-boot-starter-freemarker
第二步:添加必要的设置(重点)
#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
第三步:添加必要的配置类

第四步:添加控制器和动态页面
@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/>
    测试首页
    
    
    
    ${name}点击
点击
整合Thymeleaf第一步:添加必要的jar包

    org.springframework.boot
    spring-boot-starter-thymeleaf
第二步:添加必要的配置
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

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

第三步:添加必要配置类

第四步:添加控制器和动态页面
@Controllerpublic class baseController {    @RequestMapping("index")    public String toIndex(ModelMap model){
        model.put("name","首页啊");        return "index";
    }
}

resources/index.html


    下一页Hello World
整合WebJar第一步:添加必要的jar包

    org.webjars
    bootstrap
    3.3.7-1
    org.webjars
    jquery
    3.1.1
第二步:使用WebJar开发前端页面

    
    Dalaoyang
    
    
    
             ×         Hello, Dalaoyang!     


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/234696.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号