Reputation: 145
I want to set up CDI in my existing JSF 2-Websphere application.I am using CDI Conversional scope for state saving.
I have included bean.xml in WEB-INF and annotations in my class.But still I couldnt access CDI bean inside JSP pages.can anyone provide any samples on how this be done?
Upvotes: 1
Views: 2932
Reputation: 81
Following is an example to used an CDI @Named annotated class in JSP
class :
@RequestScoped
@Named(value = "emp")
public class Employee {
int empno;
String ename;
double salary;
public Employee() {
this.ename="Shankar";
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
Now the JSP Fragment to use this class
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<NoAutoGeneratedIdViewHandlertitle>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
${emp.ename}
</body>
</html>
This @Named annotated classes can be used in EL in JSP
Hope this satisfies you. Pl. ask for any clarification
Upvotes: 1
Reputation: 18020
It works fine in WebSphere 8.5.5. You need to annotate class with @Named("yourName")
and usually with some scope related annotation.
Then you can access your bean in plain jsp using EL like this: ${yourName.property}
Upvotes: 1
Reputation: 9059
The Weld home page mentions as
For Servlet Containers (Tomcat/Jetty) or Java EE 5 Application Servers:
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>1.1.10.Final</version>
</dependency>
The 18.3. Servlet containers (such as Tomcat or Jetty) also told us as
While JSR-299 does not require support for servlet environments, Weld can be used in a servlet container, such as Tomcat 6.0 or Jetty 6.1.
Weld can be used as a library in an web application that is deployed to a Servlet container. You should place
weld-servlet.jar
within theWEB-INF/lib
directory relative to the web root.weld-servlet.jar
is an "uber-jar", meaning it bundles all the bits of Weld and CDI required for running in a Servlet container, for your convenience. Alternatively, you can use its component jars. A list of transitive dependencies can be found in theMETA-INF/DEPENDENCIES.txt
file inside theweld-servlet.jar
artifact.
Firstly we also need to explicitly specify the servlet listener (used to boot Weld, and control its interaction with requests) in WEB-INF/web.xml
in the web root:
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
I hope this may help.
Upvotes: 1