问题
Background / Problem
I'm trying to create a reusable JSF composite component that can be shared between many applications by packaging the components in jars (similar to Deploy JSF composite components for shared use and JSF 2.0 composite component into jar).
When trying to refer to these components in a facelets page of a web project in Eclipse, it doesn't autocomplete (content assist) the tags or attributes of my composite component.
Using the same composite component jar in NetBeans works (autocompletion works!).
Quick summary
Eclipse (actually Rational Software Architect 8.0.4.1 - based on Eclipse Helios)
Web project with Dynamic web module and JSF facets enabled
Composite component jar on the build path
Composite component namespace declared in facelets page (interestingly it does autocomplete the namespace string of my component)
Only the standard JSF tags autocomplete (h:, f:, ui:, etc), as well as Primefaces (p:)
No autocompletion for my composite component :(
Autocompletion does work if I move the component to a web project and consume it from there (but then it's no longer reusable in other projects!)
The details
Composite component jar structure
My composite component jar has the following structure when compiled (I'm actually using Maven, so the source structure is a little different).
composite-component.jar
|- META-INF/
| |- beans.xml (empty - just the beans tag)
| |- faces-config.xml (empty - just the faces-config tag/attributes)
| \- resources
| \- example
| \- country.xhtml
\- com
\- example
|- Country.java
|- CountryController.java (the CDI managed bean)
Composite component definition
country.xhtml has the following contents
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<!-- INTERFACE -->
<cc:interface>
<cc:attribute name="value"
shortDescription="A value expression for the selected value."
required="true" />
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
<p:autoComplete value="#{cc.attrs.value}"
completeMethod="#{countryController.complete}" var="country"
itemLabel="#{country.name}" itemValue="#{country.code}" dropdown="true"
forceSelection="true">
</p:autoComplete>
</cc:implementation>
</html>
Managed bean
My CDI managed bean has the following contents
package example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
@ApplicationScoped
@Named
public class CountryController {
private static final List<Country> COUNTRIES = Arrays.asList(
new Country("AUS", "Australia"),
new Country("FRA", "France"),
new Country("USA", "United States of America"));
public List<Country> complete(String query) {
List<Country> suggestions = new ArrayList<Country>();
for (Country c : COUNTRIES) {
if (c.getName().toLowerCase().contains(query.toLowerCase())) {
suggestions.add(c);
}
}
return suggestions;
}
}
Consuming the composite component
If I create a JSF project in NetBeans and add my composite component (and Primefaces) to the build path, add the correct namespace to my facelets page (xmlns:x="http://java.sun.com/jsf/composite/example"
) it just works.
I get autocompletion on my component, it's attributes, and it even uses the documentation from my component's definition.
If I add the same composite component jar to the build path of my Eclipse web project (JSF facet enabled), I get nothing. It autocompletes for standard JSF tags and for Primefaces, but not for my component.
I've noticed that Primefaces uses a taglib file - is this what's required for my scenario? I'd hate to have to duplicate the information to get autocompletion to work.
Edit:
As tasel has suggested, the best solution (so far) is to install JBoss Tools (specifically the RichFaces plugin). This enables autocompletion for composite components packaged in jar files (including inserting all mandatory attributes for you, and autocompleting all other attributes with hovering documentation).
I'd also recommend installing the JBoss Tools CDI plugin if you're using CDI, as this gives you autocompletion for CDI managed beans and their properties/methods.
The Eclipse update site URLs are:
Helios:
http://download.jboss.org/jbosstools/updates/stable/helios/
Indigo:
http://download.jboss.org/jbosstools/updates/stable/indigo/
I did have issues installing from the update site URL and ended up downloading the p2 repo from the update page and installing it locally, but apart from that I'm very happy to have autocompletion working in Eclipse!
回答1:
Unfortunately support for JSF in Eclipse is not as comfortable as in NetBeans. I prefer to use the RichFaces Tools plugin from jBoss (which works perfectly without RichFaces and with any container). You might give it a try.
Better support for JSF is considered to be available with the current eclipse release 'Juno' though I have not tried it yet.
来源:https://stackoverflow.com/questions/11557201/how-to-make-eclipse-autocomplete-work-with-jsf-composite-components-in-jar