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;

<span style="color: #a71d5d;" >public</span> <span style="color: #795da3;" >DemoObj</span>() {
<span style="color: #ed6a43;" >super</span>();
}

<span style="color: #a71d5d;" >public</span> <span style="color: #795da3;" >DemoObj</span>(<span style="color: #333333;" >Long</span> <span style="color: #ed6a43;" >id</span>, <span style="color: #333333;" >String</span> <span style="color: #ed6a43;" >name</span>) {
<span style="color: #ed6a43;" >super</span>();
<span style="color: #ed6a43;" >this</span><span style="color: #a71d5d;" >.</span>id <span style="color: #a71d5d;" >=</span> id;
<span style="color: #ed6a43;" >this</span><span style="color: #a71d5d;" >.</span>name <span style="color: #a71d5d;" >=</span> name;
}
<span style="color: #a71d5d;" >public</span> <span style="color: #333333;" >Long</span> <span style="color: #795da3;" >getId</span>() {
<span style="color: #a71d5d;" >return</span> id;
}
<span style="color: #a71d5d;" >public</span> <span style="color: #a71d5d;" >void</span> <span style="color: #795da3;" >setId</span>(<span style="color: #333333;" >Long</span> <span style="color: #ed6a43;" >id</span>) {
<span style="color: #ed6a43;" >this</span><span style="color: #a71d5d;" >.</span>id <span style="color: #a71d5d;" >=</span> id;
}
<span style="color: #a71d5d;" >public</span> <span style="color: #333333;" >String</span> <span style="color: #795da3;" >getName</span>() {
<span style="color: #a71d5d;" >return</span> name;
}
<span style="color: #a71d5d;" >public</span> <span style="color: #a71d5d;" >void</span> <span style="color: #795da3;" >setName</span>(<span style="color: #333333;" >String</span> <span style="color: #ed6a43;" >name</span>) {
<span style="color: #ed6a43;" >this</span><span style="color: #a71d5d;" >.</span>name <span style="color: #a71d5d;" >=</span> 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/test

public 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()+" 可以访问此方法";

}

<span style="color: #a71d5d;" >@RequestMapping</span>(<span style="color: #0086b3;" >value</span> <span style="color: #a71d5d;" >=</span> <span style="color: #183691;" ><span >"</span>/add<span >"</span></span>,<span style="color: #0086b3;" >produces</span> <span style="color: #a71d5d;" >=</span> <span style="color: #183691;" ><span >"</span>text/plain;charset=UTF-8<span >"</span></span>)
<span style="color: #969896;" >//映射地址为http://localhost:8080/testSpringMVC/test/add</span>
<span style="color: #a71d5d;" >@ResponseBody</span>
<span style="color: #a71d5d;" >public</span> <span style="color: #333333;" >String</span> <span style="color: #795da3;" >add</span>(<span style="color: #333333;" >HttpServletRequest</span> <span style="color: #ed6a43;" >request</span>){
<span style="color: #a71d5d;" >return</span> <span style="color: #183691;" ><span >"</span>url:<span >"</span></span><span style="color: #a71d5d;" >+</span>request<span style="color: #a71d5d;" >.</span>getRequestURL()<span style="color: #a71d5d;" >+</span><span style="color: #183691;" ><span >"</span> 可以访问此方法<span >"</span></span>;
}

<span style="color: #a71d5d;" >@RequestMapping</span>(<span style="color: #0086b3;" >value</span> <span style="color: #a71d5d;" >=</span> {<span style="color: #183691;" ><span >"</span>/remove<span >"</span></span>,<span style="color: #183691;" ><span >"</span>/delete<span >"</span></span>},<span style="color: #0086b3;" >produces</span> <span style="color: #a71d5d;" >=</span> <span style="color: #183691;" ><span >"</span>text/plain;charset=UTF-8<span >"</span></span>)
<span style="color: #969896;" >//映射地址为http://.../test/remove(或http://.../test/delete)</span>
<span style="color: #a71d5d;" >@ResponseBody</span>
<span style="color: #a71d5d;" >public</span> <span style="color: #333333;" >String</span> <span style="color: #795da3;" >remove</span>(<span style="color: #333333;" >HttpServletRequest</span> <span style="color: #ed6a43;" >request</span>){
<span style="color: #a71d5d;" >return</span> <span style="color: #183691;" ><span >"</span>url:<span >"</span></span><span style="color: #a71d5d;" >+</span>request<span style="color: #a71d5d;" >.</span>getRequestURL()<span style="color: #a71d5d;" >+</span><span style="color: #183691;" ><span >"</span> 可以访问此方法<span >"</span></span>;
}

<span style="color: #969896;" >//获取request参数</span>
<span style="color: #969896;" >//获取路径参数</span>
<span style="color: #a71d5d;" >@RequestMapping</span>(<span style="color: #0086b3;" >value</span> <span style="color: #a71d5d;" >=</span> <span style="color: #183691;" ><span >"</span>/get<span >"</span></span>,<span style="color: #0086b3;" >produces</span> <span style="color: #a71d5d;" >=</span> <span style="color: #183691;" ><span >"</span>text/plain;charset=UTF-8<span >"</span></span>)
<span style="color: #969896;" >//映射路径http://.../test/get?id=123</span>
<span style="color: #a71d5d;" >@ResponseBody</span>
<span style="color: #a71d5d;" >public</span> <span style="color: #333333;" >String</span> <span style="color: #795da3;" >passRequestParam</span>(<span style="color: #a71d5d;" >@RequestParam</span> <span style="color: #333333;" >Long</span> <span style="color: #ed6a43;" >id</span>,<span style="color: #333333;" >HttpServletRequest</span> <span style="color: #ed6a43;" >request</span>){
<span style="color: #333333;" >System</span><span style="color: #a71d5d;" >.</span>out<span style="color: #a71d5d;" >.</span>println(<span style="color: #183691;" ><span >"</span>id为<span >"</span></span><span style="color: #a71d5d;" >+</span>id);
<span style="color: #a71d5d;" >return</span> <span style="color: #183691;" ><span >"</span>url:<span >"</span></span><span style="color: #a71d5d;" >+</span>request<span style="color: #a71d5d;" >.</span>getRequestURL()<span style="color: #a71d5d;" >+</span><span style="color: #183691;" ><span >"</span> 可以访问此方法<span >"</span></span>;

}


<span style="color: #969896;" >//获取路径参数</span>
<span style="color: #a71d5d;" >@RequestMapping</span>(<span style="color: #0086b3;" >value</span> <span style="color: #a71d5d;" >=</span> <span style="color: #183691;" ><span >"</span>/{id}<span >"</span></span>,<span style="color: #0086b3;" >produces</span> <span style="color: #a71d5d;" >=</span> <span style="color: #183691;" ><span >"</span>text/plain;charset=UTF-8<span >"</span></span>)
<span style="color: #969896;" >//映射路径http://.../test/123</span>
<span style="color: #a71d5d;" >@ResponseBody</span>
<span style="color: #a71d5d;" >public</span> <span style="color: #333333;" >String</span> <span style="color: #795da3;" >passPathVariable</span>(<span style="color: #a71d5d;" >@PathVariable</span> <span style="color: #333333;" >Long</span> <span style="color: #ed6a43;" >id</span>,<span style="color: #333333;" >HttpServletRequest</span> <span style="color: #ed6a43;" >request</span>){
<span style="color: #333333;" >System</span><span style="color: #a71d5d;" >.</span>out<span style="color: #a71d5d;" >.</span>println(<span style="color: #183691;" ><span >"</span>id为<span >"</span></span><span style="color: #a71d5d;" >+</span>id);
<span style="color: #a71d5d;" >return</span> <span style="color: #183691;" ><span >"</span>url:<span >"</span></span><span style="color: #a71d5d;" >+</span>request<span style="color: #a71d5d;" >.</span>getRequestURL()<span style="color: #a71d5d;" >+</span><span style="color: #183691;" ><span >"</span> 可以访问此方法<span >"</span></span>;

}

<span style="color: #969896;" >//获得对象</span>
<span style="color: #a71d5d;" >@RequestMapping</span>(<span style="color: #0086b3;" >value</span> <span style="color: #a71d5d;" >=</span> <span style="color: #183691;" ><span >"</span>/pass<span >"</span></span>,<span style="color: #0086b3;" >produces</span> <span style="color: #a71d5d;" >=</span> <span style="color: #183691;" ><span >"</span>text/plain;charset=UTF-8<span >"</span></span>)
<span style="color: #969896;" >//映射路径http://.../test/pass?id=123&name=wyf</span>
<span style="color: #a71d5d;" >@ResponseBody</span>
<span style="color: #a71d5d;" >public</span> <span style="color: #333333;" >String</span> <span style="color: #795da3;" >passObj</span>(<span style="color: #333333;" >DemoObj</span> <span style="color: #ed6a43;" >obj</span>,<span style="color: #333333;" >HttpServletRequest</span> <span style="color: #ed6a43;" >request</span>){
<span style="color: #333333;" >System</span><span style="color: #a71d5d;" >.</span>out<span style="color: #a71d5d;" >.</span>println(<span style="color: #183691;" ><span >"</span>对象的id和名称分别为为:<span >"</span></span><span style="color: #a71d5d;" >+</span>obj<span style="color: #a71d5d;" >.</span>getId()<span style="color: #a71d5d;" >+</span><span style="color: #183691;" ><span >"</span>/<span >"</span></span><span style="color: #a71d5d;" >+</span>obj<span style="color: #a71d5d;" >.</span>getName());
<span style="color: #a71d5d;" >return</span> <span style="color: #183691;" ><span >"</span>url:<span >"</span></span><span style="color: #a71d5d;" >+</span>request<span style="color: #a71d5d;" >.</span>getRequestURL()<span style="color: #a71d5d;" >+</span><span style="color: #183691;" ><span >"</span> 可以访问此方法<span >"</span></span>;

}


}