springmvc? 5?? ?? ??: 1. ?? ?? ??? ???? ? ???? @RequestMapping 2. ?? ???? ?? ???? ?? ?? ??? ????? ???? ? ???? @RequestParam @PathVariable - ?? ??? ???? ? ?????.
@RequestMapping
? ?? ?? ??? ???? ? ???? ?????.
??? ? ???? ?????. ????? ????? ?? ??? ???? ???? ?? ???? ? ??? ?? ??? ????? ?????.
Attribute
value
: ??? ?? ??? ?????. ?? ???? ?? ?? ?? ?? ?? ??? ???? ? ???? ??? ?? ????(URI ??? ?? ?? ??)value
: 指定請(qǐng)求的實(shí)際地址,值可以是普通的具體值,可以指定為含有某變量的一類值(URI Template Patterns with Path Variables)
可以指定為含正則表達(dá)式的一類值(URI Template Patterns with Regular Expressions)method
: 指定請(qǐng)求的method類型,GET、POST、PUT、DELETE等consumes
: 指定處理請(qǐng)求的提交內(nèi)容類型(Content-Type),例如application/json, text/htmlproduces
: 指定返回的內(nèi)容類型,僅當(dāng)request請(qǐng)求頭中的(Accept)類型中包含該指定類型才返回params
: 指定request中必須包含某些參數(shù)值是,才讓該方法處理headers
: 指定request中必須包含某些指定的header值,才能讓該方法處理請(qǐng)求
??1.處理get請(qǐng)求:?@RequestMapping(value?=?"index",method?=?RequestMethod.GET) ??2.springboot錯(cuò)誤處理(使用app客戶端返回json格式,使用瀏覽器返回html錯(cuò)誤頁(yè)) ???@RequestMapping(produces?=?"text/html") ??3.方法僅處理request?Content-Type為“application/json”類型的請(qǐng)求 ???@RequestMapping(value?=?"/pets",?consumes="application/json") ??4.僅處理請(qǐng)求中包含了名為“myParam”,值為“myValue”的請(qǐng)求 ???@RequestMapping(value?=?"/pets/{petId}",?params="myParam=myValue")? ??5.僅處理request的header中包含了指定“Refer”請(qǐng)求頭和對(duì)應(yīng)值為“http://www.rxy.com/”的請(qǐng)求 ???@RequestMapping(value?=?"/pets",?headers="Referer=http://www.rxy.com/")
@RequestParam
用于將請(qǐng)求參數(shù)區(qū)數(shù)據(jù)映射到功能處理方法的參數(shù)上
適用:方法參數(shù)
屬性
value/name
: 兩個(gè)屬性都指代參數(shù)名字,即入?yún)⒌恼?qǐng)求參數(shù)名字(通常表單name屬性)required
: 是否必須,默認(rèn)是true,表示請(qǐng)求中一定要有相應(yīng)的參數(shù),否則將拋出異常defaultValue
: 默認(rèn)值,表示如果請(qǐng)求中沒(méi)有同名參數(shù)時(shí)的默認(rèn)值,設(shè)置該參數(shù)時(shí),自動(dòng)將required設(shè)為false
??如果是原子類型,不管加沒(méi)加注解,都必須有值,否則拋出異常,如果允許空值請(qǐng)使用包裝類代替 ??index(@RequestParam?Integer?num){}??表示該參數(shù)必須傳遞,值允許為空 ??表示該參數(shù)非必須,如果不傳則默認(rèn)為0 ??getPageData(@RequestParam(name="pageNum",defaultValue="0")?String?pageNo,?String?pageSize)
@PathVariable
用于將請(qǐng)求URL中的模板變量映射到功能處理方法的參數(shù)上,即取出uri模板中的變量作為參數(shù)
適用:方法參數(shù)
屬性
value: 指定url模版變量名稱,如果名稱與方法參數(shù)名不一樣,則需要指定,否則可省略
????@RequestMapping("/index/{id}") ?????????public?String?index(@PathVariable("id")?String?sdf){ ???????????System.out.println(sdf); ???????????return?"index"; ?????}
@ResponseBody
該注解用于將Controller
的方法返回的對(duì)象,通過(guò)適當(dāng)?shù)?code>HttpMessageConverter轉(zhuǎn)換為指定格式后,寫(xiě)入到Response
對(duì)象的body
數(shù)據(jù)區(qū),默認(rèn)springmvc
以json
形式返回(使用jackson
轉(zhuǎn)換器)
適用:方法,返回的數(shù)據(jù)不是html標(biāo)簽的頁(yè)面,而是其他某種格式的數(shù)據(jù)時(shí)(如json、xml等)使用
對(duì)比:@RequestBody
將HTTP請(qǐng)求正文轉(zhuǎn)換為適合的HttpMessageConverter對(duì)象
@ResponseBody
?? ???? ??? ? ??(?? ???? ??? URI ??? ??)
???
: ??? ??? ??? GET, POST, PUT, DELETE ??? ?????.
consumes
: ?? ??? ?? ??? ??? ??(Content-Type)? ?????(?: application/json, text/html
produces
: ??? ??? ?? ??, ?? ??? (??) ??? ??? ??? ???? ??? ??? ?????params
: ??? ??? ???? ?? ???? ?? ?? ???? ?? ???? ???headers: ??? ???? ??? ?? ????? ????? ???. ?? ?? ???? ??? ??? ? ????<br><pre class="brush:php;toolbar:false">?????????$.ajax({
???????????type:?"POST",
???????????url:"/role/saveRole",
???????????contentType:"application/json",
???????????data:?JSON.stringify(_self.form)...</pre>
<br>@RequestParam<br><br>? ?? ???? ?? ???? ?? ?? ??? ????? ???? ? ?????.<br>?? ??: ??? ????<br>
?/??
: ? ?? ?? ???? ??, ? ??? ?? ????? ??? ?????(????? ?? ?? ??)
?? code>: ???? ??? ???? ???? ??? ?? ????? ??? ?? ???? true???. ??? ??? ??? ?????.<p><code>defaultValue
: ???, ?? ?? ???? ?????. ? ????? ???? ??? ???? false? ?????
@RequestMapping(value?=?"/saveRole",method?=?RequestMethod.POST) public?String?saveRole(@RequestBody?People?requestJson)?{}
@PathVariable
?? ?? URL? ??? ??? ?? ?? ??? ????, ? take? ?????. URI ???? ??? ????
?? ??: ??? ????
??
?: ??? ??? ???? ??? ??? ?? URL ??? ?? ??? ?????. ??? ???? ???. ??? ??? ?? ??
獲取cookie中的JSESSIONID public?String?index(@CookieValue("JSESSIONID")?String?cookie){}@ResponseBody????? ???
Controller
? ????? ??? ??? ??? HttpMessageConverter
? ?? ??? ??? ???? ? ?????. ??? ??? ? , Response
??? body
??? ??? ???. ????? springmvc
? json ???? ?????.
(jackson ??? ??) ???? ??: ???, ??? ???? html ??? ?? ???? ??? ?? ??(?: json, xml ?)? ???? ?? , ??Contrast: @RequestBody HTTP ?? ??? ??? HttpMessageConverter ??? ????@<code>ResponseBody
??? ?? ??? HTTP ?? ???? ???? ??? ??? ?????. HttpMessageConverter? ?? ???? ??? ??? ??????@RequestBody ????1. ? ??? ?? ??? ?? ??? ?? ???? ?? ?? HttpMessageConverter? ???? ?? ?? ??? ?? ?? ???? ??? ????? ? ?????. ??2. ?? ?? HttpMessageConverter?? ??? ?? ???? ?????? ???? ?? ??? ?????. ???? ??: ??? ?? ??, ??? Content-Type: application/json, application/xml ? ??? ???????application/x-www-form-urlencoded? ?? ?? ???? put?? ?????. ?? POST/GET ???? ?? ?? ??(?, @RequestParam, @ModelAttribute? ??? ? ???? ???? ??) ???) ?? multipart/form-data? ?? @RequestBody? ? ??? ???? ??? ? ????. ?? ??: ??: ?????? ???? true???. ?? ??? ?? ????? ??? ?? ?????. ??? ??? ??? ?????. : ????? ? ??? ???? ??? ??? ajax ??? ??? ?? ??? ??? ????. ??獲取請(qǐng)求中Accept-Encoding值,返回gzip,?deflate,?br public?String?index(@RequestHeader("Accept-Encoding")?String?host){return?host;}????: contentType? ??? ? ???, ???? stringify? ?? json ???? ????? ???????? ?? ???? ??? ?? ??? ? ????. ??:??
package?com.rxy.controller; @Controller public?class?HelloController?{ ???? ????@ExceptionHandler({?ArithmeticException.class?}) ????@ResponseBody ????public?String?handleArithmeticException(Exception?e)?{ ????????e.printStackTrace(); ????????return?"json?data"; ????} ???? ????@ExceptionHandler({?IOException.class?}) ????public?String?handleIOException(Exception?e)?{ ????????e.printStackTrace(); ????????//返回錯(cuò)誤頁(yè)面 ????????return?"error"; ????} ????@RequestMapping("/index") ????public?String?index(){ ????????int?i?=?10?/?0; ????????return?"index"; ????} }????? ??? ?? ??? ???? ???? List
獲取請(qǐng)求中Accept-Encoding值,返回gzip,?deflate,?br public?String?index(@RequestHeader("Accept-Encoding")?String?host){return?host;}
@ExceptionHandler
注解在方法上,表示該方法用于處理特定的異常,處理范圍是當(dāng)前類,如果想要全局捕獲異常,需要使用@ControllerAdvice
當(dāng)一個(gè)Controller中有多個(gè)HandleException注解出現(xiàn)時(shí),那么異常被哪個(gè)方法捕捉呢?這就存在一個(gè)優(yōu)先級(jí)的問(wèn)題
ExceptionHandler的優(yōu)先級(jí)是:在異常的體系結(jié)構(gòu)中,哪個(gè)異常與目標(biāo)方法拋出的異常血緣關(guān)系越緊密,就會(huì)被哪個(gè)捕捉到
屬性:value: 需要處理的異常類型集合(Class)
在當(dāng)前Controller有兩個(gè)處理異常的方法,當(dāng)訪問(wèn)/index時(shí),頁(yè)面顯示: json data
package?com.rxy.controller; @Controller public?class?HelloController?{ ???? ????@ExceptionHandler({?ArithmeticException.class?}) ????@ResponseBody ????public?String?handleArithmeticException(Exception?e)?{ ????????e.printStackTrace(); ????????return?"json?data"; ????} ???? ????@ExceptionHandler({?IOException.class?}) ????public?String?handleIOException(Exception?e)?{ ????????e.printStackTrace(); ????????//返回錯(cuò)誤頁(yè)面 ????????return?"error"; ????} ????@RequestMapping("/index") ????public?String?index(){ ????????int?i?=?10?/?0; ????????return?"index"; ????} }
? ??? springmvc?? ????? ???? 5?? ?? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

SpringBoot? SpringMVC? ?? Java ???? ????? ???? ???????? ? ???? ? ?? ??? ???? ????. ? ????? ? ? ?????? ??? ??? ???? ???? ??? ????. ?? SpringBoot? ?? ?????. SpringBoot? Spring ?????? ???? ?? ??????? ?? ? ??? ????? ?? Pivotal ??? ???????. ?? ??? ?? ??? ???? ??? ??? ??? ?????.

SpringBoot? SpringMVC? ???? ?????? SpringBoot? SpringMVC? ? ?????? ??? ?? ?? ?? ?? ? ?? Java ?? ????????. ??? ???? ??? ??? ???? ?????. ??, SpringBoot? Spring ?????? ?? ?? ??? ???? ??? ? ????. ???? ?? ?? Spring ??????? ??? ? ?? ????? ?????? ???????.

spring? springmvc? ???: 1. ?? ?? ? ?? 2. ?? ?? 4. ??? ?? ??: 1. ???? ? ?? Spring? ??? ??, ?? ?? ?????, ???? ?? ? ?? ??? ???? ???? ?????? ?? ???????, Spring MVC? ?????? ?? ??????? ??? ?????? ???????. Spring ?????? ? ?????? ??? ???? MVC ??? ?????. 2. ?? ?? ?.

SpringBoot? SpringMVC? Java ??? ????? ???? ? ?? ????????. ? ? Spring ??????? ????? ??? ???? ??? ??? ????. ?? ???? SpringBoot? SpringMVC? ??? ???? ?? ???????. 1. SpringBoot? ??: ???? ??: SpringBoot? ???? ??? ??? ?? ???? ?? ????? ?? ??????. ????? ???? ???? ????? ???? ??? ? ????.

springboot? springmvc? ???? ??? ????. 1. ??? ?? 3. ??? ?? ?? 5. ??? JAR ??? ?? ?? ?? ???? ?? 8. ??? ?? 9. ??? ???? ? ?? ?? 10. ?? ??? ???? ??

????? ?? SpringMVC? ????? ????? ????? ????? ? ???? ??? ??? ??? ?????. ?????? ??? ??? ???? ?????, ? ??? ???? ??(InterceptorChain)??? ???. ????? ???? ??? ????? ???? ??? ????? ??? ??? ???? ?????. ????? AOP ????? ???? ????? ???. ????? ??? ???: ??(Filter) ????(Intercepter)? ?? ??? ??? ??? ???? ?? JavaWeb ?????? ??? ? ????.

SpringBoot? SpringMVC? ???? ??? ?? SpringBoot? SpringMVC? Java ???? ?? ??? ?? ????????. ? ? Spring ?????? ????? ???? ???? ? ?? ??? ???? ????. ? ????? SpringBoot? SpringMVC? ???? ?? ?? ???? ???? ?????. ?? SpringBoot? ?? ?????. ????

???? ??? ? ???? ?? ? ????? ????. ?????? ????? ?????? JavaAPI? ??? ?????? ????? ???? ?? ????? ? ??? ???? ????. ?? ???? ?? ?? ?????? SpringMVC? ? ??????? ?? ???? ? ??? ???. ? ????? SpringMVC ??, ???? ?? ? ??? ???? JavaAPI ???? ? ??? ??? SpringMVC? ???? ??? ??? ?????.
