nKognito
nKognito

Reputation: 6363

Jstl: using scriptlet inside custom tags

I use custom tags in order to create some kind master page (template). The construction is following:

// Template
<%@ tag description="master" pageEncoding="UTF-8"%>
<%@ attribute name="js" fragment="true" %>
<!doctype html>
<html>
<head>
    <jsp:invoke fragment="js" />
</head>
<body>
</html>

// Page
<%@ page pageEncoding="UTF-8"%>   
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %> 
<t:master>
    <jsp:attribute name="js">
        <script type="text/javascript" src="<spring:url value="/javascript/administration/customers.js" />"></script>
    </jsp:attribute>
</t:master>

It works fine until I try to use the common solution for disabling browser-side caching of javascript by adding random string to the end of js filename:

<script type="text/javascript" src="<spring:url value="/javascript/administration/customers.js" />?<%= new java.util.Date().getTime() %>"></script>

It fails with

Scripting elements ( <%!, <jsp:declaration, <%=, <jsp:expression, <%, <jsp:scriptlet ) are disallowed here.

exception

How can I implement such solution? Thank you

Upvotes: 1

Views: 2152

Answers (1)

Gunslinger
Gunslinger

Reputation: 747

A wild suggestion :-)

Use a usebean to generate a new Date object in the request scope. Each request would then result in a new Date object creation. This will be reused through the whole request.

 <jsp:useBean id="uniqueDate" class="java.util.Date" scope="request"/>  

Then call the getTime() method on the date object (as suggested in comment).

${uniqueDate.time}

Upvotes: 3

Related Questions