如何在生活中使用集合



嗨,我正在使用Liferay SKD用于Java和Liferay 6.1(Tomcat)。我已经创建了自定义数据库如下:

<entity name="PCustomer" local-service="true" remote-service="false">
    <!-- PK fields -->
    <column name="customerId"           type="int" primary="true" />
    <!-- Audit fields -->
    <column name="name"                 type="String"/> 
    <column name="vAddress"             type="String"/>
    <column name="pAddress"             type="String"/>
    <column name="comments"             type="String"/>
    <column name="pusers"               type="Collection"       entity="PUser"      mapping-key="userId"/>
    <column name="pcontacts"            type="Collection"       entity="PContact"   mapping-key="contactId"/>
    <column name="pdemos"               type="Collection"       entity="PUserDemo"  mapping-key="demoId"/>
    <column name="plicenses"            type="Collection"       entity="PLicense"   mapping-key="licenseId"/>
    <column name="pfolders"             type="Collection"       entity="PFolder"    mapping-key="folderId"/>
</entity>

使用Service.xml,现在我想检索与某个客户相关的所有联系人。问题是,当我在JSP页面中这样做时:

<%
    String user = request.getRemoteUser();
    int userId = Integer.valueOf(user);
    PUser pUser=PUserLocalServiceUtil.getPUser(userId);
    int customerId = pUser.getCustomerId();
     PCustomer customer=PCustomerLocalServiceUtil.getPCustomer(customerId);
     java.util.List<PContact> contCus=PCustomerUtil.getPContacts(customerId);
%>

并尝试遍历这个列表,使用for每个循环:

%for (PContact pContact : contCus) 
    if(pContact.getUserType().equals("billing"))
    {%> DO SOMETHING <% } %>

显示错误:

. lang。ClassCastException: $Proxy288不能强制转换为com.myportlet.service.persistence.PCustomerPersistence

我调试了它,所有的值都是正常的,直到它尝试在JSP页面中创建列表。问题是,在页面上,它告诉我,我必须像这样构建列表。使用这些参数等等。它没有给我任何错误。

有人可以帮助我或告诉我我做错了什么?

任何帮助将不胜感激。提前感谢!!!!

尝试使用JSTL c:forEach标签:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% pageContext.setAttribute("contCus", contCus); %>
<c:forEach var="pContact" items="${contCus}">
    <c:out value="${pContact.userType}"/>
</c:forEach>    

我认为这与Java集合和迭代无关…

似乎您正在尝试将Spring托管bean转换为代码中的某个具体类。问题是Spring AOP使用代理来包装bean,在这种情况下,您唯一可以假设的是代理实现了与原始类相同的一组接口。奇怪的是,当你使用service builder来生成服务时,PCustomerPersistence应该已经是一个接口而不是一个类了。

顺便说一句,你永远不应该从你的服务外部调用PCustomerUtil(或由服务构建器生成的其他实体的等效类)。这些类直接从持久化层公开方法,因此只能从您的服务调用,而不能从jsp或portlet调用。

最新更新