博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3.2、组合的@RequestMapping变种
阅读量:7135 次
发布时间:2019-06-28

本文共 2241 字,大约阅读时间需要 7 分钟。

    Spring 4.3 中引进了下面的注解 @RequestMapping 在方法层级的变种,来帮助简化常用 HTTP 方法的映射,并更好地表达被注解的方法的语义。比如,@GetMapping可以读作 GET @RequestMapping。

  1. @GetMapping

  2. @PostMapping

  3. @PutMapping

  4. @DeleteMapping

  5. @PatchMapping

    下面是一个示例:

1)编写 JSP 页面

    首先在上一篇中的项目中的 helloWorld.jsp 中追加下面的代码


Composed RequestMapping

Test1
Test2
Username:
Password:

2)定义一个控制器

    在代码中,添加下面的控制器:

package com.techmap.examples.controllers;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.format.annotation.DateTimeFormat;import org.springframework.format.annotation.DateTimeFormat.ISO;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;/** * 组合的 @RequestMapping。 */@Controller@RequestMapping("/composed")public class ComposedController{        @GetMapping("/get")    public String get()    {        return "/examples/targets/test1";    }        /**     * 带有 URI 模板     */    @GetMapping(path = "/{day}")    public String getForDay(@PathVariable @DateTimeFormat(iso = ISO.DATE) Date day, Model model)    {        System.out.println("--> " + new SimpleDateFormat("yyyy-MM-dd").format(day));                return "/examples/targets/test2";    }        @PostMapping("/post")    public String post(            @RequestParam(value="username") String user,             @RequestParam(value="password") String pass            )    {        System.out.println("--> Username: " + user);        System.out.println("--> Password: " + pass);                return "/examples/targets/test3";    }}

3)测试

    点击 Composed RequestMapping 下面的 test1 和 test2 超链接,会正常进行页面跳转。输入用户名和密码,并点击“登录”按钮后,也会进行跳转,但是控制台会像下面那样打印出输入的用户名密码(我输入的用户名和密码都是inspector):

......DEBUG 2016-09-07 08:31:24,923 Returning cached instance of singleton bean 'composedController'  (AbstractBeanFactory.java:249) --> Username: inspector--> Password: inspectorDEBUG 2016-09-07 ......

转载地址:http://eztrl.baihongyu.com/

你可能感兴趣的文章
Oracle 12C 新特性之表分区带 异步全局索引异步维护(一次add、truncate、drop、spilt、merge多个分区)...
查看>>
Mybatis动态SQL生成
查看>>
javascript 正则表达式学习
查看>>
关于Asp.Net中避免用户连续多次点击按钮,重复提交表单的处理
查看>>
js页面刷新一次
查看>>
坑人的运算符
查看>>
spring security custom-filter with java configuration 验证码验证
查看>>
01、BootstrapperShell
查看>>
Automation Test in Maya Plugin Development
查看>>
DRM 简介
查看>>
idea编译时JDK版本变化
查看>>
搭建LoadRunner中的场景(一) 创建场景
查看>>
蓄水池算法
查看>>
POJ2049
查看>>
** turtle模块和random模块
查看>>
javaScript-进阶篇(一)
查看>>
介绍SmartUpload很好的网站
查看>>
工厂模式
查看>>
装机的一些问题
查看>>
mysql主从同步
查看>>