我尝试将servlet中的列表发送到jsp页面.这是servlet代码:
I tried to send a list from my servlet to a jsp page. This is the servlet code:
Query q = new Query("post").addSort("time", SortDirection.DESCENDING); PreparedQuery pq = datastore.prepare(q); QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions); for (Entity entity : results) { System.out.println(entity.getProperty ("content")); System.out.println(entity.getProperty ("time")); } req.setAttribute("postList",results); req.getRequestDispatcher("/tublr.jsp").forward(req, resp);jsp代码:
<% QueryResultList<Entity> result = request.getAttribute("postList"); for (Entity entity : results) { <b> IT WORRRKKKK !!! </b> <br> } %>但是我得到一个错误
我添加了
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.List,com.google.appengine.api.datastore.Query.SortDirection,com.google.appengine.api.datastore.*" %>现在我收到一个新错误
jsp文件:/tublr.jsp中的第37行出现错误 不匹配:无法从Object转换为QueryResultList ..... 造成原因:
An error occurred at line: 37 in the jsp file: /tublr.jsp Type mismatch: cannot convert from Object to QueryResultList ..... Caused by:
org.apache.jasper.JasperException:无法为JSP编译类:
org.apache.jasper.JasperException: Unable to compile class for JSP:
我是在学校里做的,我们现在必须这样,我们必须在jsp页面中使用java.
I m do it for the school and we have to di it like this now , we have to use java in the jsp page.
推荐答案1)您需要在JSP的顶部添加import语句.
1) You need to add import statements at top of the JSP.
示例:
<%@ page import="java.util.List" %>2)将Java代码直接嵌入JSP中是不的好习惯
2) It is NOT good practice to have Java code directly embedded in JSP
在 SO Wiki
更多推荐
将数据从servlet发送到jsp
发布评论