springboot(x)——小技能合集

版权声明:本文出自mqy1023的博客,转载必须注明出处。 https://blog.csdn.net/mqy1023/article/details/79888554
1、不用创建Controller控制类,直接根据配置跳转到页面
package com.xxx.yyy.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
    /**
     * 以前要访问一个页面需要先创建个Controller控制类,在写方法跳转到页面
     * 在这里配置后就不需要那么麻烦了,直接访问http://localhost:8080/totest就跳转到test.html(test.ftl或其他模板页面了
     *
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("welcome");
        registry.addViewController("/totest").setViewName("test");
        registry.addViewController("/error/errordeal").setViewName("error/errordealogin");

        super.addViewControllers(registry);
    }
}
2、lombok的使用

在pom.xml中加入lombok库;在bean类头部添加如下注释的效果:
* 1、@Accessors(chain = true)
bean可以链式setXxx().setYyy()...来连续赋值
* 2、@Data (相当于@Getter@setter) ,用于省略掉javabean写一堆·getXxxsetXxx方法

3、静态资源文件的访问

springboot默认不要写static路径就能直接访问到static目录下的静态文件
比如http://localhost:8899/xxProject/css/xxx.css 能直接访问到项目资源目录下static/css/xxx.css的文件

表示访问该路径时代表请求静态资源,用户可以直接访问该请求路径中的静态资源

application.yml 配置中设置static-path-pattern 静态路径模式如下,

spring:
  mvc:
    static-path-pattern: /static/**

http://localhost:8899/xxProject/static/css/xxx.css 才能访问

4、FreeMarker中所有页面自动导入公共模板、使用宏macro

1、templates\common\common.ftl 公共模板并存放公共的宏

<#macro head  title="">
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/static/logo.png">

    <title>${title}</title>
    <#nested />
</head>
</#macro>

<#macro body>
<body>

<#nested />
</body>
</html>
</#macro>

2、application.yml设置common.ftl 在所有页面都自动导入, 并设置了缩写

spring
  freemarker:
    settings:
      auto_import: common/common.ftl as com

3、xyz.ftl中使用

<@com.head title="xyz">

</@com.head>
<@com.body>

</@com.body>
5、整合shiro

springboot学习笔记-5 springboot整合shiro

springboot学习笔记:11.springboot+shiro+mysql+mybatis(通用mapper)+freemarker+ztree+layui实现通用的java后台管理系统(权限管理+用户管理+菜单管理)

6、Entity实体中中不映射成数据库中列的字段, 需加@Transient 注解
7、Entity实体中中某个字段不需要返回到前端, 需要加@JsonIgnore注解
8、请求返回数据中的空值的字段不显示出来
spring:
    jackson:
        default-property-inclusion: non_null

猜你喜欢

转载自blog.csdn.net/mqy1023/article/details/79888554