0

我尝试使用 jquery 将数据解析到我的 jsp 站点中。如果我只想解析一个对象,一切正常,但如果我想解析更多,jquery 什么也不做。

这有效:

@RequestScoped
@ApplicationPath("/webservices")
@Path("/poster")
public class Poster extends Application implements Serializable {
    private static final long serialVersionUID = 1L;

    public Poster() {}

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Image> getPosters() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("SPJUnit");
        EntityManager em = factory.createEntityManager();

        TypedQuery<Image> query = em.createQuery("SELECT I FROM Image I WHERE I.imageID = 1", Image.class);

        return query.getResultList();
    }
}

使用这个jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-script-type" content="text/javascript" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="content-language" content="de" />

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

<title>Poster</title>

<script>
    $.ajax({
        dataType: "json",
        url: "http://localhost:8080/MyApp/webservices/poster/",
        success: function(data) {
             $.each(data, function(index, item) {
                 $("#poster").append("<img src=" + item.image + "/><br></br>");</br>");
             });
         }
    });
</script>
</head>

<body>
    <div id="wrapper">
        <h2 id="title">Poster</h2>
        <div id="poster"></div>

    </div>
</body>
</html>

如果我更改我的选择语句以选择所有图像:

TypedQuery<Image> query = em.createQuery("SELECT I FROM Image I", Image.class);

我的jsp上没有输出。选择返回 1200 张图像。这对jquery来说太多了还是这里发生了什么?

如果我打开我的 Google Chrome 开发者工具,它会给我一个错误:

Failed to load resource: the server responded with a status of 500 (Internal Server Error) 

他无法获得图像的来源。他试图得到http://localhost:8080/MyApp/webservices/undefined

这是服务器日志:

hasResource perm: ("javax.security.jacc.WebResourcePermission" "/jsp/undefined/index.jspx" "GET")
Warnung: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
com.sun.faces.context.FacesFileNotFoundException: /jsp/undefined/index.xhtml Not Found in ExternalContext as a Resource
at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:232)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:273)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:209)
at com.sun.faces.application.view.ViewMetadataImpl.createMetadataView(ViewMetadataImpl.java:114)
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:233)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
4

1 回答 1

0

找到了解决方案。

循环是错误的!我做了:

$.each(data, function(index, item) {
    $("#poster").append("<img src=" + item.image + "/><br></br>");</br>");
});

但我必须这样做:

$.each(data, function() {
    $.each(this, function(index, item) {
        $("#poster").append("<img src=" + item.image + "/>");
    });
});
于 2013-07-02T17:33:10.340 回答