整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

Spring-Boot RequestMapping

Spring-Boot RequestMapping各方法的使用

pringBoot RequestMapping各方法的使用

在使用SpringBoot开发web应用时实际采用的是spring mvc来实现,现在采用REST风格的开发方式越来越多spring当然也支持这一开发模式。

代码环境spring boot 页面配置为使用jsp的方式

rest模式我就不介绍了,主要是GET、POST、PUT、DELETE方法,spring mvc也提供了对应的实现方式

@RequestMapping(method=RequestMethod.GET)

@RequestMapping(method=RequestMethod.POST)

@RequestMapping(method=RequestMethod.PUT)

@RequestMapping(method=RequestMethod.DELETE)

当然也可以使用

@GetMapping

@PostMapping

@PutMapping

@DeleteMapping

这与上面的是一样的效果

下面我们来一个一个的实现

GetMapping

 @GetMapping("/httpMethod")
 public String httpMethd(){
 return "test/test";
 }

使用这个代码我们定义了一个get方法,通过这个方法spring boot会自动跳转到test目录下的test.jsp页面当然你还可以在@GetMapping下面再加上@ResponseBody,这样返回的就是一个字符串而不是跳转页面了。

PostMapping

test.jsp页面

<%--
 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,这时提交表单在后台页面就能打印处理提交的信息,同时会向前台返回success字符串。

PutMapping DeleteMapping

本质上浏览器端的form表单只支持GET和POST方法并不支持PUT和DELETE方法,但是spring已经解决了这个问题,从spring3.0开始定义了一个filter来支持对PUT和DELETE方法的解析,下面我们就来看看怎么处理。

首先我们要对jsp页面的表单做一个简单的修改

<%--
 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>

对比一下就看到了吧,我们添加了一个隐藏的输入框名字叫_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";
 }

下面是重点了,引入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自己定义来处理put和delete请求的filter,只需要配置这个filter过滤所有请求就行了,当然你还可以考虑过滤指定的路径请求。

配置好后就可以测试一下结果。

跳转到邮箱:

 <a href="mailto:someone@microsoft.com?subject=Hello%20again">发送邮件</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!">发送邮件!</a>

图像映射:

创建图像映射需要img、map、area三个标签同时存在,img 元素中的 "usemap" 属性引用 map 元素中的 "id" 或 "name" 属性,area中coords属性为设置或返回图像映射中可点击区域的坐标,area标签可以设置多个。相关文档推荐: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>

特殊字符:标签会被解析,采用特殊字符代码可以代替特殊字符


锚点定位:

通过锚点链接,用户可以快速的定位到目标。


 <a href="#two">定位到标题2</a><!-- 使用<a href="#id名">链接文本</a>,注意#的使用 --> 
    
  <h3>标题1</h3>
  <h3 id="two">标题2</h3><!-- 使用id名进行标注供锚点链接使用 -->
  <h3>标题3</h3>
  <h3>标题4</h3>

路径:

相对路径:

分为三种情况:1、同级目录 2、上一级目录 3、下级目录


   <img src="picture.jpg"/> <!-- 同级目录下的文件直接写文件名引入即可 -->
    <img src="images/picture.jpg"/> <!-- 下级目录中的文件需要先进入下一级目录,然后/找到文件后写文件名即可 -->
    <img src="../images/picture.jpg"/> <!-- 上级目录中的文件需要先../进入上级目录,再使用同级目录的方法查找文件 -->

绝对路径:

绝对路径是相对于计算机或者网站(网址)而言的,一般很少使用绝对路径


 <img src="C:\Users\17121\Desktop\picture.jpg"/> <!-- 电脑上面绝对路径几乎没人使用,因为换电脑后,文件就无法使用 -->
    <img src="http://webimages/picture.jpg"/> <!-- 网上的绝对路径经常会被使用,但是文件一旦消失或者主机和域名过期,将无法使用 -->

提示:本文图片等素材来源于网络,若有侵权,请发邮件至邮箱:810665436@qq.com联系笔者删除。

笔者:苦海123

其它问题可通过以下方式联系本人咨询:
QQ:810665436
微信:ConstancyMan

带有可点击区域的图像映射:

<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>


浏览器支持

目前大多数浏览器支持 <map>标签。


标签定义及使用说明

<map> 标签用于客户端图像映射。图像映射指带有可点击区域的一幅图像。

<img>中的 usemap 属性可引用 <map> 中的 id 或 name 属性(取决于浏览器),所以我们应同时向 <map> 添加 id 和 name 属性。

area 元素永远嵌套在 map 元素内部。area 元素可定义图像映射中的区域。


HTML 4.01 与 HTML5之间的差异

注意: 在 HTML5 中, 如果 id 属性在<map> 标签中指定, 则你必须同样指定 name 属性。


HTML 与 XHTML 之间的差异

在 XHTML 中,name 属性已经废弃,使用 id 属性替换它。


属性

属性描述
namemapname必需。为 image-map 规定的名称。

全局属性

<map> 标签支持全局属性,查看完整属性表 HTML全局属性。


事件属性

<map> 标签支持所有 HTML事件属性。

如您还有不明白的可以在下面与我留言或是与我探讨QQ群308855039,我们一起飞!