SPR-8803 Refine UriComponentsBuilder.replaceQueryParam().

If no values are given, the query parameter is removed.
master
Rossen Stoyanchev 13 years ago
parent dc88a7c8ba
commit 2a39f34d33
  1. 6
      org.springframework.web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java
  2. 2
      org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java
  3. 14
      spring-framework-reference/src/mvc.xml

@ -404,7 +404,7 @@ public class UriComponentsBuilder {
/**
* Sets the query parameter values overriding all existing query values for the same parameter.
* If no values are given, the resulting URI will contain the query parameter name only.
* If no values are given, the query parameter is removed.
*
* @param name the query parameter name
* @param values the query parameter values
@ -413,7 +413,9 @@ public class UriComponentsBuilder {
public UriComponentsBuilder replaceQueryParam(String name, Object... values) {
Assert.notNull(name, "'name' must not be null");
this.queryParams.remove(name);
queryParam(name, values);
if (!ObjectUtils.isEmpty(values)) {
queryParam(name, values);
}
return this;
}

@ -240,7 +240,7 @@ public class UriComponentsBuilderTests {
builder.replaceQueryParam("baz");
result = builder.build();
assertEquals("baz", result.getQuery());
assertNull("Query param should have been deleted", result.getQuery());
}
}

@ -2905,11 +2905,14 @@ URI uri = uriComponents.expand("42", "21").encode().toUri();
<para>In a Servlet environment the
<classname>ServletUriComponentsBuilder</classname> sub-class provides
static factory methods to copy available URL information from a
Servlet request including host, scheme, port, path and query string:
Servlet requests:
</para>
<programlisting language="java">HttpServletRequest request = ...
// Re-use host, scheme, port, path and query string
// Replace the "accountId" query param
ServletUriComponentsBuilder ucb =
ServletUriComponentsBuilder.fromRequest(request).replaceQueryParam("accountId", "{id}").build()
.expand("123")
@ -2919,7 +2922,9 @@ ServletUriComponentsBuilder ucb =
<para>Alternatively, you may choose to copy a subset of the available
information up to and including the context path:</para>
<programlisting language="java">// Host, port and context path
<programlisting language="java">// Re-use host, port and context path
// Append "/accounts" to the path
ServletUriComponentsBuilder ucb =
ServletUriComponentsBuilder.fromContextPath(request).path("/accounts").build()
</programlisting>
@ -2928,7 +2933,10 @@ ServletUriComponentsBuilder ucb =
by name (e.g. <literal>/main/*</literal>), you can also have the literal part
of the servlet mapping included:</para>
<programlisting language="java">// Host, port, context path, and the literal part of the servlet mapping
<programlisting language="java">// Re-use host, port, context path
// Append the literal part of the servlet mapping to the path
// Append "/accounts" to the path
ServletUriComponentsBuilder ucb =
ServletUriComponentsBuilder.fromServletMapping(request).path("/accounts").build()
</programlisting>

Loading…
Cancel
Save