Created
November 5, 2015 10:17
-
-
Save Rokko11/a04fefd9d319b0fad540 to your computer and use it in GitHub Desktop.
Documentation, how to use a composite component together with JavaEE-events.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
Verwendung der oben definierten Komponente | |
namespace: xmlns:util="http://java.sun.com/jsf/composite/util" | |
--> | |
<util:greeting value="#{greetingBean.value}" greeting="#{greetingBean.greeting}" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.enterprise.event.Event; | |
import javax.inject.Inject; | |
import javax.inject.Named; | |
/** | |
* Klasse, die das zu feuernde Event beinhaltet. | |
* Diese wird benoetigt, da bei JSF < 2.2 keine DI in Komponenten moeglich ist. | |
*/ | |
@Named | |
public class EventContainer { | |
/** | |
* Das zu feuernde Event | |
*/ | |
@Inject | |
Event<String> event; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ui:composition xmlns="http://www.w3.org/1999/xhtml" | |
xmlns:cc="http://java.sun.com/jsf/composite" | |
xmlns:ui="http://java.sun.com/jsf/facelets" | |
xmlns:h="http://java.sun.com/jsf/html" | |
xmlns:p="http://primefaces.org/ui"> | |
<!--Man bemerke die Referenz auf die GreetingComponent--> | |
<cc:interface componentType="greetingComponent"> | |
<!--Auf dieses Attribut wird in der GreetingComponent zurueckgegriffen.--> | |
<cc:attribute name="value" required="true" /> | |
<cc:attribute name="greeting" required="true" /> | |
</cc:interface> | |
<cc:implementation> | |
<h:form id="greeting-form"> | |
<h:inputText value="#{cc.attrs.value}" /> | |
<!--Action-Listener geht auf die refresh-Methode. Dies passiert per Ajax, | |
um sofort einen Effekt zu sehen. Funktioniert natuerlich auch ohne Primefaces--> | |
<p:commandButton actionListener="#{cc.refresh}" value="Greet me" update="@form"/> | |
<h:outputText value="#{cc.attrs.greeting}" /> | |
</h:form> | |
</cc:implementation> | |
</ui:composition> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static javax.enterprise.event.Reception.*; | |
import java.io.Serializable; | |
import javax.enterprise.event.Observes; | |
import javax.inject.Named; | |
import org.omnifaces.cdi.ViewScoped; | |
/** | |
* Bean, welches die Anzeigesteuerung der View regelt. | |
*/ | |
@Named | |
@ViewScoped | |
public class GreetingBean implements Serializable { | |
private String value; | |
/** | |
* Attribut ist readonly, da es ausschliesslich ueber Events zu setzen ist. | |
*/ | |
private String greeting; | |
public void setValue(String value) { | |
this.value = value; | |
} | |
public String getValue() { | |
return value; | |
} | |
public String getGreeting() { | |
return greeting; | |
} | |
/** | |
* Event-Listener. Kann auch ohne Event aufgerufen werden. | |
* Wegen Bean-Konventionen heisst diese Methode nicht setGreeting. | |
* @param value ist der im Input-Feld eingegebene Name | |
*/ | |
public void updateGreeting(@Observes(notifyObserver = IF_EXISTS) String value) { | |
this.greeting = "Hello " + value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.faces.component.FacesComponent; | |
import javax.faces.component.UINamingContainer; | |
import org.omnifaces.util.Faces; | |
/** | |
* Greeting-Komponente: Wird direkt in der greeting.xhtml definiert. | |
*/ | |
@FacesComponent("greetingComponent") | |
public class GreetingComponent extends UINamingContainer { | |
/** | |
* Refresh-Methode. Wird in der greeting.xhtml mit cc.refresh aufgerufen. | |
*/ | |
public void refresh() { | |
// Hier wird der Wert des Attributs "cc.attrs.value" geholt. | |
String value = (String) getValueExpression("value").getValue(getFacesContext().getELContext()); | |
// Workaround. Da mit JSF < 2.2 das Event nicht in Komponenten-Klassen | |
// injected werden kann, Umweg ueber einen EventContainer. | |
((EventContainer) Faces.evaluateExpressionGet("#{eventContainer}")).event.fire(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment