如何使用Spring MVC在一个表中的JSP页面上显示两个表数据(子表和父表)

时间:2015-01-30 05:41:30

标签: spring hibernate jsp

我正在开发一个基于在线购物网络应用程序的项目。在这个项目中我使用的是Hibernate 4.3和Spring MVC 3.1。我有两个表,一个是Order表(orderid,customerid,dateorderplaced,description),第二个是Customers(父表)表。 我想根据订单表(customerid)的外键显示订单和客户名称的完整详细信息,我只能在jsp页面上显示客户ID而不显示客户名称。

订单表: -

DROP TABLE IF EXISTS `pib`.`orders`;
CREATE TABLE  `pib`.`orders` (
`orderid` int(10) unsigned NOT NULL auto_increment,
`customerid` int(10) unsigned NOT NULL,
`dateorderplaced` datetime default NULL,
`orderdetails` varchar(200) NOT NULL,
PRIMARY KEY  (`orderid`),
KEY `FK_orders_1` (`customerid`),
CONSTRAINT `FK_orders_1` FOREIGN KEY (`customerid`) REFERENCES `customers` (`customerid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

客户表: -

DROP TABLE IF EXISTS `pib`.`customers`;
CREATE TABLE  `pib`.`customers` (
`customerid` int(10) unsigned NOT NULL auto_increment,
`firstname` varchar(45) default NULL,
`lastname` varchar(45) default NULL,
`loginname` varchar(45) NOT NULL,
`loginpassword` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`mobile` varchar(13) default NULL,
`address` varchar(45) default NULL,
`city` varchar(45) default NULL,
`state` varchar(45) default NULL,
`pincode` varchar(45) default NULL,
`createddate` date default NULL,
`role` varchar(45) NOT NULL,
PRIMARY KEY  (`customerid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Order.java: -

public class Orders{
private Integer orderid;
 private Customers customers;
 private Date dateorderplaced;
 private String orderdetails;
//setters n getters
}

Cutomers.java: -

public class Customers  implements java.io.Serializable {


 private Integer customerid;
 private String firstname;
 private String lastname;
 private String loginname;
 private String loginpassword;
 private String email;
 private String mobile;
 private String address;
 private String city;
 private String state;
 private String pincode;
 private Date createddate;
 private String role;
// setters n getters
}

AdminOrderController.java: -

@Controller
public class AdminOrderController {
OrdersDAO ordersDAO;
CustomersDAO customersDAO;
@RequestMapping(value = "/vieworders.htm")
public ModelAndView prepareOrderList(){
    ModelAndView mav = new ModelAndView();
    mav.addAllObjects(prepareOrderModel());
    mav.setViewName("admin/vieworders");
    return mav;
}
我试图在jsp页面上显示的是orderid,客户的名字和姓氏或其他字段的订单表。希望你们明白我想做什么。提前谢谢。

public Map prepareOrderModel(){
    Map map = new HashMap();
    map.put("orderList", ordersDAO.findAll());
    //map.put("customersList", customersDAO.findAll());
    return map;
}

<table>
        <tbody>
        <% i=0; %>
        <tr>
        <th>#</th>
        <th>Order ID</th>
        <th>Customer</th>
        <th>Date</th>

        </tr>
          <c:forEach items="${orderList}" var="ol">
        <tr>
        <td><%= ++i %></td>
        <td>${ol.orderid}</td>
        <td>${ol.customer}</td>
        <!-- <td>${ol.firstname}</td>
        <td>${ol.cutomers.firstname}</td> -->
        <td>${ol.dateorderplaced}</td>
       </tr>
       </c:foreach> </table>

1 个答案:

答案 0 :(得分:0)

什么是$ {custName}?也许它应该是$ {ol.custName}或$ {ol.customer.custName}或者什么