简体   繁体   English

在.jsp文件中使用javascript

[英]using javascript in .jsp file

So in eclipse when you generate a .jsp file it automatically includes the following top line: 因此,在eclipse中,当您生成.jsp文件时,它会自动包含以下第一行:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

Is there a way to also include javascript code so that I can have some of the file written in java and some written in javascript? 有没有办法也包含javascript代码,以便我可以用Java编写一些文件,而用javascript写一些文件呢?

Usually I include my JavaScript in JSP like this: 通常,我将JavaScript包含在JSP中是这样的:

<script type="text/javascript" src="js/script.js"></script>

This way I can minimize the js code and also cache it differently than JSP files in Tomcat. 这样,我可以最小化js代码,并且还可以与Tomcat中的JSP文件不同地缓存它。

The answer is Yes, you can add the js in jsp. 答案是可以的,您可以在jsp中添加js。 And this also has benefit. 这也有好处。 It helps you to use the values stored in request and session, because the js code which contains the jsp stuff in jsp will be compiled while the *.js file will not. 它可以帮助您使用存储在请求和会话中的值,因为包含jsp中jsp内容的js代码将被编译,而* .js文件不会被编译。 And also you should keep in mind that you'd better put the js code in .js file not in jsp, so the browser doesn't need to load it repeatly and code seems more clear, also good for debug. 另外,您还应该记住,最好将js代码放在.js文件中而不是jsp中,因此浏览器不需要重复加载它,并且代码看起来更清晰,也很适合调试。 Another answer has give you the example code to import js file, so I just ignore it. 另一个答案已为您提供了导入js文件的示例代码,因此我将其忽略。

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

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