javascript 如何在javascript中嵌入JSP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23087096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to embed JSP in javascript?
提问by i3wangyi
I'm working on the project that requires reading database in JSP, but the data is used by javascript to render the google map(the map points). I don't know how to access the NoSQL database by javascript, so I'm thinking about embed JSP in javascript to access the data and as a way to feed them to javascript.
我正在研究需要在 JSP 中读取数据库的项目,但 javascript 使用数据来呈现谷歌地图(地图点)。我不知道如何通过 javascript 访问 NoSQL 数据库,所以我正在考虑在 javascript 中嵌入 JSP 来访问数据,并将它们作为一种将它们提供给 javascript 的方式。
I've searched a lot about these features, I'd like to have code like:
我已经搜索了很多关于这些功能的信息,我想要这样的代码:
var a = <%=data %>
How could I control the script(since it's .js), and index.jsp ?
我如何控制脚本(因为它是 .js)和 index.jsp ?
Thanks
谢谢
回答by Luiggi Mendoza
Java code runs on server, this means, it runs on your application server and helps to render the view (in this case, the JSP). JavaScript runs on client side, this means, it runs on the client browser (Internet Explorer [ugh], Firefox, Chrome, and on...). So, based from your current code:
Java 代码在服务器上运行,这意味着它在您的应用程序服务器上运行并有助于呈现视图(在本例中为 JSP)。JavaScript 在客户端运行,这意味着它在客户端浏览器(Internet Explorer [呃]、Firefox、Chrome 等)上运行。因此,根据您当前的代码:
var a = <%=data %>;
Assuming data
is a String
and has a value of "Hello World"
, the generated HTML/JS would be:
假设data
是 aString
并且值为"Hello World"
,则生成的 HTML/JS 将是:
var a = Hello World;
Which will produce an error. So, you would need to quote the variable:
这将产生错误。因此,您需要引用变量:
var a = '<%=data %>';
Now this will produce:
现在这将产生:
var a = 'Hello World';
For more complex communication, like passing a list or a complex structure or a list of complex structures from server to client, it would be better using a common format to exchange data like JSON. You can marshall the data to JSON string from server (preferably in a Servlet) and then pass it to JavaScript side easily. For example, using Hymanson Library for Java:
对于更复杂的通信,例如从服务器向客户端传递列表或复杂结构或复杂结构列表,最好使用通用格式来交换数据,例如 JSON。您可以将数据从服务器(最好在 Servlet 中)编组为 JSON 字符串,然后轻松地将其传递给 JavaScript 端。例如,使用 Java 的 Hymanson 库:
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
List<ComplexObject> complexObjectList = ... //get your data
request.setAttribute("complexObjectList", OBJECT_MAPPER.writeValueAsString(complexObjectList));
//forward to the desired view...
request.getRequestDispatcher("/WEB-INF/theView.jsp").forward(request, response);
}
}
Then in your JSP (using Expression Languagebecause you should avoid using scriptlets):
然后在您的 JSP 中(使用表达式语言,因为您应该避免使用 scriptlets):
<script type="text/javascript">
var complexObjectList = JSON.parse('${complexObjectList}');
//play with your new object in JavaScript side...
</script>