简体   繁体   English

如何使用jsp和javascript为链接和脚本标记创建动态源(src)

[英]How can I create dynamic sources (src) for link and script tags using jsp and javascript

I have a jsp file that passes a parameter to another jsp file. 我有一个jsp文件将参数传递给另一个jsp文件。 This parameter is of type string and takes the value of a folder name in the web server path (ie http://myserver.com/Page/Folder_Name ). 此参数的类型为字符串,并在Web服务器路径中获取文件夹名称的值(即http://myserver.com/Page/Folder_Name )。

In the second jsp file, I have quite a few js and css files I'm attaching using script and link tags, respectively. 在第二个jsp文件中,我分别使用脚本和链接标记附加了很多js和css文件。 I need to make their paths relative to the parameter passed. 我需要使他们的路径相对于传递的参数。 My thought-process is construct dynamic source (src) paths using jsp tags and javascript. 我的思维过程是使用jsp标签和javascript构造动态源(src)路径。

<head>
  <link rel="stylesheet" type="text/css" href="Folder_Name/css/style.css" />
  <script type="text/javascript" src="Folder_Name/js/script.js"></script>
</head>

Except Folder_Name will be dynamic, utilizing JSP somehow. 除了Folder_Name,它将是动态的,以某种方式利用JSP。 At least, that's my mindset. 至少,这是我的心态。
The question is how may I accomplish this? 问题是我怎么能做到这一点? I'm open to other suggestions, with the initial condition of said parameter is passed to the second jsp page. 我对其他建议持开放态度,将所述参数的初始条件传递给第二个jsp页面。

Thank you. 谢谢。

Why do this with JavaScript? 为什么用JavaScript? If it's already a JSP page, you're already doing server-side processing, so it seems like you'd want to use Java. 如果它已经是JSP页面,那么您已经在进行服务器端处理,因此您似乎想要使用Java。 Eg, once you have the folder name in a variable (say, folderName ), just do this to output the links: 例如,一旦您在变量(例如, folderName )中有文件夹名称,只需执行此操作即可输出链接:

<head>
  <link rel="stylesheet" type="text/css" href="<%=folderName%>/css/style.css" />
  <script type="text/javascript" src="<%=folderName%>/js/script.js"></script>
</head>

(Note the <%=folderName%> bit, which runs on the server and outputs the value of the server-side folderName variable.) Or if you're using a container that supports the JSP expression language , use ${folderName} instead: (注意<%=folderName%>位,它在服务器上运行并输出服务器端folderName变量的值。)或者如果您使用的是支持JSP表达式语言的容器,请使用${folderName}代替:

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

You certainly could use JavaScript for the task, if you want, but I'm not sure it makes sense. 如果你愿意,你当然可以使用JavaScript来完成任务,但我不确定它是否有意义。 You could use Rhino (JavaScript for the JVM) on the server, of course. 当然,您可以在服务器上使用Rhino(JVM的JavaScript)。 If you're talking client-side, you'd basically have to echo the folder name into a place where the client-side JavaScript can find it (so, output it to a dynamically-generated global variable — <script>var folderName = '<%=folderName%>';</script> for example — or to a hidden input , or whatever), and then have your JavaScript add the necessary script tags via document.createElement , set their src , and append them to the head or body (doesn't matter where), but it seems a very round-about way to do it if you're already doing server-side processing. 如果您正在谈论客户端,您基本上必须将文件夹名称回显到客户端JavaScript可以找到它的位置(因此,将其输出到动态生成的全局变量 - <script>var folderName = '<%=folderName%>';</script>例如 - 或隐藏input或其他),然后让您的JavaScript通过document.createElement添加必要的脚本标记,设置它们的src ,并将它们附加到headbody (无所谓),但如果您已经在进行服务器端处理,那么这似乎是一种非常圆润的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM