Spring PetClinic <spring:url ....../> encoding for very url

jakarta-ee, java, spring, spring-mvc, url

Solution

Yes, it's a good practice. That tag is a Spring JSP tag for creating URLs with enhancements for JSTL `c:url`. `<c:url>` tag is used to create a url and it is helpful in the case when cookies is turned off by the client, and you would be required to rewrite URLs that will be returned from a jsp page. The rewritten URL will encode the session ID if necessary to provide persistent sessions.

For example, your URL will be displayed as:

<c:url value="a.jsp">

/context/a.jsp // when cookies is enabled
/context/a.jsp;jsessionid=B01F432.... // when cookie is disabled

This way, the servlet container can track the user requests. Another important thing is that `c:url` will prefix the context root, so you don't need to write your root context prefix everywhere.

Like I said, `<spring:url>` contains some enhancements over JSTL like encoded URI template variables for example.

<spring:url value="/url/path/{variableName}">
   <spring:param name="variableName" value="more than JSTL c:url" />
 </spring:url>

Results in: /currentApplicationContext/url/path/more%20than%20JSTL%20c%3Aurl

See more details in here: http://docs.spring.io/spring/docs/3.1.4.RELEASE/javadoc-api/org/springframework/web/servlet/tags/UrlTag.html

Problem

I am implementing the Spring Pet Clinic Sample project given here Implementation here In the home page all the urls are encoded by `<spring:url />` tag. I just want to confirm that is it a good programming practice to encode the links, even the simple navigation urls by the spring:url tag or is it done for a specific reason?.

Original source