我正在学习如何使用Java和Google App Engine进行编码。我想要检索一些我上传到Google App数据库并显示它们的图像。我的代码编译,但图像不会显示在浏览器中。我只看到一个小广场。
我怀疑该错误是在viewimage.jsp文件之前。有人可以看看我的一些jsp文件并给我一些建议吗?非常感谢您的帮助。谢谢你!
viewimage.jsp
<%@ page import="javax.jdo.*" %>
<%@ page import="com.*" %>
<%
PersistenceManager pm = PMF.getPMF().getPersistenceManager();
try {
String itemId = request.getParameter("item");
Item item = (Item)pm.getObjectById(Item.class, Long.parseLong(itemId));
byte[] photo = item.getPhoto(item);
response.setContentType("image/jpeg");
response.getOutputStream().write(photo);
} catch (Exception cannotLoad) {
cannotLoad.printStackTrace();
} finally {
pm.close();
}
%>
view.jsp的
<!DOCTYPE html>
<html><head>
<title>CS496 Homework 2 - Display Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="jquery-1.10.2.min.js"></script>
<script>
var MOBILE = navigator.userAgent.match(
/Android|BlackBerry|iPhone|iPad|iPod|IEMobile/i) ? true : false;
if (MOBILE) {
document.write("<link rel=stylesheet href='http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css'>");
document.write("<link rel=stylesheet href='stylemobile.css'>");
document.write('<s'+'cript src="jquery.mobile-1.3.2.min.js"></scr'+'ipt>');
} else
document.write("<link rel=stylesheet href='styledesktop.css'>");
</script>
</head><body>
<div data-role="page">
<div data-role="header">
<h1>CS496 Homework 2</h1>
</div>
<div data-role="content" class="photolist">
<ul data-role="listview" data-inset="true" id="allitems"></ul>
</div>
</div>
<div data-role="page" id="detailPage">
<div data-role="header">
<h1 id="detailTitle"></h1>
</div>
<div data-role="content" id="detailContent" class="photoViewer">
<img src="" id="detailImage">
</div>
</div>
<script>
function addItemMobile(itemId, school) {
var anchor = $("<a/>").text(school).attr('href', '#detailPage').click(function() {
$("#detailTitle").text(school);
$("#detailImage").attr('src', '');
$("#detailImage").attr('src', 'viewimage.jsp?item='+itemId);
});
var li = $("<li/>").append(anchor);
$("#allitems").append(li);
}
function addItemDesktop(itemId, school) {
var loc = $("#allitems");
loc.append($("<h2 />").text(school));
loc.append($("<div />").append($("<img />").attr('src', 'viewimage.jsp?item='+itemId)));
}
var addItem = MOBILE ? addItemMobile : addItemDesktop;
$.ajax({
type:"GET",
dataType:"json",
url:"viewapi.jsp",
success:function(items) {
for (var i = 0; i < items.length; i++) {
var item = items[i];
addItem(item.id, item.school);
}
$("#allitems").listview('refresh');
},
error:function(a,b,c) {
alert("ERROR: "+a);
}
});
</script>
</body></html>
viewapi.jsp
<%@ page import="java.util.*" %>
<%@ page import="javax.jdo.*" %>
<%@ page import="org.json.simple.*" %>
<%@ page import="cs496hwk2.*" %>
<%
PersistenceManager pm = PMF.getPMF().getPersistenceManager();
try {
List<Item> items = Item.loadItems(null, pm);
JSONArray array = new JSONArray();
for (Item item : items) {
JSONObject object = new JSONObject();
object.put("id", item.getID());
object.put("school", item.getSchool());
array.add(object);
}
out.write(array.toString());
} finally {
pm.close();
}
%>