How do I get output from the NextClient
bean, which uses CDI, to the facelet?
I'm trying to use CDI imports:
package dur.beans;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
@Named("nextClient")
@ApplicationScoped
public class NextClient implements NextClientLocal {
private int next = 1009;
@Override
public int getNext() {
next = next + 1;
return next;
}
}
with a facelets example:
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:head></h:head>
<h:body>
This and everything before will be ignored
<ui:composition template="template.xhtml">
<ui:define name="navigation">
<ui:include src="menu.xhtml"/>
</ui:define>
<ui:define name="main">
<h1>bird</h1>
<p>
next #{nextClient.next}
</p>
</ui:define>
</ui:composition>
This and everything after will be ignored
</h:body>
</html>
and yet there doesn't seem to be any output from the bean:
thufir@dur:~$
thufir@dur:~$ lynx -dump http://localhost:8080/EntAppWeb-war/next.xhtml birds...
crud ops
__________________________________________________________________
[1]Home
[2]Parrot
[3]Eagle
[4]Falcon
[5]next
bird
next
References
1. http://localhost:8080/EntAppWeb-war/next.xhtml
2. http://localhost:8080/EntAppWeb-war/next.xhtml
3. http://localhost:8080/EntAppWeb-war/next.xhtml
4. http://localhost:8080/EntAppWeb-war/next.xhtml
5. http://localhost:8080/EntAppWeb-war/next.xhtml
thufir@dur:~$
This example is adapted from Facelets Essentials ; I'd like to use CDI, however.
is the problem with beans.xml
? Some places say it's optional, others that beans.xml
is required. It seems optional:
23.13 Configuring a CDI Application
When your beans are annotated with a scope type, the server recognizes the application as a bean archive and no additional configuration is required. The possible scope types for CDI beans are listed in Using Scopes. CDI uses an optional deployment descriptor named beans.xml. Like other Java EE deployment descriptors, the configuration settings in beans.xml are used in addition to annotation settings in CDI classes. The settings in beans.xml override the annotation settings if there is a conflict. An archive must contain the beans.xml deployment descriptor only in certain limited situations...
The Java EE 7 Tutorial Release 7 for Java EE Platform p 404
From what I've read, it seems that CDI is recommended over @ManagedBean
, for example. I've not found a simpler example, than this, to start from.
see also:
https://stackoverflow.com/a/4397444/262852
https://stackoverflow.com/questions/26110888/what-is-the-alternative-to-managedbean
source code:
The backing bean is correct. You need to check that your server supports CDI or you would need to use some extra libraries to make it work (eg Apache Tomcat).
I think the beans.xml with CDI is required as the container needs to scan all the beans that are annotated with CDI annotations.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
</beans>
This bean-discovery-mode="annotated" is what scan your classes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.