SpringBoot練習

1.起勢

1.簡單的包結構

pojo-dao-service-controller

i18n-resources-static templates

2.靜態資源映射規則

1.webjars

以jar包方式引入資源

2.靜態資源文件夾
3.首頁

靜態資源文件夾下的index.html

4.配置網頁圖標

所有的favicon.ico都是在靜態資源文件夾下尋找

自定義靜態資源文件夾

spring.resources.static-locations=classpath:/test1/,classpath:/test2/

更改項目訪問名

server.context-path=/myself

3.設置模板引擎

SpringBoot默認使用嵌入式Tomcat,不支持JSP

推薦使用Thymeleaf(語法更簡單,功能更強大 )

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

修改版本號

<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>
    2.2.2
</thymeleaf-layout-dialect.version>

Thymeleaf語法規則規則

2.擴展springmvc的功能

@Configuration
extends WebMvcConfigurerAdapter 
1.設置首頁

添加視圖映射,直接把指定頁面設置爲首頁

@Bean
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
            }
        };
        return adapter;
    }

登錄錯誤提示:

返回報錯信息,頁面接收並展示

<p style="color: red;" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
2.設置攔截器

避免用戶繞過登錄界面直接跳入後臺

1)編寫攔截器(方法之前)

implements HandlerInterceptor {

Object user = request.getSession().getAttribute("username");
if (user == null) {
//未登錄
request.setAttribute("msg","沒有權限,請先登錄");
request.getRequestDispatcher("/").forward(request,response);
return false;
}
return true;
}

2)註冊攔截器

/*自動做好靜態資源映射,只需要排除登錄界面和登錄請求即可*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/", "/index.html", "/user/login");
}
2.集成日誌

抽象層 : SLF4J

實現層 : LogBack

如何讓系統中所有的日誌都統一到slf4j;

1、將系統中其他日誌框架先排除出去;

2、用中間包來替換原有的日誌框架;

3、我們導入slf4j其他的實現

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}
logging.file=springboot.log             #當前路徑下的springboot.log文件
logging.path=/springboot/log            #spring.log爲默認文件
#  在控制檯輸出的日誌的格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
# 指定文件中日誌輸出的格式
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n

指定框架配置文件

logback-spring.xml

@EndWebMvc      全面接管sping配置(不建議使用)

6.定製錯誤頁面

其實設置過攔截器後,當訪問錯誤頁面時,總會跳至首頁。

爲的是登陸後的報錯提示而準備。

3.RestfulCRUD

CRUD滿足Rest風格;

URI: /資源名稱/資源標識 HTTP請求方式區分對資源CRUD操作

普通CRUD(uri來區分操作) RestfulCRUD
查詢 getEmp
添加 addEmp?xxx
修改 updateEmp?id=xxx&xxx=xx
刪除 deleteEmp?id=1

簡單說一下修改和刪除

修改

爲簡化開發,修改和新增使用同一界面

跳入界面提前判斷當前是否有傳入數據(有數據則是修改,沒有數據是新增)

th:value="${emp!=null}?${emp.lastName}"
th:text="${emp!=null}?'修改':'添加'"

根據CRUD規則,新增是POST請求,修改是PUT請求

<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
<input type="hidden" name="id" th:if="${emp!=null}" th:value="${emp.id}">

刪除

刪除使用form表單提交

爲避免每一個刪除按鍵都是form,將表單單獨拿出,使用js提交。

<button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">刪除</button>

th:attr=”del_uri=@{/emp/}+${emp.id}” : 自定義del_uri屬性,放入當前id

<form  method="post" id="del">
    <input type="hidden" name="_method" value="delete">
</form>

定義提交的表單

<script>
    $(".deleteBtn").click(function () {
        $("#del").attr("action",$(this).attr("del_uri")).submit();
        return false;
    });
</script>

國際化

CSDN博客總結

天氣接口

支付接口

SSO單點登錄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章