Spring的UriComponents

UriComponents对象是不可变的,expand和encode方法可以返回新的URIComponents

@RequestMapping(value = "/user/{name}")
    public ResponseEntity<String> uri(HttpServletRequest req, @PathVariable String name, @RequestParam int id) {

        UriComponents uriComponents = null;

        uriComponents = UriComponentsBuilder.fromUriString(
                "http://example.com/hotels/{hotel}/bookings/{booking}").build();
        URI uri = uriComponents.expand("42", "21").encode().toUri();
        System.out.println(uri.toString());

        uriComponents = UriComponentsBuilder.newInstance()
                .scheme("http").host("example.com").path("/hotels/{hotel}/bookings/{booking}").build()
                .expand("42", "21")
                .encode();
        System.out.println(uriComponents.toUri().toString());

        return null;
    }

ServletUriComponentsBuilder

提供从Servlet中复杂URL的静态方法
@RequestMapping(value = "/user/{name}")
    public ResponseEntity<String> uri(HttpServletRequest req, @PathVariable String name, @RequestParam int id) {

        UriComponents uriComponents = null;

        //重用 host, scheme, port, path 和 query
        uriComponents = ServletUriComponentsBuilder.fromRequest(req)
                .replaceQueryParam("id", "{id}")//替换id参数,没有添加上
                .build()
                .expand("1")
                .encode();
        System.out.println(uriComponents.toUri().toString());

         重用 host,port
        uriComponents = ServletUriComponentsBuilder.fromContextPath(req)
                .path("/accounts")// 用/accouts替换换来的path部分
                .build();
        System.out.println(uriComponents.toUri().toString());

        uriComponents = ServletUriComponentsBuilder.fromServletMapping(req)
                .path("/accounts")//用/accouts替换换来的path部分
                .build();
        System.out.println(uriComponents.toUri().toString());
        return null;
    }

MvcUriComponentsBuilder

可以根据控制器和handler方法来生成URL
@RequestMapping(value = "/user/{name}")
    public ResponseEntity<String> uri(HttpServletRequest req, @PathVariable String name, @RequestParam int id) {

        UriComponents uriComponents = null;

        uriComponents = MvcUriComponentsBuilder
                .fromMethodName(URIComponetsController.class, "uri", null, "devin", 21).build();
        System.out.println(uriComponents.toUri().toString());

        uriComponents = MvcUriComponentsBuilder
                .fromMethodCall(on(URIComponetsController.class).uri(null, "devin", 21)).build();
        System.out.println(uriComponents.toUri().toString());

        return null;
    }
如果handler方法返回String,MvcUriComponnetsBuilder的on静态会报错 Cannot subclass final class class java.lang.String因为on方法为返回值创建代理对象,String类final的无法代理。

JSP中构建URL(s:mvcUrl方法)

mvcUrl方法中传 Controller类名中大写字母+#+handler方法名
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>

<html>
<head>
    <title>Home</title>
</head>
<body>
    <a href="${s:mvcUrl('URICC#home').build()}">Click Me</a>
</body>
</html>

@Controller
@RequestMapping(value = "/uri")
public class URIComponentsController {

    @RequestMapping(value = "/index")
    public String index(){
        return "mvc_url";
    }

}

源代码https://github.com/duansiman/SpringMVC


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值