将集合填充到 JSP 页面时出现 404 错误
我试图将集合(项目集合)从 servlet 填充到我的 jsp 页面。
在我的 servlet 代码中,我将项目存储在集合中:
String itemsJson = new Gson().toJson(items);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(itemsJson);
在我的 jsp 页面中,我尝试将项目填充到 jquery 可调整大小的框中。代码是如下:
$(document).ready(function() {
$("#resizable").resizable();
$.getJSON('resizable', function(itemsJson) {
var $resizable = $('#resizable');
$.each(itemsJson, function(index, itemcatalog) {
$('<option>').text(itemcatalog.item).appendTo($resizable);
});
});
});
<div class="demo">
<div id="resizable" class="ui-widget-content">
<h3 class="ui-widget-header">Item List</h3>
<option></option>
I was trying to populate collection(collection of items) from servlets to my jsp page..
In my servlet code I store the items in collection:
String itemsJson = new Gson().toJson(items);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(itemsJson);
In my jsp page I am tryin to populate the items onto a jquery resizable box.The code is as follows:
$(document).ready(function() {
$("#resizable").resizable();
$.getJSON('resizable', function(itemsJson) {
var $resizable = $('#resizable');
$.each(itemsJson, function(index, itemcatalog) {
$('<option>').text(itemcatalog.item).appendTo($resizable);
});
});
});
<div class="demo">
<div id="resizable" class="ui-widget-content">
<h3 class="ui-widget-header">Item List</h3>
<option></option>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么这个 URL 就完全错误了。您在
$.getJSON()
中使用相对 URL想象一下,此代码被放置在由以下 URL 打开的 JSP 文件中
,那么您需要确保该 servlet 在
JavaScript 上可用/jQuery 会将相对 URL 映射到文档的基本 URL。
或者,当 servlet 实际上 监听
(通过直接在浏览器地址栏中打开其地址进行测试)
时,您需要更改
$.getJSON()< /code> URL 如下
或(相对于域)
或(仅在 JSP 内部有效)
或将 JSP 文件移动到
或者如果 URL 实际上是正确的,那么它仅意味着 servlet 无法启动。读取服务器日志中的异常/堆栈跟踪并相应地修复代码。或者 servlet 可能根本没有映射到 URL。
Then the URL is plain wrong. You're using a relative URL in
$.getJSON()
Imagine that this code is been placed in a JSP file which is opened by the following URL
then you need to ensure that the servlet is available on
JavaScript/jQuery will namely map relative URLs to the document's base URL.
Or when the servlet is actually listening on
(test it by opening its address straight in browser address bar)
then you need to change the
$.getJSON()
URL as followsor (domain-relative)
or (works only if this is inside JSP)
or to move the JSP file to
Or if the URL is actually correct, then it simply means that the servlet failed to startup. Read the server logs for the exception/stacktrace and fix the code accordingly. Or maybe the servlet is not mapped on an URL at all.