吐血整理!SpringMVC常用简单注解(超详细)

本文阅读 8 分钟
首页 代码,Java 正文

1.1需求分析

如今前端与后端主流的数据交互格式就是json,当我们前端页面需要向后端传递数据时,我们springMVC给了我们一个注解,就是@RequestBody。它可以将前端数据接收,并且为程序所用。

1.2具体情境

前端:假设为一个用户信息的收集,表单验证

后端:需要得到前端用户填写的数据

1.3最佳实操

  1. 引入webjar(用于前端主页导入JQuery) 添加maven依赖 <dependency>

    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.5.1</version>

    </dependency> 添加后,整个项目中就会有一个JQuery,路径为: http://localhost:8080/webjars/jquery/3.5.1/jquery.js

  2. 编写前端代码 我们采用ajax的发送方式,编写index.html,这样子就可以直接用localhost:8080访问 <pre><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>html</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>head</span><span class="token punctuation">></span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>测试<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>head</span><span class="token punctuation">></span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>form</span><span class="token punctuation">></span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>button<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>点我发送<span class="token punctuation">"</span></span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>sub<span class="token punctuation">"</span></span><span class="token punctuation">/&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>form</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>webjars/jquery/3.5.1/jquery.js <span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/javascript<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"> <span class="token function">$</span><span class="token punctuation">(</span>document<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">ready</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{

  <!-- --></span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#sub"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">click</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{ 
  <!-- --></span> $<span class="token punctuation">.</span><span class="token function">ajax</span><span class="token punctuation">(</span><span class="token punctuation">{ 
  <!-- --></span> url<span class="token operator">:</span><span class="token string">'http://10.136.9.228:8080/test'</span><span class="token punctuation">,</span><span class="token comment">//向后端发送的路径,请改为你自己的ip</span> contentType<span class="token operator">:</span><span class="token string">'application/json;charset=utf-8'</span><span class="token punctuation">,</span><span class="token comment">//发送的数据类型是json以及字符编码</span> data<span class="token operator">:</span><span class="token string">'{"name":"张三","age":18}'</span><span class="token punctuation">,</span><span class="token comment">//发送的数据</span> type<span class="token operator">:</span><span class="token string">'post'</span><span class="token punctuation">,</span><span class="token comment">//发送的方式</span> <span class="token function-variable function">success</span><span class="token operator">:</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span><span class="token punctuation">{ 
  <!-- --></span><span class="token comment">//成功的回调函数</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token function-variable function">error</span><span class="token operator">:</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">error</span><span class="token punctuation">)</span><span class="token punctuation">{ 
  <!-- --></span><span class="token comment">//发送失败的回调函数</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>html</span><span class="token punctuation">></span></span>

  1. 编写实体类User <pre><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>springweb<span class="token punctuation">.</span>pojo</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">AllArgsConstructor</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">Data</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">NoArgsConstructor</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">ToString</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Data</span><span class="token comment">//get,set方法</span>
<span class="token annotation punctuation">@ToString</span><span class="token comment">//tostring方法</span>
<span class="token annotation punctuation">@AllArgsConstructor</span><span class="token comment">//全参数构造方法</span>
<span class="token annotation punctuation">@NoArgsConstructor</span><span class="token comment">//无参数构造方法</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token punctuation">{

<!-- --></span>
<span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
<span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span>

<span class="token punctuation">}</span>

  1. 编写Controller <pre><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>springweb<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>springweb<span class="token punctuation">.</span>pojo<span class="token punctuation">.</span></span><span class="token class-name">User</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token operator">*</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">HashMap</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Map</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@RestController</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestController</span> <span class="token punctuation">{

<!-- --></span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/test"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">po</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">User</span> user<span class="token punctuation">)</span><span class="token punctuation">{ 
<!-- --></span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"传来的用户名是"</span><span class="token operator">+</span>user<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">", 传来的用户的年龄是"</span><span class="token operator">+</span>user<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token punctuation">}</span>

</pre>

1.4运行结果以及分析

我们这里用ip:8080的方式,如果还是访问localhost:8080,向后端发送请求时会产生跨域问题,所以采用ip:8080

img

点击后控制台输出传输到后台的数据

img

SpringMVC看见当你的User被RequestBody注解时,当前端给对应的请求时,就会自动解析JSON字符串,将字符串的内容封装入User中,但是如果json的key值与User中的对象值不相等,则认为是无效的值,所以不会封装,使用时注意key值与属性值对应即可

2.1需求分析

我们现在所上的网站基本上是这样的一个格式

img

XXX.COM/xxx/xxx

如果我们仍然用原生的servlet的话很难做到这样的url,像这样的url格式,我们称之为restful风格

2.2具体情境

我们可以用路径变量进行传值,第一个代表什么意思,第二个代表什么意思,等等

2.3最佳实操

  1. 编写Controller <pre><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/user/{id}/{name}"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">Map</span> <span class="token function">getUser</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">)</span> <span class="token class-name">Integer</span> id<span class="token punctuation">,</span>

                   <span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> name<span class="token punctuation">)</span><span class="token punctuation">{ 
    <!-- --></span>
    <span class="token class-name">Map</span> map<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">,</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">,</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> map<span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

  2. 访问 localhost:8080/user/1/liweihua

2.4运行结果以及分析

访问后的数据回显根据你的url内的值变化而变化

img

img

分析:

url中第二个路径代表id,第三个代表name,当访问匹配时,将路径变量中的参数取出并将map赋值,回显到浏览器,所以url不同,则回显的map值也不同

3.1需求分析

当一个客户机相当于浏览器向web服务器发送一个请求时,需要发送一个请求的命令行,一般是GET或者POST命令,当发送POST命令时,还会向服务器发送一个叫“Content-Length”的请求头(Request Header) 用来指明数据长度,除此之外,还会发送一些其他的Headers,如:

Accept-Charset 浏览器支持的字符编码

User-Agent (客户端的类型,一般用来区分不同的浏览器)

我们可以通过请求头来给不同客户机做出不同的兼容性体验等等

3.2具体情境

我需要对Edge浏览器和Google浏览器做出不同的反应,知道客户端的类型是怎样的,方便我们做出适配

3.3最佳实操

新增一个RequestMapping

@RequestMapping("/header")
public String b(@RequestHeader("User-Agent") String client,@RequestHeader("Accept-Encoding") String encoding){ 
    return "客户端是"+ client + "<br/>支持的编码是" + encoding;
}

不同浏览器访问localhost:8080/header

3.4运行结果以及分析

谷歌浏览器出现如下 img

IE是这样的

img

4.1需求分析

Spring3.2后,该注解出现,目的是为了拓展URL请求地址的功能

多个变量可以使用“;”(分号)来分隔,如果是一个变量的多个值,可以使用逗号来分隔,或者可以使用重复的变量名

4.2具体情境

我们知道,在实际开发中,当Cookie没有被禁用之前,客户端向服务器发送请求时,会将保存在Cookie中的sessionId取出并发送给服务器,服务器将session中的值取出,但是,当Cookie被禁用的时候,我们取不到sessionId,就没有办法取出session的值,就可以采用矩阵变量的方式,将sessionid发送给服务器,形式如下:

/path/sell;sessionId=xxxxx

4.3最佳实操

  1. 写Mapping
@RequestMapping("/school/{schoolId}")
public String a(@PathVariable("schoolId") String schoolId, @MatrixVariable("sid") int studentId){ 
    return "学校id是" + schoolId + "学生id是" + studentId;
}
  1. 访问 http://localhost:8080/school/1;sid=111

4.4运行结果以及分析

正常运行结果是

img

如果你没有过配置,那么你肯定会出现400的错误,因为SpringBoot默认禁用掉了矩阵变量的配置,将Url中的“;”(分号)自动剔除,会找不到对应的矩阵变量

解决办法:

新增一个配置类,实现WebMvcConfigurer接口,设置UrlPathHelper中的自动屏蔽分号为关闭

@Configuration
public class Config implements WebMvcConfigurer { 
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer){ 
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
}

5.1需求分析

需要对Controller中的成员变量进行初始化等操作,或者想要在访问Controller下的Mapping之前执行一段程序,就可以使用ModelAttribute

由于@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用。

5.2具体情境

Controller中有一个data成员变量,我想为它赋值,作为初始化,当有请求被响应时,可以获取初始化后的值

5.3最佳实操

  1. 编写Mapping <pre><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/modelattribute"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{

    <!-- --></span>
    <span class="token keyword">return</span> data<span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

  2. 编写ModelAttribute <pre><span class="token keyword">private</span> <span class="token class-name">String</span> data<span class="token operator">=</span><span class="token string">"none"</span><span class="token punctuation">;</span><span class="token comment">//Controller的成员变量是data</span>
    <span class="token annotation punctuation">@ModelAttribute</span>

<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{

<!-- --></span>
data<span class="token operator">=</span><span class="token string">"wuhu"</span><span class="token punctuation">;</span><span class="token comment">//执行初始化操作</span>

<span class="token punctuation">}</span>
</pre>

5.4运行结果以及分析

访问 localhost:8080/modelattribute

img

发现不再是成员变量最开始的值“none”,而是回显了一个“wuhu”

当SpringMVC匹配到请求时,会先执行被ModelAttribute注解的代码,将data赋值为wuhu,所以再次访问时值就会产生变化

6.1需求分析

将cookie中的值取出,方便服务器做出对应的响应

6.2具体情境

先设置用户端的Cookie,再从服务器中读取,发送给界面

6.3最佳实操

  1. 新建创建Cookie的Mapping <pre> <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/setCookie"</span><span class="token punctuation">)</span>

    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">setCookie</span><span class="token punctuation">(</span><span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span><span class="token punctuation">{ 
    <!-- --></span>
        <span class="token class-name">Cookie</span> cookie <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Cookie</span><span class="token punctuation">(</span><span class="token string">"userName"</span><span class="token punctuation">,</span><span class="token string">"huaye"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        response<span class="token punctuation">.</span><span class="token function">addCookie</span><span class="token punctuation">(</span>cookie<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">"设置成功!"</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  2. 新建获取Cookie的Mapping <pre><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/cookieValueTest"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">cookieValueTest</span><span class="token punctuation">(</span><span class="token annotation punctuation">@CookieValue</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"userName"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> userName<span class="token punctuation">)</span> <span class="token punctuation">{

    <!-- --></span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"通过@CookieValue获得用户名:"</span> <span class="token operator">+</span> userName<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token string">"Cookie中的用户名是"</span><span class="token operator">+</span> userName<span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

  3. 访问 localhost:8080/setCookie
    <li> 访问 localhost:8080/cookieValueTest </li>

6.4运行结果以及分析

img

再访问

img

7.1需求分析

多用于普通请求参数的提交和表单的提交等等

7.2具体情境

用户需要提交自己的id和用户名到后台验证

7.3最佳实操

  1. 编写RequestMapping <pre><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/Param"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">pa</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestParam</span><span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">)</span> <span class="token keyword">int</span> userid<span class="token punctuation">,</span><span class="token annotation punctuation">@RequestParam</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span> <span class="token class-name">String</span> username<span class="token punctuation">)</span><span class="token punctuation">{

    <!-- --></span>
    <span class="token class-name">String</span> x<span class="token operator">=</span><span class="token string">"传入的用户id是"</span><span class="token operator">+</span>userid<span class="token operator">+</span><span class="token string">"传入的用户名是"</span><span class="token operator">+</span>username<span class="token punctuation">;</span>
    <span class="token keyword">return</span> x<span class="token punctuation">;</span>

    <span class="token punctuation">}</span>

  2. 访问 localhost:8080/Param?id=xxx&name=xxx

7.4运行结果以及分析

运行结果

img

传递的参数会在后台进行匹配,id与@RequestParam中的value值进行匹配,匹配成功就赋值给被成功匹配的RequestParam注解的参数,于是id就赋值给userid;name就赋值给了username

本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/m0_46435741/article/details/120248696
-- 展开阅读全文 --
Redis底层数据结构--跳跃表
« 上一篇 04-28
BUUCTF Web [强网杯 2019]随便注
下一篇 » 06-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复