0

嗨,我正在使用 Liferay SKD for 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 (PContact pContact : contCus) 
    if(pContact.getUserType().equals("billing"))
    {%> DO SOMETHING <% } %>

它给了我一个错误:

java.lang.ClassCastException:$Proxy288 无法转换为 com.myportlet.service.persistence.PCustomerPersistence

我对其进行了调试,所有值都正常,并且它可以正常工作,直到它尝试在 JSP 页面中创建列表。问题是在页面中它向我展示了我必须像这样构建列表。使用这个参数等等。它没有给我任何错误。

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

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

4

2 回答 2

1

尝试使用 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>    
于 2013-05-15T15:18:19.750 回答
0

我认为这与Java集合或迭代无关......

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

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

于 2013-05-15T19:41:06.397 回答