在使用SpringBoot開發(fā)web應(yīng)用時(shí)實(shí)際采用的是spring mvc來(lái)實(shí)現(xiàn),現(xiàn)在采用REST風(fēng)格的開發(fā)方式越來(lái)越多spring當(dāng)然也支持這一開發(fā)模式。
代碼環(huán)境spring boot 頁(yè)面配置為使用jsp的方式
rest模式我就不介紹了,主要是GET、POST、PUT、DELETE方法,spring mvc也提供了對(duì)應(yīng)的實(shí)現(xiàn)方式
@RequestMapping(method=RequestMethod.GET)
@RequestMapping(method=RequestMethod.POST)
@RequestMapping(method=RequestMethod.PUT)
@RequestMapping(method=RequestMethod.DELETE)
當(dāng)然也可以使用
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
這與上面的是一樣的效果
下面我們來(lái)一個(gè)一個(gè)的實(shí)現(xiàn)
GetMapping
@GetMapping("/httpMethod") public String httpMethd(){ return "test/test"; }
使用這個(gè)代碼我們定義了一個(gè)get方法,通過這個(gè)方法spring boot會(huì)自動(dòng)跳轉(zhuǎn)到test目錄下的test.jsp頁(yè)面當(dāng)然你還可以在@GetMapping下面再加上@ResponseBody,這樣返回的就是一個(gè)字符串而不是跳轉(zhuǎn)頁(yè)面了。
PostMapping
test.jsp頁(yè)面
<%-- Created by IntelliJ IDEA. User: jacky Date: 17-10-3 Time: 下午4:53 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>HttpMethodTest</title> </head> <body> <form action="/httpMethod" method="post"> 名稱 <input type="text" name="name"> <br> 密碼 <input type="text" name="pwd"> <br> <input type="submit" name="提交"> </form> </body> </html>
controller類
@PostMapping("/httpMethod") @ResponseBody public String httpMethod(@RequestParam String name,@RequestParam String pwd){ System.out.println("sent name is "+name); System.out.println("sent pwd is "+pwd); return "success"; }
這里表單定義了提交方法為post,這時(shí)提交表單在后臺(tái)頁(yè)面就能打印處理提交的信息,同時(shí)會(huì)向前臺(tái)返回success字符串。
PutMapping DeleteMapping
本質(zhì)上瀏覽器端的form表單只支持GET和POST方法并不支持PUT和DELETE方法,但是spring已經(jīng)解決了這個(gè)問題,從spring3.0開始定義了一個(gè)filter來(lái)支持對(duì)PUT和DELETE方法的解析,下面我們就來(lái)看看怎么處理。
首先我們要對(duì)jsp頁(yè)面的表單做一個(gè)簡(jiǎn)單的修改
<%-- Created by IntelliJ IDEA. User: jacky Date: 17-10-3 Time: 下午4:53 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>HttpMethodTest</title> </head> <body> <form action="/upload/httpMethod" method="post"> <input type="hidden" name="_method" value="PUT/DELETE"> 名稱 <input type="text" name="name"> <br> 密碼 <input type="text" name="pwd"> <br> <input type="submit" name="提交"> </form> </body> </html>
對(duì)比一下就看到了吧,我們添加了一個(gè)隱藏的輸入框名字叫_method,值為PUT或者是DELETE
controller代碼
@PutMapping("/httpMethod") @ResponseBody public String httpMethodPut(@RequestParam String name,@RequestParam String pwd){ System.out.println("put sent name is "+name); System.out.println("put sent pwd is "+pwd); return "success"; } @DeleteMapping("/httpMethod") @ResponseBody public String httpMethodDel(@RequestParam String name,@RequestParam String pwd){ System.out.println("delete sent name is "+name); System.out.println("delete sent pwd is "+pwd); return "success"; }
下面是重點(diǎn)了,引入filter,直接看代碼
@Configuration @ImportResource({"classpath:applicationContext.xml"}) public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(MaterialApplication.class); } @Bean public FilterRegistrationBean httpMethodFilterRegistrationBean() { FilterRegistrationBean registrationBean=new FilterRegistrationBean(); registrationBean.setFilter(httpMethodFilter()); registrationBean.addUrlPatterns("/*"); registrationBean.setName("HttpMethodFilter"); registrationBean.setOrder(1); return registrationBean; } @Bean public Filter httpMethodFilter(){ return new HiddenHttpMethodFilter(); }
核心的filter就是HiddenHttpMethodFilter,這是spring自己定義來(lái)處理put和delete請(qǐng)求的filter,只需要配置這個(gè)filter過濾所有請(qǐng)求就行了,當(dāng)然你還可以考慮過濾指定的路徑請(qǐng)求。
配置好后就可以測(cè)試一下結(jié)果。
跳轉(zhuǎn)到郵箱:
<a href="mailto:someone@microsoft.com?subject=Hello%20again">發(fā)送郵件</a>
<a href="mailto:someone@microsoft.com?cc=someoneelse@microsoft.com&bcc=andsomeoneelse2@microsoft.com&subject=Summer%20Party&body=You%20are%20invited%20to%20a%20big%20summer%20party!">發(fā)送郵件!</a>
圖像映射:
創(chuàng)建圖像映射需要img、map、area三個(gè)標(biāo)簽同時(shí)存在,img 元素中的 "usemap" 屬性引用 map 元素中的 "id" 或 "name" 屬性,area中coords屬性為設(shè)置或返回圖像映射中可點(diǎn)擊區(qū)域的坐標(biāo),area標(biāo)簽可以設(shè)置多個(gè)。相關(guān)文檔推薦:https://www.w3school.com.cn/html/html_images.asp
<body>
<img src="img.PNG" usemap="#buhuo" alt="捕獲" />
<map name="buhuo" id="buhuo">
<area shape="circle" coords="10,10,100,100" href="上次閱讀位置.PNG" target="_self"alt="沒有圖片!" />
<area shape="circle" coords="10,10,100,100" href="上次閱讀位置.PNG" target="_self"alt="沒有圖片!" />
</map>
</body>
特殊字符:標(biāo)簽會(huì)被解析,采用特殊字符代碼可以代替特殊字符
錨點(diǎn)定位:
通過錨點(diǎn)鏈接,用戶可以快速的定位到目標(biāo)。
<a href="#two">定位到標(biāo)題2</a><!-- 使用<a href="#id名">鏈接文本</a>,注意#的使用 -->
<h3>標(biāo)題1</h3>
<h3 id="two">標(biāo)題2</h3><!-- 使用id名進(jìn)行標(biāo)注供錨點(diǎn)鏈接使用 -->
<h3>標(biāo)題3</h3>
<h3>標(biāo)題4</h3>
路徑:
相對(duì)路徑:
分為三種情況:1、同級(jí)目錄 2、上一級(jí)目錄 3、下級(jí)目錄
<img src="picture.jpg"/> <!-- 同級(jí)目錄下的文件直接寫文件名引入即可 -->
<img src="images/picture.jpg"/> <!-- 下級(jí)目錄中的文件需要先進(jìn)入下一級(jí)目錄,然后/找到文件后寫文件名即可 -->
<img src="../images/picture.jpg"/> <!-- 上級(jí)目錄中的文件需要先../進(jìn)入上級(jí)目錄,再使用同級(jí)目錄的方法查找文件 -->
絕對(duì)路徑:
絕對(duì)路徑是相對(duì)于計(jì)算機(jī)或者網(wǎng)站(網(wǎng)址)而言的,一般很少使用絕對(duì)路徑
<img src="C:\Users\17121\Desktop\picture.jpg"/> <!-- 電腦上面絕對(duì)路徑幾乎沒人使用,因?yàn)閾Q電腦后,文件就無(wú)法使用 -->
<img src="http://webimages/picture.jpg"/> <!-- 網(wǎng)上的絕對(duì)路徑經(jīng)常會(huì)被使用,但是文件一旦消失或者主機(jī)和域名過期,將無(wú)法使用 -->
提示:本文圖片等素材來(lái)源于網(wǎng)絡(luò),若有侵權(quán),請(qǐng)發(fā)郵件至郵箱:810665436@qq.com聯(lián)系筆者刪除。
筆者:苦海123
其它問題可通過以下方式聯(lián)系本人咨詢:
QQ:810665436
微信:ConstancyMan
例
帶有可點(diǎn)擊區(qū)域的圖像映射:
<img src="planets.gif" width="145" height="126" alt="Planets" usemap="#planetmap">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>
瀏覽器支持
目前大多數(shù)瀏覽器支持 <map>標(biāo)簽。
標(biāo)簽定義及使用說(shuō)明
<map> 標(biāo)簽用于客戶端圖像映射。圖像映射指帶有可點(diǎn)擊區(qū)域的一幅圖像。
<img>中的 usemap 屬性可引用 <map> 中的 id 或 name 屬性(取決于瀏覽器),所以我們應(yīng)同時(shí)向 <map> 添加 id 和 name 屬性。
area 元素永遠(yuǎn)嵌套在 map 元素內(nèi)部。area 元素可定義圖像映射中的區(qū)域。
HTML 4.01 與 HTML5之間的差異
注意: 在 HTML5 中, 如果 id 屬性在<map> 標(biāo)簽中指定, 則你必須同樣指定 name 屬性。
HTML 與 XHTML 之間的差異
在 XHTML 中,name 屬性已經(jīng)廢棄,使用 id 屬性替換它。
屬性
屬性 | 值 | 描述 |
---|---|---|
name | mapname | 必需。為 image-map 規(guī)定的名稱。 |
全局屬性
<map> 標(biāo)簽支持全局屬性,查看完整屬性表 HTML全局屬性。
事件屬性
<map> 標(biāo)簽支持所有 HTML事件屬性。
如您還有不明白的可以在下面與我留言或是與我探討QQ群308855039,我們一起飛!
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。