genericsjsfcomposite-component

How to Specify a Generic Type in JSF Composite Component Attribute?


I'm working on a JSF project where I need to pass a List to a composite component. However, I'm encountering an issue when trying to specify this generic type in the type attribute of cc:attribute. Here's what I've tried:

<cc:interface>
    <cc:attribute name="patient" type="com.divudi.entity.Patient" />
    <cc:attribute name="rooms" type="java.util.List<com.divudi.entity.inward.PatientRoom>" />
</cc:interface>

This results in the following error:

javax.servlet.ServletException: Error Parsing /resources/ezcomp/common/admission_details.xhtml: Error Traced[line: 13] The value of attribute "type" associated with an element type "cc:attribute" must not contain the '<' character.

I understand that JSF composite component attributes don't support generic types directly.

Is there a workaround or a best practice for handling this situation?

I need to ensure that the rooms attribute specifically accepts a List and not just any List. How can I achieve this while avoiding the parsing error?


Solution

  • You can't, also not after fixing the XML parsing error by using &lt;/&gt; instead of </>. Only the below is valid:

    <cc:attribute name="rooms" type="java.util.List" />
    

    Note that the restriction is ultimately in EL API not in JSF API.

    Your best bet to enforce type is to wrap it in a custom model which extends ArrayList:

    public class PatientRooms extends ArrayList<PatientRoom> {
        public PatientRooms(List<PatientRoom> rooms) {
            super(rooms);
        }
    }
    
    <cc:attribute name="rooms" type="com.divudi.entity.inward.PatientRooms" />