English 中文(简体)
如何动态进口 j和 files
原标题:How to dynamically import javascript and css files

i • 根据某些条件进口特定物品或javascript文档。


protected void doPost(...)
{
   if(condition)
   {
     //import some javascript or css file here
   }

}

I need this behavior since i have too many files to import and the files name may vary according to the condition. Is it possible?

最佳回答

是的。

boolean condition = evaluateItSomehow();
request.setAttribute("condition", condition);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/“rel=“nofollow noreferer”

<head>
    <c:if test="${condition}">
        <link rel="stylesheet" type="text/css" href="style.css">
        <script type="text/javascript" src="script.js"></script>
    </c:if>
    ...
</head>

<>Update:,因为你似乎有不止一份这方面的档案,你甚至可以通过仅仅确定所希望的档案名称(或预先确定,或甚至全名),使其更加灵活:

String suffix = evaluateItSomehow();
request.setAttribute("suffix", suffix);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

以及

<head>
    <link rel="stylesheet" type="text/css" href="style_${suffix}.css">
    <script type="text/javascript" src="script_${suffix}.js"></script>
    ...
</head>

If you set suffix to for example "foo", this will load style_foo.css 以及 script_foo.js. I think this gives enough new insights.

问题回答

你们是否试图将 Java字和特别安全带进来? 我会从客户方面这样做。 我指的是possible,以明确写明<script...> or <link...>。 更好的办法是向客户发回一些东西,告诉客户增加一个风格表格或 Java字。

之后,你可以积极补充:

如果需要以动态方式做到这一点,那就更加容易。 只是打上旗帜,在你的共同财产调查或伙伴关系中,检查国旗状况。 在有条件的标签内,你将加入《社会保障法》和《 Java》。 然而,我从你看来是前者。

function loadjscssfile(filename, filetype) {
            if (filetype == "js") { //if filename is a external JavaScript file
               // alert( called );
                var fileref = document.createElement( script )
                fileref.setAttribute("type", "text/javascript")
                fileref.setAttribute("src", filename)
                alert( called );
            }
            else if (filetype == "css") { //if filename is an external CSS file
                var fileref = document.createElement("link")
                fileref.setAttribute("rel", "stylesheet")
                fileref.setAttribute("type", "text/css")
                fileref.setAttribute("href", filename)
            }
            if (typeof fileref != "undefined")
                document.getElementsByTagName("head")[0].appendChild(fileref)
        }

要求这种 j印功能,以动态方式装载 c和 j。 通过“档案名称”论点中的全称档案。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签