SpringMvc showcase -- Mapping Controller
2014-04-28 15:02
363 查看
SpringMvc showcase – Mapping Controller
1 SimpleController
一个简单的映射@Controller public class SimpleController { @RequestMapping("/simple") public @ResponseBody String simple() { return "Hello world!"; } }
2 MappingController
(1)byPath简单的映射路径
@RequestMapping("/mapping/path") public @ResponseBody String byPath() { return "Mapped by path!"; }
(2)byPathPattern
路径模式映射
@RequestMapping(value="/mapping/path/*") public @ResponseBody String byPathPattern(HttpServletRequest request) { return "Mapped by path pattern ('" + request.getRequestURI() + "')"; }
(3)byMethod(Get/Post)
@RequestMapping(value="/mapping/method", method=RequestMethod.GET) public @ResponseBody String byMethod() { return "Mapped by path + method"; }
(4)byParameter
@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo") public @ResponseBody String byParameter() { return "Mapped by path + method + presence of query parameter!"; }
href="<c:url value="/mapping/parameter?foo=bar" />返回成功
href="<c:url value="/mapping/parameter?fooo=bar" />返回失败
(5)byParameterNegation
不带指定参数映射
@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="!foo") public @ResponseBody String byParameterNegation() { return "Mapped by path + method + not presence of query parameter!"; }
href="<c:url value="/mapping/parameter?foo=bar" />返回失败
href="<c:url value="/mapping/parameter?fooo=bar" />返回成功
(6)byHeader
@RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="FooHeader=foo") public @ResponseBody String byHeader() { return "Mapped by path + method + presence of header!"; }
```javascript $.ajax({ url: this.href, dataType: "text", beforeSend: function(req) { req.setRequestHeader("FooHeader", "foo"); }, success: function(form) { MvcUtil.showSuccessResponse(form, link); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, link); }});
ajax发送之前设置request header:FooHeader,不设置将可能不被映射
(7)byHeaderNegation
不带指定header映射
@RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="!FooHeader") public @ResponseBody String byHeaderNegation() { return "Mapped by path + method + absence of header!"; }
(8)byConsumes
$.ajax({ type: "POST", url: form.attr("action"), data: "{ \"foo\": \"barrrrrr\", \"fruit\": \"apple\" }", contentType: "application/json", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
```java @RequestMapping(value="/mapping/consumes", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE) public @ResponseBody String byConsumes(@RequestBody JavaBean javaBean) { return "Mapped by path + method + consumable media type (javaBean '" + javaBean + "')"; }
javaBean=JavaBean {foo=[barrrrrr], fruit=[apple]}
(9)produces json
//以在url上直接带.json字样也可以被映射,如:/mapping/produces.json
@RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) public @ResponseBody JavaBean byProducesJson() { return new JavaBean(); }
返回json格式的JavaBean类数据文本到前台
(10) produces xml
//以在url上直接带.xml字样也可以被映射,如:/mapping/produces.xml)
@RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_XML_VALUE) public @ResponseBody JavaBean byProducesXml() { return new JavaBean(); }
返回xml格式的数据文本到前台
相关文章推荐
- java 快速排序算法
- java Future用法和意义一句话击破
- Java快速判断是不是手机号
- Java快速判断是不是手机号
- JAVA静态导入(inport static)详解
- 如何利用eclipse调试struts2的源码
- java 中的不同类型的相互转换
- java iterator
- Java 各种Mock工具比较
- JavaSE I/O RandomAccessFile类
- java内存回收机制
- java jdk7学习笔记:InputStream与OutputStream
- Java 8 简明教程——领略Java 8的全新特性
- spring mvc 配置(xml配置详解)
- 通过JAXB注解,根据java类生成xml内容
- Java OCR 图像智能字符识别技术,可识别中文
- 获取JAVA文件路径
- 观察者模式
- java参数传递
- 彻底解决 Eclipse出现the user operation is waiting的问题