parameters and variables
nobody4601 May 3, 2011 12:58 PMHello, I'm new to seam and actually in web development at all, so there are probably many things I do not understand, or I think I understand, but I'm wrong :)
The thinks I need should be relatively simple:
1) take a parameter discountId
from an URL:
http://127.0.0.1:8080/seam-booking/home.seam?discountId=33
and send it to the session bean's method to get the object with corresponding ID.
So I have:
@Stateful @Name("DiscountController") @Scope(SESSION) @TransactionAttribute(REQUIRES_NEW) public class DiscountControllerAction implements DiscountController { @PersistenceContext private EntityManager em; @RequestParameter String discountId; public Discount getDiscountById() { String sQuery = "Select dsc from Discount dsc WHERE dsc.id = ?1"; Query query = em.createQuery(sQuery); Long dscId = Long.decode(discountId); query.setParameter(1, dscId); return (Discount) query.getSingleResult(); }
and in home.xhtml:
<h:outputText id="outText1" value="#{DiscountController.getDiscountById().formatedText}" escape="false"/>
This was the only way I could make it work but I'd prefer to store that discountId url parametr in some variable "variable" and then use something like:
<h:outputText id="outText1" value="#{DiscountController.getDiscountById(variable).formatedText}" escape="false"/>
such "variable" I could also use later, but I do not know how to do that and if it is possible at all.
Actually, that's related to my second question:
2) I need to display more properties from the "Discount" what is the return type from the
#{DiscountController.getDiscountById()}
So shoud I each time use
<h:outputText id="outText1" value="#{DiscountController.getDiscountById().formatedText}" escape="false"/> <h:outputText id="outText2" value="#{DiscountController.getDiscountById().name}"/> <h:outputText id="outText2" value="#{DiscountController.getDiscountById().address}"/>
or is there a way to create variable "discount" and use
<h:outputText id="outText1" value="#{discount.formatedText}" escape="false"/> <h:outputText id="outText2" value="#{discount.name}"/> <h:outputText id="outText2" value="#{discount.address}"/>
thanks a lot!
Peter