5.4 The c:url Action

   

5.4 The <c:url> Action

The <c:url> action processes a URL, applying URL rewriting ”for relative URLs only ”as necessary. The <c:url> action has two syntaxes; here's one of them: [6]

[6] Items in brackets are optional. See "<c:url>" on page  490 for a more complete description of <c:url> syntax.

  <c:url value [context] [var] [scope]/>  

The mandatory value attribute specifies the URL that's processed by the <c:url> action. The context attribute lets you specify a foreign context. Like <c:import> and <c:redirect>, if you specify the context attribute for <c:url>, you must also specify a context-relative URL, with the value attribute, that points to a resource in that foreign context. By default, <c:url> sends the processed URL to the current JspWriter , but you can store that URL in a scoped variable instead if you specify the var attribute and, optionally , the scope attribute.

Like <c:import> and <c:redirect>, you can specify request parameters that are encoded in the URL that <c:url> processes with nested <c:param> actions. You can do that with the following syntax:

  <c:url value [context] [var] [scope]>   <c:param> actions   </c:url>  

If you specify a context-relative or page-relative URL for the value attribute, <c:url> will prepend the context path of the Web application to the URL; for example, consider the following use of <c:url>:

 <c:url value='/test_1.jsp'/> 

If the context path of the Web application is / core -jstl/webapp , <c:url> will produce the following URL: /core-jstl/webapp/test_1.jsp , not taking into account possible URL rewriting. Because of this feature, you must not use <c:url> in conjunction with <c:import> or <c:redirect> for relative URLs because those actions also prepend the context path to relative URLs before passing the URL to the request dispatcher. For example, consider the following code:

 <c:import url='/test_1.jsp'/> 

The preceding code fragment is not equivalent to the following code fragment:

 <%-- WARNING: this code fragment will throw an exception --%>  <c:url value='/test_1.jsp' var='processedURL'/> <c:import url='${processedURL}'/> 

The preceding code fragment will throw an exception because both <c:url> and <c:import> will try to prepend the context path to the relative URL. URLs processed by <c:url> actions are meant to be sent directly to the browser; for example:

 <c:url value='/test_1.jsp' var='processedURL'/>  <a href='<c:out value="${processedURL}"/>'>Click Here</a> 

The preceding code fragment creates a URL with <c:url> and uses the resulting URL with the HTML anchor element, which is how <c:url> is meant to be used.

The examples discussed in "Accessing External Resources" on page 210 and "Accessing Resources in Foreign Contexts" on page 215 both use <c:url> to process URLs that are sent directly to the browser.

Core Warning

graphics/openglobe.gif

Don ' t use <c:url> to encode relative URLs used by <c:import> or <c:redirect>.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net