Friday, February 15, 2013

Handling Context root relative URLs problem using Spring's

 While developing web applications the common problem is to reference the static resources like js, stylesheets,images in JSPs from the relative URLs and submitting forms. To avoid this problem we should always use <spring:url> tag, as <spring:url> tag resolves the path from context root. So you can always give the path for static resources from context root irrespective of current URL.
Usage of <spring:url> tag
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<spring:url value="/images" var="images_url" />

<img src="${images_url}/sample_logo.gif">

<spring:url value="/saveApplication" var="save_form" />

<form action="${save_form}" method ="post">

<spring:url value="/application/link1" var="link1_url" />

<a href="${link1_url}">Demo Link</a>

<spring:url value="/resources/js " var="js_url" />

<script type="text/javascript" src="${js_url}/sortable.js"></script>

<spring:url value="/resources/css " var="css_url" />

<link type="text/css" rel="stylesheet" href="${css_url}/style.css" 
media="screen" />

No comments:

Post a Comment