开发者

Download a file from fall back location

We are g开发者_Python百科enerating links to CSS and JavaScript files from our JSP Page. These CSS and JS files are organized in Locale specific folders. Now we need to download the file from the fall back place in case the requested file is not available in the specified location (the similar approach that is used to load the Resource Bundles-if the locale specific Bundle is not available, then the default one is used). Could you please suggest an approach to implement this?


You could make a servlet to handle fetching the locale specific files. Map the servlet to a URL like "/yourApp/localeFile" and use it in your JSPs like:

<script src="/yourApp/localeFile?name=something.js&locale=en_US">

The servlet would read and return the contents of the locale's file if it exists. If the file is not found, it would return the content of the fallback locale's file.

Alternately, maybe you could do something like have the server's 404 handler for the locale directory return the fallback file.


If you use jsp-api-2.0 you can write tag file, otherwise you can use concept only.

create /WEB-INF/tags/file.tag

<%@tag import="java.io.File"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="g" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

<%@ attribute name="path" required="true" type="java.lang.String" rtexprvalue="true"%>
<%@ attribute name="file" required="true" type="java.lang.String" rtexprvalue="true"%>

<%
    final PageContext pc = (PageContext) getJspContext();
    final String lang = request.getLocale().getLanguage();

    final String prefered = pc.getServletContext().getRealPath(path + "/" + lang + "/" + file);
    final File file = new File(prefered);
    if (file.exists()) {
        pc.setAttribute("fileToUse", path + "/" + lang + "/" + file);
    } else {
        pc.setAttribute("fileToUse", path + "/" + file);
    }
%>
${fileToUse}

in your jsp add

<%@ taglib tagdir="/WEB-INF/tags" prefix="g" %>

then you can use it as

<script type="text/javascript" src='<g:file file="myfile1.js" path="/js" />'></script>
<script type="text/javascript" src='<g:file file="myfile2.js" path="/js" />'></script>

assuming that you have webapp/js, webapp/js/en ... folders.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜