博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3.Spring MVC 4.1-@RequestMapping
阅读量:4118 次
发布时间:2019-05-25

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

2.1 @RequestMapping

  • @RequestMapping是SpringMVC的核心注解,负责访问的url与调用方法之间的映射;
  • @RequestMapping可以放在类和方法上;
    • @RequestMapping的属性produces属性控制response返回的形式;
    • @RequestMapping的属性method属性控制接受访问的类型,不写不做限制,本例为演示方便全部都是get请求;
  • @ResponseBody(放在方法上或者返回值类型前)将方法参数放置在web body的body中(返回的不是页面而是你所控制的字符)
  • @RequestBody(放在方法参数前)将方法参数放置在web request的body中(如提交一个json对象作为参数-在03点睛Spring MVC 4.1-REST演示)
  • produces的内容是指定返回的媒体类型让浏览器识别
    • 如返回text/plain的话,chrome浏览器下network显示Response的Content-Type:text/plain;
    • 如返回application/json的话,chrome浏览器下network显示Response的application/json;
    • 因本节无页面,在03点睛Spring MVC 4.1-REST有只管的阐述和演示;
  • 这节使用@RequestMapping演示常用映射场景

2.2 演示

  • 传值对象
package com.wisely.web;public class DemoObj {    private Long id;    private String name;    public DemoObj() {        super();    }    public DemoObj(Long id, String name) {        super();        this.id = id;        this.name = name;    }    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}
  • 控制器 TestController
package com.wisely.web;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;@Controller //声明为控制器bean@RequestMapping("/test")// 根地址为http://localhost:8080/testSpringMVC/testpublic class TestController {    //response媒体类型(MediaType)为text/plain,编码是utf-8    @RequestMapping(produces = "text/plain;charset=UTF-8")    //映射地址为http://localhost:8080/testSpringMVC/test    @ResponseBody //此注解让返回值不是页面,也是将结果字符串直接返回    public  String root(HttpServletRequest request){        return "url:"+request.getRequestURL()+" 可以访问此方法";    }    @RequestMapping(value = "/add",produces = "text/plain;charset=UTF-8")    //映射地址为http://localhost:8080/testSpringMVC/test/add    @ResponseBody    public   String add(HttpServletRequest request){        return "url:"+request.getRequestURL()+" 可以访问此方法";    }    @RequestMapping(value = {
"/remove","/delete"},produces = "text/plain;charset=UTF-8") //映射地址为http://.../test/remove(或http://.../test/delete) @ResponseBody public String remove(HttpServletRequest request){ return "url:"+request.getRequestURL()+" 可以访问此方法"; } //获取request参数 //获取路径参数 @RequestMapping(value = "/get",produces = "text/plain;charset=UTF-8") //映射路径http://.../test/get?id=123 @ResponseBody public String passRequestParam(@RequestParam Long id,HttpServletRequest request){ System.out.println("id为"+id); return "url:"+request.getRequestURL()+" 可以访问此方法"; } //获取路径参数 @RequestMapping(value = "/{id}",produces = "text/plain;charset=UTF-8") //映射路径http://.../test/123 @ResponseBody public String passPathVariable(@PathVariable Long id,HttpServletRequest request){ System.out.println("id为"+id); return "url:"+request.getRequestURL()+" 可以访问此方法"; } //获得对象 @RequestMapping(value = "/pass",produces = "text/plain;charset=UTF-8") //映射路径http://.../test/pass?id=123&name=wyf @ResponseBody public String passObj(DemoObj obj,HttpServletRequest request){ System.out.println("对象的id和名称分别为为:"+obj.getId()+"/"+obj.getName()); return "url:"+request.getRequestURL()+" 可以访问此方法"; }}

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

你可能感兴趣的文章
Reverse Integer--反转整数
查看>>
Container With Most Water --装最多水的容器(重)
查看>>
Longest Common Prefix -最长公共前缀
查看>>
Letter Combinations of a Phone Number
查看>>
Single Number II --出现一次的数(重)
查看>>
Valid Parentheses --括号匹配
查看>>
Remove Element--原地移除重复元素
查看>>
Remove Duplicates from Sorted Array--从有序数组中移除重复元素
查看>>
Count and Say
查看>>
Gas Station
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
Valid Palindrome 简单的回文判断
查看>>
Pascal's Triangle -- 生成杨辉三角
查看>>
Pascal's Triangle II 生成杨辉三角中的某行
查看>>
Minimum Depth of Binary Tree -- 二叉树的最小深度 DFS 加剪枝
查看>>
Climbing Stairs 爬楼梯方法 动态规划
查看>>
Merge Two Sorted Lists 合并两个有序链表
查看>>
pow(x,n) 为什么错这么多次
查看>>
Jump Game 动态规划
查看>>
Binary Tree Maximum Path Sum 自底向上求解(重重重重)
查看>>