foreach循环在jsp中不显示任何输出 [英] foreach loop not displaying any output in jsp
问题描述
我正在jsp中运行第一个foreach循环.我不知道问题是什么.它也没有显示任何错误.
I am running a very first foreach loop in jsp. I dont know what the problem is. Its not showing any errors as well.
这是我的Java代码:
This is my java code:
package pack1;
import java.util.ArrayList;
public class jstlClass
{
String emp_name;
String emp_id;
String emp_dept;
public String getEmp_name()
{
return emp_name;
}
public void setEmp_name(String emp_name)
{
this.emp_name = emp_name;
}
public String getEmp_id()
{
return emp_id;
}
public void setEmp_id(String emp_id)
{
this.emp_id = emp_id;
}
public String getEmp_dept()
{
return emp_dept;
}
public void setEmp_dept(String emp_dept)
{
this.emp_dept = emp_dept;
}
public static void main(String[] gs)
{
ArrayList li=new ArrayList();
jstlClass emp=new jstlClass();
emp.setEmp_id("20");
emp.setEmp_name("vishnu");
emp.setEmp_dept("it");
li.add(emp);
jstlClass j=new jstlClass();
j.setEmp_id("21");
j.setEmp_name("prem");
j.setEmp_dept("csc");
li.add(j);
}
}
这是我的jsp代码:
<html>
</head>
<body>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="emp1" class="pack1.jstlClass" scope="session"/>
<%@ page import="java.util.*" %>
<%
<c:forEach var="li" items="${sessionScope.li}">
<c:out value="${li.emp_id}"/>
<c:out value="${li.emp_name}"/>
<c:out value="${li.emp_dept}"/>
</c:forEach>
%>
</body>
</html>
我已经尝试了很长时间,但仍然显示相同的输出.我正在使用Eclipse和Apache Tomcat服务器.我什至尝试在Google Chrome浏览器中运行它,但没有任何更改.我已经在其中放置了"Hello world"并显示了它,但是没有进入foreach循环. 这是我的第一个foreach循环程序,我完全不知道出了什么问题. 请帮忙!
I have tried for so long but still it shows the same output. I am using Eclipse and Apache Tomcat server. I even tried running it in google chrome server, but no changes. I have put that "Hello world" in there and its displaying that, but not entering the foreach loop. This is my first foreach loop program and i have absolutely no idea what is going wrong. Help please!
推荐答案
这根本不对... 在执行此操作之前,您必须阅读servlet jsp ...
This is not at all right... You are doing it worng... You will have to read servlet jsp before doing this...
为您的项目创建一个servlet,并将数据从servlet传递到jsp,然后只有jsp可以从那里访问数据
for your project create a servlet and pass data from servlet to jsp and then only jsp can access data from there
首先尝试servlet jsp示例. 示例servlet
Try servlet jsp examples first.. Example servlet
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
public Login() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//right your code here to get data from jstlClass and pass it to the jsp in request
request.setAttribute("","");
request.getRequestDispatcher("/FirstJSP.jsp").forward(request, response);
}
protected void doget(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
}
}
然后您可以在那里访问参数
and then you can access the parameters there
尝试这个例子 http://www.java-samples.com/showtutorial.php?tutorialid=552
Try this example http://www.java-samples.com/showtutorial.php?tutorialid=552
这篇关于foreach循环在jsp中不显示任何输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!