In variables and stateful beans
cfagiani May 14, 2006 10:39 AMI have a stateful bean that has a DataModel attribute that is populated by a Factory method on load of a facelet. On the same facelet, I have a form to create a new item. Is it possible to use the same stateful bean? (If I declare either an @In or @Out variable in the bean, I get an error on page load... so far, the only way I can get this to work is to have the save form call a different bean all together).
Anyone have any suggestions?
Here's the bean code...
@Stateful
@Name("editCategoryAction")
public class EditCategoryAction implements EditCategory, Serializable {
@EJB
private static ICategory categoryBean;
@DataModel
private List<Category> categoryList;
@Factory("categoryList")
public void listCategories() {
categoryList = categoryBean.listCategories();
}
public void saveCategories(){
categoryBean.saveCategories(categoryList);
}
@Destroy
@Remove
public void destroy() {
}
}
And here's the code for the facelet:
<!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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:c="http://java.sun.com/jstl/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title> Category Administration</title>
</head>
<body>
<h1>
Categories
</h1>
<f:view>
<h:form>
<div>
<h:outputText value="There are no categories." rendered="#{empty categoryList}" />
<h:dataTable value="#{categoryList}" var="category" rendered="#{not empty categoryList}">
<h:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:inputText value="#{category.name}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Description" />
</f:facet>
<h:inputText value="#{category.desc}" />
</h:column>
</h:dataTable>
</div>
<div>
<h:messages />
</div>
<div>
<h:commandButton value="Save" action="#{editCategoryAction.saveCategories}" />
</div>
</h:form>
<h:form>
<div>
<table>
<tr>
<td>
Name:
<h:inputText value="#{category.name}" />
</td>
<td>
Desc:
<h:inputText value="#{category.desc}" />
</td>
<td>
<h:commandButton value="Create Category" action="#{createCategoryAction.createCategory}" />
</td>
</tr>
</table>
</div>
</h:form>
</f:view>
</body>
</html>
And finally, the code for the other bean:
@Stateless
@Name("createCategoryAction")
public class CreateCategoryAction implements CreateCategory {
@In
private Category category;
@EJB
private static ICategory categoryBean;
public void createCategory() {
categoryBean.saveCategory(category);
}
}
What I'd like to do is combine the 2 beans into a single bean that is responsible for the interactions of this single page.