您的位置:首页 > 编程语言 > Java开发

SPRING MVC3.2案例讲解--SPRING MVC3的各种URL映射(2)

2013-10-11 17:15 423 查看
继续上一章节,亲们没事留个言,对我是个鼓励,俺就把更有动力这个学习笔记继续下去!!!




 

JAVA代码如下:

@Controller
public class MappingController {
//对应的URL 只能是 /mapping/path
@RequestMapping("/mapping/path")
public @ResponseBody String byPath() {
return "Mapped by path!";
}
// 对应的URL 可以是/mapping/path/abc,也可以是/mapping/path/def ;
//  /mapping/path/* :*对应任意字符串
@RequestMapping(value="/mapping/path/*", method=RequestMethod.GET)
public @ResponseBody String byPathPattern(HttpServletRequest request) {
return "Mapped by path pattern ('" + request.getRequestURI() + "')";
}

//对应的URL是/mapping/method,但必须是GET方式提交请求
@RequestMapping(value="/mapping/method", method=RequestMethod.GET)
public @ResponseBody String byMethod() {
return "Mapped by path + method";
}

//对应URL是/mapping/parameter?foo=bar ,即URL链接后必须带有foo参数
@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo")
public @ResponseBody String byParameter() {
return "Mapped by path + method + presence of query parameter!";
}

//对应URL是/mapping/parameter ,即URL链接后必须不带有foo参数,但可以是其他参数
@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="!foo")
public @ResponseBody String byParameterNegation() {
return "Mapped by path + method + not presence of query parameter!";
}
//  /mapping/header必须是GET提交,并且必须带有requestHeader(FooHeader=foo);见 js代码片段1
@RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="FooHeader=foo")
public @ResponseBody String byHeader() {
return "Mapped by path + method + presence of header!";
}

// 这个就是和上面相反的,
@RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="!FooHeader")
public @ResponseBody String byHeaderNegation() {
return "Mapped by path + method + absence of header!";
}

//  /mapping/consumes 必须是POST 提交 ;contentType必须是: "application/json" ;consumes(消费者)表示用户需要提交的数据类型为JSON

//见JS代码片段1
@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 + "')";
}

// /mapping/produces 必须是GET提交,produces(生产者),表示返回给用户的数据类型,本例是JAVABEAN转化成JSON数据
@RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody JavaBean byProducesJson() {
return new JavaBean();
}

// 类似于上面的
@RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_XML_VALUE)
public @ResponseBody JavaBean byProducesXml() {
return new JavaBean();
}

}

 

  js代码片段1

 $("#byHeader").click(function(){	var link = $(this);

$.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); }});
return false;
});

$.ajax({ type: "POST", url: form.attr("action"), data: data, contentType: "application/json", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
return false;






大小: 44.5 KB

查看图片附件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: