//fix this /
Welcome to the first edition of Fix This.
The idea of this section is to challenge your
coding skills. In each issue, we will publish a code
brainteaser. In the following issue, we will let you
know what the right answer was and why. We'll
also share what percentage of submitters gave what answer so
you can see how you fared against other submitters. Our first
submission is from Arun Gupta, Java evangelist at Oracle.
2 THE CODE
Consider the following code fragment for a JSF backing bean:
@Named @Stateless
public class MyBean {
public void save() {
// business logic to persist to database
}
}The WAR structure looks like this:
JAVA IN ACTION
1 THE PROBLEM
Hint: This is the most
common error when
building CDI-enabled
applications.
WEB-INF/classes
/MyBean
index.xhtml
Contexts and Dependency Injection (CDI) is a
new specification in the Java EE 6 platform.
ABOUT US
It provides standards-based type-safe
dependency injection for your Web applications.
The CDI unifies JSF and EJB programming
models and bridges the gap between the Web
and the transactional tier by allowing an EJB
to be used as a JSF backing bean.
3 WHAT S THE FIX?
@Named allows the bean to be accessible in the .xhtml file for a
JSF page as an Expression Language #{ myBean.save}. Why can't the
EJB be injected in the JSF page?
1) EJBs must be packaged in a JAR or EAR file to enable injection.
2) “beans.xml” is required to enable injection.
3) The business methods of an EJB must have ActionEvent as the
parameter in order to be invoked.
4) CDI injection is not available from spec-defined classes.
GOT THE ANSWER?
E-mail it to us here.
Answers will be posted next issue.
blog
PHOTOGRAPH BY MARGOT HARTFORD
ART BY I-HUA CHEN