Currently I am using a Transaction View pattern to make lazy-loading of collections possible in views.
I have the following in web.xml
<filter>
<filter-name>view</filter-name>
<filter-class>com.jasoni.ViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>view</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>
And the Filter class has the following...
public class ViewFilter implements Filter {
@Resource UserTransaction tx;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
tx.begin();
chain.doFilter(request, response);
}
//catch here
finally {
//another try-catch
tx.commit();
}
}
}
Then assuming I have the following (rather contrived) backing bean
@ManagedBean
@RequestScoped
public class DepartmentEmployees {
@EJB
private DepartmentServiceBean deptService;
@ManagedProperty(value="#{param.deptId}")
private Integer deptId;
private Department dept;
@PostConstruct
public String init() {
dept = deptService.findById(deptId);
}
}
I can do something like this in my view (.xhtml file)
<ul>
<c:forEach var="emp" items="#{departmentEmployees.dept.employees}">
<li>#{emp.firstName} #{emp.lastName}</li>
</c:forEach>
</ul>
Just wondering if anybody knows of a different way to accomplish the same thing without using filters (or servlets).