javascript
SpringMVC使用及知识点提炼
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
- value映射路徑
- method請求方式,post或者get
- params限定包含某參數(shù),或者限定參數(shù)符合某個條件
- hearder限定請求頭部
- 映射路徑滿足ant通配符,?代替某個字符,*代替某個文件名,**代替某個路徑
- @PathVariable 注解占位,例如:@RequestMapping(/test/{id}),方法參數(shù)寫為:(@PathVariable("id") int id)
4.HiddenHttpMethodFilter: 將post請求可以轉(zhuǎn)化為delete和put請求,處理REST風格的請求。
5.使用@RequestParam注解設定方法的參數(shù)名稱,例如:test(@RequestParam(value="username", required=false defaultValue="admin") String username)
6 使用@RequestHeader 得到請求頭,類似@RequestParam
7.使用@CookieValue 得到cookie值,類似@RequestParam
8.可以使用POJO作為方法參數(shù),可以自動匹配,參數(shù)名直接使用pojo對象的屬性名,而不用pojo.屬性名。
9.可以使用servlet原生API作為參數(shù),支持類型如下:
10.ModelAndView:使用ModelAndView作為方法的返回值類型,SpringMVC會把ModelAndView的model中的數(shù)據(jù)放到request域?qū)ο笾小?/p> ModelAndView mav = new ModelAndView(); mav,addObject("time", new Date());
在對應的頁面上這樣接收:
time:${requestScope.time}11.可以使用Map形式作為方法參數(shù),將數(shù)據(jù)放到map中,return返回頁面的名稱。
12.使用@SessionAttributes將數(shù)據(jù)共享到session,可以通過屬性名和屬性的對象類型指定,哪些數(shù)據(jù)可以放到session中,該注解只能放到類上面。
13.@ModelAttributes 更新操作,某個屬性值前臺不傳,并且不想被置空,使用該注解可以從數(shù)據(jù)庫中取出對象。使用該注解標記的方法,會在每個目標方法執(zhí)行之前被SpringMVC調(diào)用,SpringMVC從Map中取出對象,并把表單請求參數(shù)賦給該對象的對應屬性,最后SpringMVC把上述對象傳入目標方法的參數(shù)。
14.配置直接轉(zhuǎn)發(fā)的頁面,無須再經(jīng)過handler
<mvc:view-controller path="/success" view-name="success"> <mvc:annotation-driven></mvc:annotation-driven>15.重定向與轉(zhuǎn)發(fā):使用返回的字符串開頭為redirect或者forward。
16.返回json類型數(shù)據(jù):方法上使用注解@ResponseBody,需要使用jackson的jar包
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.4</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.9.4</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.9.4</version></dependency>springmvc配置文件:
<!-- 定義跳轉(zhuǎn)的文件的前后綴 ,視圖模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><!--json視圖攔截器,讀取到@ResponseBody的時候去配置它--><ref bean="mappingJacksonHttpMessageConverter"/></list></property></bean><!--json轉(zhuǎn)化器,它可以將結(jié)果轉(zhuǎn)化--><bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="supportedMediaTypes"><list><value>application/json;charset=UTF-8</value></list></property></bean>轉(zhuǎn)載于:https://my.oschina.net/u/1011854/blog/1620026
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的SpringMVC使用及知识点提炼的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 产品经理如何基于需求迭代产品(下篇3):
- 下一篇: List的Clear方法与RemoveA