在servlet中:
List<myItem> yourObjectToReturn = search.parserContent();
request.setAttribute("yourObjectToReturn",yourObjectToReturn);
数组yourObjectToReturn包含3个变量(id,txtfile,sentence),您可以从中看到 myItem类
public class myItem{
String sentence;
int id;
String txtfile;
// public myItem(){
// }
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getTxtfile(){
return txtfile;
}
public void setTxtfile(String txtfile){
this.txtfile = txtfile;
}
public String getSentence(){
return sentence;
}
public void setSentence(String sentence){
this.sentence = sentence;
}
}
如何在JSP中单独显示id,txtfile,句子?如何将arraylist从servlet传递给JSP。
JSP:如何编辑我的JSP。我的JSP错误了:
类型安全:从objectto arraylist
取消选中
<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% List<myItem> myList = (ArrayList<myItem>) request.getAttribute("yourObjectToReturn"); %>
The search Result SENTENCE IS: <%=myList %> --%>
</body>
</html>
答案 0 :(得分:2)
不要在jsp页面中使用scriptlet。 通过以下方式包含JSTL标准taglib:
<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
然后在JSP中使用迭代标记:
<c:forEach items="${requestScope.yourObjectToReturn}" var="current">
<c:if test="${current.sentence== 'secret' }">
<h1>seeeeeeeeeecret revealed</h1>
</c:if>
</c:forEach>
其中:
${requestScope.yourObjectToReturn} is your collection object.
并且(在每次迭代期间):
${current} is your actual element.
有关详细信息,请参阅http://docs.oracle.com/javaee/5/tutorial/doc/bnahq.html
为了避免奇怪的错误:不要忘记导入myItem
类(真的应该是MyItem
,你......)
编辑:在深入研究JSTL之前,我建议您阅读this other question。特别关注选定的答案,它提供了很好的见解。
答案 1 :(得分:0)
要从Id
中检索Txtfile
和List
,您需要使用例如for
循环进行迭代:
...
for (int i=0; i < myList.size(); i++) {
%>
<%=myList.get(i).getId()%>
<%=myList.get(i).getTxtfile())%>
<%}%>
答案 2 :(得分:0)
您可以按如下方式一起使用for循环和html:
<%
@SupressWarnings("unchecked")
List<mtItem> myList
for (MyItem myitem : myList)
{
%>
The search result is <%=myitem%>
<%
}
%>