在javascript函数中使用jsp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19707809/
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
using jsp in javascript function
提问by Colinch
I've searched for this but can't find anything. Please correct my question if it's incorrect english.
我已经搜索过这个,但找不到任何东西。如果英语不正确,请更正我的问题。
This is my code: EDIT:The code is within my .jsp file!
这是我的代码: 编辑:代码在我的 .jsp 文件中!
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Beoordeling', 'Stage Opleider', 'Student'],
['1', '1', '4'],
<% ArrayList < Stelling > alleStellingenLijst2 = new ArrayList < Stelling > ();
alleStellingenLijst2 = (ArrayList < Stelling > ) request.getAttribute("stellingen");
for (Stelling s: alleStellingenLijst2) {
out.println("['1', '" + s.getDeStelling() + "' , '" + s.getDeWaarde() + "'],");
} %> ]);
var options = {
title: 'Laatste competenties',
hAxis: {
title: 'Score',
titleTextStyle: {
color: 'green'
}
},
vAxis: {
title: 'Beoordeling nummer',
titleTextStyle: {
color: 'green'
}
},
// Allow multiple simultaneous selections.
selectionMode: 'multiple',
colors: ['#BEF781', 'green']
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
For some reason, it wont execute the code between the <% %> (from the jsp).
出于某种原因,它不会执行 <% %> (来自jsp)之间的代码。
This page online: http://project-omega.appspot.com/grafieken.jspThe google app engine logs say the error is on the last line of my page. It's a nullpointerexception.
此页面在线:http: //project-omega.appspot.com/grafieken.jsp谷歌应用引擎日志说错误在我页面的最后一行。这是一个空指针异常。
I have no idea what it means and I really hope someone can help me.
我不知道这意味着什么,我真的希望有人能帮助我。
Thanks a lot and sorry for my english.
非常感谢,对不起我的英语。
EDITThe rendered output looks as follows
编辑呈现的输出如下所示
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Beoordeling', 'Stage Opleider', 'Student'],
for (Stelling s : alleStellingenLijst2) {
out.println("['1', '" + s.getDeStelling() + "' , '" + s.getDeWaarde() + "'],");
}
]);
NEW CODE:
新代码:
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Beoordeling', 'Stage Opleider', 'Student'],
['1', 1, 4],
<%
ArrayList<Stelling> alleStellingenLijst2 =(ArrayList<Stelling>) getServletContext().getAttribute("stellingen");
for (Stelling s : alleStellingenLijst2) {
out.println("['1', " + s.getDeStelling() + " , " + s.getDeWaarde() + "],");
}
%>
['2', 2, 2]
]);
采纳答案by Eel Lee
These are JSP markups, you cannot use them in JavaScript!
这些是 JSP 标记,您不能在 JavaScript 中使用它们!
That's because JSP files are compiled to the .java classes during compilation, and JavaScript is executed on the client side.
这是因为 JSP 文件在编译过程中被编译为 .java 类,而 JavaScript 在客户端执行。
You could do the opposite - generate a JavaScript code in the JSP file, that way you could pass some data you want to the JS variables.
你可以做相反的事情——在 JSP 文件中生成一段 JavaScript 代码,这样你就可以将一些你想要的数据传递给 JS 变量。
回答by rzymek
I suppose you haven't set the stellingen
request attribute.
You usually set the request attributes in a servlet, before forwarding the request to jsp:
我想你还没有设置stellingen
请求属性。在将请求转发到 jsp 之前,您通常在 servlet 中设置请求属性:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
ArrayList<Stelling> list = ...;
req.setAttribute("stellingen", list);
req.getRequestDispatcher("/grafieken.jsp").forward(req, resp);
}
Also make sure the attribute is set in the JSP code:
还要确保在 JSP 代码中设置了该属性:
<%
List<Stelling> stellingen = (List<Stelling>) getServletContext().getAttribute("stellingen");
if(stellingen == null) {
out.println("stellingen attribute not set!");
}else{
for (Stelling s : stellingen) {
out.println("['1', " + s.getDeStelling() + " , " + s.getDeWaarde() + "],");
}
}
%>