Seam question regarding @In annotation and request params
mmarcom Jun 7, 2006 9:19 AMhi all,
i have written a small Seam application that resembles a shopping cart. I have a EcontrolSession bean, and two entity beans, Expense and Items.
I am adding items to the expense bean and then i plan to persist the expense via the SessionBean
My question is:
- my Session bean is stateful and has a session scope
- my Expense entity will be a global variable in the EControlSession bean, because it lasts for the lifespan of user session
- My Item will have a request scope because user enters items at every request.
My thought was that . this would have worked
@Stateful @Local ( {EControlSession.class}) @Name("econtrol") @Scope(SESSION) public class EControlSessionBean implements EControlSession, java.io.Serializable { private Expense expense = new Expense(); @In(create=true) private Item item = new Item(); @PersistenceContext EntityManager em; int counter = 0; /** * Add an user (Admin only should access this) */ public void addExpense (Expense expense) { System.err.println("Persisting expense"); em.persist(expense); System.err.println("Expense persisted.."); } public Expense getExpense() { return expense; } public Item getItem() { return item; } public String add() { item.setName("test" + 1); item.setPrice(1); expense.addItem(item); return "success"; } ...
and here's my page
<ui:composition 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" xmlns:s="http://jboss.com/products/seam/taglib" template="template.xhtml"> <ui:define name="body"> <h2> Adding Product</h2> <h:messages/> <f:view> <h:form id="foobrrewwe"> <table> <tr> <td>Site:<h:outputText value="#{econtrol.expense.site}"/></td> <td>Date:<h:outputText value="#{econtrol.expense.date}"/></td> </tr> </table> <table> <tr> <td>Product Name</td> <td><h:inputText value="#{econtrol.item.name}"/></td> </tr> <tr> <td>Price</td> <td><h:inputText value="#{econtrol.item.price}"> <f:converter converterId="javax.faces.Double"/> </h:inputText> </td> </tr> </table> <h:commandButton action="#{econtrol.add}" value="Add Item" class="formButton" style="width: 166px;" /> <h:commandButton action="#{econtrol.stop}" value="Finish" class="formButton" style="width: 166px;" /> </h:form> </f:view> <h4><a href="register.seam">Back To Index</a></h4> </ui:define> </ui:composition>
my assumption was because i thought that @In somehow resolved to a getter and setters, so that the Item object can be populated using
<h:inputText value="#{econtrol.item.price}/>
as i have found out, it does not work that way...
is the only way out for me to declare two @request variable and populate each item with those requet variables?
thanks and regards
marco