Search code examples
jspliferayliferay-6

Duplicate local variable in jsp liferay


In my view.jsp page I am getting the duplicate local variable contact error from Eclipse. However there is no duplicate variable present. When I run the portlet I get this error

An error occurred at line: 25 in the jsp file: /html/contact/view.jsp
Type mismatch: cannot convert from com.bcnet.portlet.biobank.model.Contact to com.liferay.portal.model.Contact
22: total="<%= ContactLocalServiceUtil.getContactsCount() %>"
23: />

I am using my own Contact entity the code for view.jsp is as below

<%@ include file="/html/init.jsp" %>
<%!
   com.liferay.portal.kernel.dao.search.SearchContainer<Contact> searchContainer = null;
%>

<%
    String redirect = PortalUtil.getCurrentURL(renderRequest);
%>

<aui:button-row>
    <portlet:renderURL var="addContactURL">
        <portlet:param name="mvcPath" value="/html/contact/edit_contact.jsp" />
        <portlet:param name="redirect" value="<%= redirect %>" />
    </portlet:renderURL>

    <aui:button onClick="<%= addContactURL.toString() %>" value="add-contact" />
</aui:button-row>

<liferay-ui:search-container emptyResultsMessage="contact-empty-results-message">
    <liferay-ui:search-container-results
        results="<%= ContactLocalServiceUtil.getAllContacts(searchContainer.getStart(), searchContainer.getEnd()) %>"
        total="<%= ContactLocalServiceUtil.getContactsCount() %>"
    />

    <liferay-ui:search-container-row
        className="com.bcnet.portlet.biobank.model.Contact"
        keyProperty="contactId"
        modelVar="contact" escapedModel="<%= true %>"
    >
        <liferay-ui:search-container-column-text
            name="name"
            value="<%= contact.getFirstName()+\" \"+contact.getLastName() %>"
        />

        <liferay-ui:search-container-column-text
            name="phone"
            property="phone"
        />

        <liferay-ui:search-container-column-text
            name="email"
            property="email"
        />

        <liferay-ui:search-container-column-text
            name="address"
            property="address"
        />

        <liferay-ui:search-container-column-text
            name="zip"
            property="zip"
        />

        <liferay-ui:search-container-column-text
            name="city"
            property="city"
        />

        <%
            String country = "";

            try {
                country = CountryLocalServiceUtil.getCountry(contact.getCountry()).getName();
            } catch (Exception e) {
            }
        %>

        <liferay-ui:search-container-column-text
            name="country"
            value="<%= country %>"
        />

        <%
            String juristicPersonName = "";

            try {
                juristicPersonName = JuristicPersonLocalServiceUtil.getJuristicPerson
                        (contact.getJuristicPersonId()).getName();
            } 
            catch (Exception e) {
            }
        %>

        <liferay-ui:search-container-column-text
            name="juristicPersonName"
            value="<%= juristicPersonName %>"
        />

        <liferay-ui:search-container-column-text
            name="department"
            property="department"
        />

        <liferay-ui:search-container-column-text
            name="orcId"
            property="orcId"
        />

        <liferay-ui:search-container-column-jsp
            align="right"
            path="/html/contact/contact_actions.jsp"
        />
    </liferay-ui:search-container-row>

    <liferay-ui:search-iterator />
</liferay-ui:search-container>

Solution

  • The imports seem to be all correct in init.jsp for my custom Contact entity.

    <%@ page import="com.bcnet.portlet.biobank.model.Contact" %>
    <%@ page import="com.bcnet.portlet.biobank.service.ContactLocalServiceUtil"%>
    

    May be liferay's predefined contact type com.liferay.portal.model.Contact also seems to be imported within the same scope somehow which might have caused the problem. And hence Type mismatch: cannot convert from com.bcnet.portlet.biobank.model.Contact to com.liferay.portal.model.Contact.

    The work around was to give the modelVar attribute some value other than contact say like biobankContact. So changing this

    <liferay-ui:search-container-row
            className="com.bcnet.portlet.biobank.model.Contact"
            keyProperty="contactId"
            modelVar="contact" escapedModel="<%= true %>"
        >
    

    to

    <liferay-ui:search-container-row
            className="com.bcnet.portlet.biobank.model.Contact"
            keyProperty="contactId"
            modelVar="biobankContact" escapedModel="<%= true %>"
        >
    

    seemed to resolve the problem.