springboot訪問對映 RESTful

2021-10-01 09:41:16 字數 2009 閱讀 4019

spring_boot專欄

主目錄rest風格

問題在controller.hellocontroller.class類中【需要在類名標明@controller】:()

public string index()

【注意】

如果在控制器類controller.hellocontroller.class中以

1.配置類實現webmvcconfigurer介面。注意標註@configuration

2.重寫addviewcontrollers()方法,@override

3.註冊檢視控制器,將addviewcontroller("/login")中/login對映到檢視(頁面)setviewname(「login」) login.html

@configuration

public

class

mymvcconfig

implements

webmvcconfigurer

@configuration

public

class

mymvcconfig

implements

webmvcconfigurer};

return webmvcconfigurer;

}

uri:/資源名稱/資源標識 http請求方式區分對資源crud操作

普通crud(uri來區分操作)

restfulcrud

查詢getemp

emp—get

新增addemp?***

emp—post

修改updateemp?id=***&***=xx

emp/—put

刪除deleteemp?id=1

emp/—delete

例如:實驗功能

請求uri

請求方式

查詢所有員工

emps

get來到新增頁面

empget

新增員工

emppost

來到修改頁面(查出員工進行資訊回顯)

emp/1

get修改員工

empput

刪除員工

emp/1

delete

在實現 restful 的更新操作時,需要將表單資料以 put 方法提交:

"hidden" name=

"_method" value=

"put" th:if=

"$"/

>

emp!=null,即需要put請求進行修改時,伺服器卻將其作為post來處理。

原因:

在 spring boot 的 meta-inf/spring-configuration-metadata.json 配置檔案中,預設是關閉 spring 的 hiddenmethod 過濾器的,所以直接在表單提交的資料中新增 「_method」 資料並不起作用。:

解決方式:

# 啟用hiddenmethod過濾器

spring.mvc.hiddenmethod.filter.enabled=

true

spring boot模板對映

寫小專案的時候vue就顯的很複雜,可以用對映模板簡單代替。後端 把資料對映到模板上 返回模板查詢 getall public string getall model model 呼叫方法 return redirect user getall 前端 接受模板並輸出 返回集合 userstat 是為了...

SpringBoot 路徑對映

在spring boot中整合thymeleaf後,我們知道thymeleaf的預設的html的路徑為classpath templates也就是resources templates,那如何訪問這個路徑下面的靜態頁面呢?假設我們要訪問乙個頁面為hello.html。spring.thymeleaf...

springboot靜態資源對映

1 webjars springboot中以jar包形式引入的靜態資源 所有 webjars 都去classpath meta inf resources webjars 找資源 webjars官網 匯入靜態資源後,資源訪問路徑為 如圖所示 訪問jquery.js的路徑為 localhost 808...