springboot(x)——小技能合集
阿新 • • 發佈:2018-11-01
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寫一堆·getXxx
和setXxx
方法
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
6、Entity實體中中不對映成資料庫中列的欄位, 需加@Transient 註解
7、Entity實體中中某個欄位不需要返回到前端, 需要加@JsonIgnore註解
8、請求返回資料中的空值的欄位不顯示出來
spring:
jackson:
default-property-inclusion: non_null