sinat_33164199
sinat_33164199
采纳率30%
2015-12-26 11:30

jsp里面的foreach如何正确获取从 servlet里list里的值

jsp页面
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

















宠物医院管理系统



系统首页 登录 注册

































用户ID:
用户登录名:
用户密码:
性别:
联系方式:
地址:
宠物ID:

请选择宠物ID..

${listpets}
/c:forEach

    </tr>






servlet页面
package www.bttc.manager.ownerservlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import www.bttc.manager.model.Pets;
import www.bttc.manager.model.Powner;
import www.bttc.manager.service.OwnerService;
import www.bttc.manager.service.PetsService;
import www.bttc.manager.service.impl.OwnerServiceImpl;
import www.bttc.manager.service.impl.PetsServiceImpl;
import www.bttc.manager.utils.ServiceException;

public class AddOwnerServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doPost(request,response);       
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");// 设置请求的编码方式
    response.setCharacterEncoding("UTF-8");// 设置响应的编码方式
    response.setContentType("text/html;charset=utf-8");

    OwnerService ownerService=new OwnerServiceImpl();
    PetsService petsService=new PetsServiceImpl();
    HttpSession session=request.getSession();
    int ownerID=Integer.parseInt(request.getParameter("ownerID"));
    String osex=request.getParameter("osex");
    String oname=request.getParameter("oname");
    String opassword=request.getParameter("opassword");
    int phone=Integer.parseInt(request.getParameter("phone"));
    String address=request.getParameter("address");
    int petID=Integer.parseInt(request.getParameter("petID"));

    /*try {
        List<Pets> list=(List<Pets>) petsService.findAllPetID();
        //request.setAttribute("ownerID", ownerID);
        request.setAttribute("list", list);
    } catch (ServiceException e1) {
        e1.printStackTrace();
    }*/

    try {
        //Pets pets=new Pets();
        Pets pets=petsService.findPetsByPetsID(petID);
        if(ownerService.findOwnerByOwnerID(ownerID)!=null){
            session.setAttribute("message", "添加的普通用户记录已存在!");
            request.getRequestDispatcher("addowner.jsp").forward(request, response);
        }else{
            Powner owner=new Powner(ownerID,osex,oname,opassword,phone,address,pets);
            ownerService.addOwner(owner);
            //Powner owner=ownerService.addOwner(owner);
            session.setAttribute("message", "添加普通用户已成功!");
            request.getRequestDispatcher("/admin.jsp").forward(request, response);

        }
    } catch (ServiceException e) {
        e.printStackTrace();
    }



}

}
另一个servlet

package www.bttc.manager.ownerservlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import www.bttc.manager.model.Pets;
import www.bttc.manager.service.PetsService;
import www.bttc.manager.service.impl.PetsServiceImpl;
import www.bttc.manager.utils.ServiceException;

public class SelectPetID extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doPost(request,response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");// 设置请求的编码方式
    response.setCharacterEncoding("UTF-8");// 设置响应的编码方式
    response.setContentType("text/html;charset=utf-8");

    //int petID=Integer.parseInt(request.getParameter("petID"));
    PetsService petsService=new PetsServiceImpl();

    try {
        List<Pets> pets = petsService.findAllPetID(); 
        request.setAttribute("listpets", pets);
        request.getRequestDispatcher("/addowner.jsp").forward(request, response);
    } catch (ServiceException e) {
        e.printStackTrace();
    }


}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • lijunwyf 野木香 6年前

    <%
    List list = (List)request.getAttribute("listpets");
    for(Object o: list){
    Pets p = (Pets)o;

    %>

    <%=p.getName()%>

    <%}%>

    点赞 评论 复制链接分享
  • Bush_nanjing xiaoke_code 6年前

    $(requestScope.listpets)这样就在前台获得了后台传来的list集合
    关于遍历后台传来的list
    可以用JSTL标签中的foreach
    var就是后台传的list,item是遍历的元素。很长时间没写JavaWeb了,可能forEach里的属性我记错了

    点赞 1 评论 复制链接分享

为你推荐