1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.List" %> 4 <%@ page import="entity.House" %> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 接力Servlet,让我JSP继续运行<br> 14 <table width="70%" border="0" cellspacing="1" cellpadding="1" align="center" bgcolor="#000099"> 15 <tr bgcolor="#FFFFFF"> 16 <td>编号</td> 17 <td>名字</td> 18 <td>地址</td> 19 </tr> 20 <c:forEach var="L" items="${houseList_lable}"> 21 <tr bgcolor="#FFFFFF"> 22 <td>${L.id}</td> 23 <td>${L.name}</td> 24 <td>${L.address}</td> 25 </tr> 26 </c:forEach> 27 28 29 </table> 30 </body> 31 </html>
第22、23、24中的值应该怎样改才能遍历houseList_lable,并输出相应的值?
<c:foreach var="" items="支持el表达式">
</c:foreach>
这个不就是遍历的标签吗
是的,但是我应该怎样才能使其出现在td中?我代码中的那个方法没有效果
@冥河守望: 你遍历的map集合还是list 我也是新手!!!
@吉吉的城: lsit集合
@吉吉的城: 我也是今天接触的JSTL,还蒙着呢。。。
@冥河守望: 方便把你的action贴上来看看嘛
@吉吉的城:
1 package action; 2 3 import java.io.IOException; 4 import java.util.List; 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import Service.propertyService; 11 import Service.propertyServiceImple; 12 import entity.property; 13 14 15 16 @WebServlet("/propertyServlet") 17 public class propertyServlet extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 21 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 22 propertyService propertyService = new propertyServiceImple(); 23 List<property> propertyList = propertyService.getAllproperty(); 24 25 request.setAttribute("house_label", propertyList); 26 27 request.getRequestDispatcher("test.jsp").forward(request,response); 28 } 29 30 31 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 32 33 doGet(request, response); 34 } 35 36 }
这就是了
@吉吉的城: 错了,是这个。。。
package action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.House;
import service.HouseService;
import service.HouseServiceImpl;
import java.util.List;
@WebServlet("/HouseAction_showAllHouses")
public class HouseAction_showAllHouses extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从jsp中把这2行代码移动过来
HouseService houseService = new HouseServiceImpl();
List<House> houseList = houseService.getAllHouses();
request.setAttribute("houseList_label", houseList);
//请求并不会中断,而且继续在服务端接力运行
//并不是发出一个新的请求
//所以对于客户端的地址栏,是不会发生变化
request.getRequestDispatcher("show.jsp").forward(request,response);
//通知客户端:你发出一个新的请求:show.jsp
//response.sendRedirect("show.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
@冥河守望: 你的items里面的值应该是属性名,而不是后面的,应该是这个house_label 而不是你写的那个
我这边网卡 ,刚看见你后来发的
@吉吉的城: 我并没有house_lable,这样子的结果它显示为空值
感觉没错,你看看是不是你dao层查数据的时候出问题了,可以在一些地方输出一下或者debug
@冥河守望: 我看错了 看的是你发的第一个action了网太卡,回你之后才看见的第二action
不好意思哈哈哈!!!!
@吉吉的城: 我的错,sorry
@冥河守望: 你检查你的dao层看看!!!感觉你的action和jsp都正常呀!也许我水平低没看出来呜呜呜!别鄙视我就行了 嘿嘿
@吉吉的城: O(∩_∩)O谢谢,我好了。。,
它不能使用request.getAttribute()方法,要用pageContext.getAttribute()方法:
1 package action; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import entity.House; 10 import service.HouseService; 11 import service.HouseServiceImpl; 12 import java.util.List; 13 14 15 @WebServlet("/HouseAction_showAllHouses") 16 public class HouseAction_showAllHouses extends HttpServlet { 17 private static final long serialVersionUID = 1L; 18 19 20 protected void doGet(HttpServletRequest pageContext, HttpServletResponse response) throws ServletException, IOException { 21 22 //从jsp中把这2行代码移动过来 23 HouseService houseService = new HouseServiceImpl(); 24 List<House> houseList = houseService.getAllHouses(); 25 26 pageContext.setAttribute("houseList_label", houseList); 27 //请求并不会中断,而且继续在服务端接力运行 28 //并不是发出一个新的请求 29 //所以对于客户端的地址栏,是不会发生变化 30 pageContext.getRequestDispatcher("show.jsp").forward(pageContext,response); 31 //通知客户端:你发出一个新的请求:show.jsp 32 //response.sendRedirect("show.jsp"); 33 34 } 35 36 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 37 doGet(request, response); 38 } 39 40 }
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.List" %> 4 <%@ page import="entity.House" %> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 <% 14 List<House> list1=(List)pageContext.getAttribute("houseList_label"); 15 %> 16 接力Servlet,让我JSP继续运行<br> 17 <table width="70%" border="0" cellspacing="1" cellpadding="1" align="center" bgcolor="#000099"> 18 <tr bgcolor="#FFFFFF"> 19 <td>编号</td> 20 <td>名字</td> 21 <td>地址</td> 22 </tr> 23 <c:forEach var="L" items="${houseList_label}"> 24 <tr bgcolor="#FFFFFF"> 25 <td>${L.id}</td> 26 <td>${L.name}</td> 27 <td>${L.address}</td> 28 </tr> 29 </c:forEach> 30 31 32 </table> 33 </body> 34 </html>
真心谢谢你很我讨论怎么久,下次有问题我再来我你哈。
@冥河守望: 行 多交流么!解决了就好!为啥requst不行,要用pagecontext?求解答
@吉吉的城: 还不清楚,等明天问问老师吧。。。
@冥河守望: 或者你改成request试试,然后在item里面写成${requestScope.houseList_label}看下能不能出来结果
@吉吉的城: 厉害了,我的哥,完美绝杀!!!
@吉吉的城: 这是什么原因呢?
@冥河守望: 我能说我也不知道吗?只是感觉pageContext都可以 为啥request不行,估计是属性范围的原因吧