SpringMvc showcase -- Mapping Controller

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,springMVC,mapping)