list<实体类>互相嵌套和前台取值问题

2023-04-23,,

list<实体类>嵌套list<实体类>,必须保证嵌套的实体类里面有这个list对象,把这个list<实体类>当做一个对象

这是需要解析的数据,并把这些数据封装成list<实体类>对象,传给前台

<root>
<RETURN_CODE>0</RETURN_CODE>
<RETURN_DESC>成功!</RETURN_DESC>
<RETURN_INFO>
<order_info>
<so_nbr>7004519</so_nbr>
<cust_id>2028814</cust_id>
<appl_date>2019-03-21 15:27:56</appl_date>
<compl_date>null</compl_date>
<deal_staff_name>null</deal_staff_name>
<deal_staff_mobile>null</deal_staff_mobile>
<compl_phenom>无法打开互动电视页面</compl_phenom>
<fault_reason>null</fault_reason>
<book_date>2019-04-19 14:00:00</book_date>
<book_time>14:00--18:00</book_time>
<run_sts>待处理</run_sts>
<deal_log>
<deal_info>
            <deal_time>2019-03-29 08:58:27</deal_time><deal_type>领单</deal_type><deal_name>王超群</deal_name><deal_mobile>null</deal_mobile>
          </deal_info>
<deal_info>
            <deal_time>2019-03-29 09:00:04</deal_time><deal_type>回单</deal_type><deal_name>王超群</deal_name><deal_mobile>null</deal_mobile>
          </deal_info>
<deal_info>
            <deal_time>2019-03-22 17:01:57</deal_time><deal_type>定单生成</deal_type><deal_name>系统</deal_name><deal_mobile>null</deal_mobile>
          </deal_info>
</deal_log>
</order_info>
<order_info>
<so_nbr>7004505</so_nbr>
<cust_id>2028814</cust_id>
<appl_date>2019-04-19 08:48:50</appl_date>
<compl_date>null</compl_date>
<deal_staff_name>null</deal_staff_name>
<deal_staff_mobile>null</deal_staff_mobile>
<compl_phenom>整体网速慢</compl_phenom>
<fault_reason>null</fault_reason>
<book_date>2019-04-19 08:30:00</book_date>
<book_time>08:30--12:00</book_time>
<run_sts>待处理</run_sts>
<deal_log>
<deal_info>
            <deal_time>2019-04-19 08:49:02</deal_time><deal_type>定单生成</deal_type><deal_name>系统</deal_name><deal_mobile>null</deal_mobile>
          </deal_info>
</deal_log>
</order_info>
</RETURN_INFO>
</root>

下面是实体类,为了节省空间get、set方法没写;如果不写toString()方法,看不到数据

public class OrderInfo {

    /**
* 定单号
*/
private String soNbr; /**
* 客户证号
*/
private String custId; /**
* 受理时间
*/
private String applDate; /**
* 完成时间
*/
private String complDate;/**
* dealLogInfo
*/
private List<DealLogInfo> dealLogInfo; }
public class DealLogInfo {

    /**
* 操作时间 yyyy-mm-dd hh24:mi:ss
*/
private String dealTime; /**
* 操作类型
*/
private String dealType; /**
* 操作人
*/
private String dealName; /**
* 操作人联系方式
*/
private String dealMobile; }

逻辑代码,不全

List<OrderInfo> orderList = new ArrayList<OrderInfo>();
//存储到list实体类集合
OrderInfo or = new OrderInfo();
orderList.add(or);
or.setApplDate(appl_date);
or.setBookDate(book_date);
or.setBookTime(book_time); List<DealLogInfo> dealInfoList = new ArrayList<DealLogInfo>();
DealLogInfo deal = new DealLogInfo();
dealInfoList.add(deal);
for (Element element : dealList) {
deal.setDealTime(deal_time);
deal.setDealType(deal_type);
deal.setDealName(deal_name);
deal.setDealMobile(deal_mobile);
}
or.setDealLogInfo(dealInfoList);
request.setAttribute("orderList", orderList);

前台取值

遇到的错误:

el表达式获取list数据报错:java.lang.NumberFormatException:For input string: "xxx"

后来查询资料,是由于list会默认将后面的数据当做下标读取,会出现这个情况

错误代码:

<c:forEach items="${orderList}" var="orderList" >
<ul >
<li>
<div>客户证号:</div>
<div>${orderList.custId}</div>
</li>
<li>
<div>故障现象:</div>
<div>${orderList.complPhenom}</div>
</li>
</ul>
<ul>
<li class="title color-6">派单</li>
<li class="detail color-9">
<p>${orderList.dealLogInfo.dealTime}</p>
</li>
</ul>
</c:forEach>

正确代码

只要标明下标或者再次foreach遍历roles的list就可以了

<c:forEach items="${orderList}" var="orderList" >
<ul >
<li>
<div>客户证号:</div>
<div>${orderList.custId}</div>
</li>
<li>
<div>故障现象:</div>
<div>${orderList.complPhenom}</div>
</li>
</ul>
<c:forEach items="${orderList.dealLogInfo}" var="dealLogInfo">
<ul>
<li class="title color-6">派单</li>
<li class="detail color-9">
<p>${dealLogInfo.dealTime}</p>
</li>
</ul>
</c:forEach>
</c:forEach>

页面需要引入c标签代码,否则也会爆这个错误

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!doctype html>
<html lang="en">
<%
String openId = request.getAttribute("openId") == null ? "" : request.getAttribute("openId").toString();
String area_id = request.getAttribute("area_id") == null ? "" : request.getAttribute("area_id").toString();
String customercode = request.getAttribute("cust_id") == null ? "" : request.getAttribute("cust_id").toString(); %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

list<实体类>互相嵌套和前台取值问题的相关教程结束。

《list<实体类>互相嵌套和前台取值问题.doc》

下载本文的Word格式文档,以方便收藏与打印。