Stateful Session Bean updates(?) on buttonclick - java

I'm fairly new to Java EE and I'm building a simple webshop using maven web application . I have a problem with my Stateful Session Bean. I've been searching the net and tried different sollutions(most of them for using servlets) but it doesn't seem to work.
Anyway, my problem is that I'm trying to use my session bean to keep track of what's in the shopping cart. I am using an arrayList to store the items. However, when I add a new item it just replaces the old item instead of adding it to the list. I'm guessing the session bean somehow updates or a new instance of it is created but I just can't seem to find any sollution or information about this.
The stateful session bean
#Stateful
#LocalBean
public class CartSessionBean{
private List contents;
public CartSessionBean(){
contents= new ArrayList();
}
public List getContents() {
return contents;
}
public void addProduct(String title) {
contents.add(title);
}
}
The Managed Bean
#ManagedBean
#RequestScoped
public class ProductController {
private List cartList = new ArrayList();
private int nrOfCartItems=0;
#EJB private CartSessionBean cart;
public String doAddCart(String title)
{
cart.addProduct(title);
setCartList(cart.getContents());
setNrOfCartItems(cart.getContents().size());
return "products.xhtml";
}
}
The Facelet
<h:form>
<p>
your cart contains <h:outputLabel class="" value="#{productController.nrOfCartItems}" /> items.
<ui:repeat value="#{productController.cartList}" var="cart">
<h:outputLabel value="#{cart}" />
</ui:repeat>
<h:commandButton value="go to checkout"/>
</p>
</h:form>
<h:form>
<h:dataTable value="#{productController.productList}" var="pr" border="0">
<h:column>
<h:graphicImage value="images/#{pr.picture}" />
</h:column>
<h:column>
<h2><h:outputText value="#{pr.product_name}"/></h2>
<p> in stock: <h:outputText value="#{pr.stock}"/><br/>
price: <h:outputText value="#{pr.price}"/> SEK<br/><br/>
<h:outputText value="#{pr.description}"/><br/></p>
<h:commandButton value="add to cart" action="#{productController.doAddCart(pr.product_name)}"/>
</h:column>
</h:dataTable>
</h:form>

Your managed bean should be SessionScope to live during the session.
In your case you always creating new ProductController bean for each request and because of that you always inject different CartSessionBean (there is no way how could container know that it should inject the same SessionBean into your RequestScope Managed Bean).

Related

Passing parameter from one page to another - The scope of the object referenced by expression is shorter than the referring managed beans error

I am trying to pass a parameter from one page to another, but when i press the button "Details" it gives me this error: "Unable to create managed bean agencyDetailBean. The following problems were found: - The scope of the object referenced by expression #{agency}, request, is shorter than the referring managed beans (agencyDetailBean) scope of session"
This is part from my .xhtml page:
<p:dataGrid var="agency" value="#{agencyBean.agencyList}" columns="1"
rows="5" paginator="true" paginatorPosition="bottom">
<p:growl id="growl" showDetail="true" />
<p:fieldset legend="${agency.tUser.name}" toggleable="true">
<p:ajax event="toggle" listener="#{fieldsetBean.handleToggle}"
update="growl" />
<h:panelGrid columns="2" cellpadding="3" cellspacing="1">
<h:panelGrid columns="2" cellpadding="3" cellspacing="1">
Name: <h:outputText value="${agency.tUser.name}" />
Phone: <h:outputText value="${agency.tUser.phone}" />
</h:panelGrid>
<h:commandButton value="Details" action="agencyDetail">
<f:setPropertyActionListener target="#{agencyBean.tAgency}"
value="${agency}" />
</h:commandButton>
</h:panelGrid>
</p:fieldset>
</p:dataGrid>
My 1st bean:
#ManagedBean(name = "agencyBean")
#SessionScoped
public class AgencyBean implements Serializable {
private TAgency tAgency = new TAgency();
private List<TAgency> agencyList;
public List<TAgency> getAgencyList() {
return agencyList;
}
public void setAgencyList(List<TAgency> agencyList) {
this.agencyList = agencyList;
}
#PostConstruct
public void init() {
EntityManager em = HibernateUtil.getEntityManager();
Query q = em.createQuery("select u from TAgency u");
agencyList = q.getResultList();
}
public TAgency gettAgency() {
return tAgency;
}
public void settAgency(TAgency tAgency) {
this.tAgency = tAgency;
}
}
My 2nd bean class:
#ManagedBean(name = "agencyDetailBean", eager = true)
#SessionScoped
public class AgencyDetailBean implements Serializable {
private static final long serialVersionUID = 1L;
#ManagedProperty(value = "#{tAgency}")
private AgencyBean agencyBean;
private TAgency tAgency;
public TAgency gettAgency() {
if (agencyBean != null) {
tAgency = agencyBean.gettAgency();
}
return tAgency;
}
public void setAgencyBean(AgencyBean agency) {
this.agencyBean = agency;
}
}
Initially, the .xhml page was like this and it worked.
<p:growl id="growl" showDetail="true" />
<c:forEach items="#{agencyBean.agencyList}" var="agency">
<p:fieldset legend="${agency.tUser.name}" toggleable="true">
...
<h:commandButton value="Details" action="agencyDetail">
<f:setPropertyActionListener target="#{agencyDetailBean.agency}"<br/>
value="${agency}" />
</h:commandButton>
</h:panelGrid>
</p:fieldset>
</c:forEach>
Any suggestion what is the problem and how to fix it?
Thanks in advance!
Using #ManagedBean, you cannot inject beans with smaller scope into beans with bigger scope. For your case, inject the #SessionScoped into the #RequestScoped. Then you can update any properties of the #SessionScoped from the #RequestScoped bean.
Regarding your update, in the 2nd bean, you cannot get tAgency from a getter like that. The following function would do the job.
#PostConstruct
public void init() {
tAgency = agencyBean.gettAgency();
}

Loaded Records not showing in view

So to make it relatively simple: I have some Primefaces-Page, which is supposed to represent database records in a table-structure.
I keep these records in a List<Customer> which resides in a #ConversationScoped backing bean. I have verified via debugging, that the List gets correctly filled with records from the Database (hibernate db FWIW). This is accomplished with the help of a "businessInterface" Distributor class, that is in fact nothing but a decoupling mask for (Database)Service-Classes.
As mentioned, I have verified that the Database, as well as the Distributor correctly return the expected values. Unfortunately in the View there are no records present and instead the emptyMessage is displayed.
The application is running on a jBoss 7.1.1-Final Application Server.
For better readability I have excluded the h:head, as well as h:body, ui:composition, ui:define and h:form around the provided code, as well as shortened the columns to the two different usages (property display and action exposition)
The View (shortened and truncated):
<ui:define name="inhalt">
<p:growl id="msgGrowl" autoUpdate="true" showDetail="true" />
<h:form onkeypress="if (event.keyCode == 13) {return false; }">
<p:dataTable var="customeritem" id="customerTable"
rowkey="#{customeritem.id}" value="#{customerListController.customerList}"
paginator="true" rows="13" autoUpdate="true"
filteredValue="#{customerListController.filteredCustomers}"
emptyMessage="no customers found!"
sortFunction="#{customerListController.filteredCustomers}">
<p:column sortBy="name" filterBy="name" headerText="Kunde"
filterMatchMode="contains">
<h:outputText value="#{customeritem.name}" />
</p:column>
<p:column>
<f:facet name="header">
<p:commandButton value="Neuer Kunde"
action="${customerListController.addCustomer()}"
icon="ui-icon-plus" />
</f:facet>
<p:commandButton id="doViewDetailsButton" icon="ui-icon-clipboard"
action="${customerListController.viewDetails(customeritem.getId())}" />
<p:tooltip for="doViewDetailsButton" value="Details ansehen" />
</p:column>
</p:dataTable>
</h:form>
</ui:define>
The Backing Bean:
#Named
#ConversationScoped
public class CustomerListController implements Serializable {
private static final long serialVersionUID = -5961625401284927892L;
private List<Customer> customerList = new ArrayList<Customer>();
private List<Customer> filteredCustomers = new ArrayList<Customer>();
#Inject
CustomerEditController customerEditController;
#Inject
CustomerDetailsController customerDetailsController;
#Inject
CustomerDistributor businessInterface;
public String addCustomer() {
return editCustomer(0l);
}
public String editCustomer(long customerId) {
setFilteredCustomers(null);
customerEditController.recieveCustomerById(customerId);
return Pages.CUSTOMER_EDIT;
}
public String viewDetails(long customerId) {
setFilteredCustomers(null);
customerDetailsController.recieveCustomerById(customerId);
return Pages.CUSTOMER_DETAILS;
}
public String deleteCustomer(long customerIdToDelete) {
businessInterface.delete(customerIdToDelete);
setFilteredCustomers(null);
fillCustomerList();
return Pages.CUSTOMER_LIST;
}
#PostConstruct
public void fillCustomerList() {
customerList.clear();
customerList.addAll(businessInterface.loadAll());
}
public List<Customer> getCustomerList() {
return customerList;
}
public List<Customer> getFilteredCustomers() {
return filteredCustomers;
}
public void setFilteredCustomers(List<Customer> filteredCustomers) {
this.filteredCustomers = filteredCustomers;
}
}
This used to work, when I had the Backing Bean in #SessionScoped, but as that required hackish workarounds to produce intuitive (and expected) behavior I decided to move the Backing Bean to a smaller scope. I therefore chose the #ConversationScoped, because the BackingBean needs to stay longer than the request lifecycle... (Also running a query against a db for every request is damn expensive...)
A short explanation on the CustomerEditController and CustomerDetailsController. They are the responsible ones for Editing and Showing further information on the single records if they are requested by clicking one of the Buttons.
The non-working stuff is the #PostConstruct public void fillCustomerList(). Everything else works as expected...
If you need any further information, please ask, I will provide context as needed ;)
I have found a successful workaround for this, but it's extremely hackish and I really dislike the approach, as it introduces additional behavior in a getter. I modified the Backing Bean as follows:
public List<Customer> getCustomerList() {
if (customerList.size() == 0) {
fillCustomerList();
}
return customerList;
}
But let me state this again,
this is definitely not the desired behavior and not a good approach at solving this problem.
EDIT:
I found a different fix after a little more digging and a lucky link. I modified the backing bean as follows:
#Inject
Conversation conversation;
#PostConstruct
public void init() {
if(conversation.isTransient()) {
conversation.end();
}
conversation.setTimeout(120000);
conversation.start();
}
and now It works even without the hackish behavior in the getter (as demonstrated above).

Injecting #Named managed bean into another reusing JSF page?

I'm trying to reuse a jsf page by including using facelets into another jsf page, since this page will be used by a lot of other pages. But the problem is that i can't get the atributes os the managed bean reposible for this page into the managed beans that includes this page.
The page, pessoa.xhtml, some of the repetitive and header code has been removed:
<h:body>
<div id="pessoa">
<h:form id="formPessoa">
<h:messages for="formPessoa"/>
<h:panelGrid columns="3">
<h:outputLabel for="id" value="Código: " />
<h:inputText id="id" value="#{pessoaMB.pessoa.id}"/>
<h:message for="id" />
<h:outputLabel for="apelidoNomeFantasia" value="Apelido/Nome Fantasia: " />
<h:inputText id="apelidoNomeFantasia" value="#{pessoaMB.pessoa.apelidoNomeFantasia}"/>
<h:message for="apelidoNomeFantasia" />
<h:outputLabel for="rgIe" value="RG/Inscrição Estadual: " />
<h:inputText id="rgIe" value="#{pessoaMB.pessoa.rgIe}"/>
</h:panelGrid>
</h:form>
</div>
</h:body>
The #Named managed PessoaMB
#Named
#SessionScoped
public class PessoaMB implements Serializable {
private Pessoa pessoa;
public PessoaMB() {
this.pessoa = new Pessoa();
} //fim do construtor
public Pessoa getPessoa() {
return pessoa;
}
public void setPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}
}
Here is the code of one of the pages that includes the pessoa.xhtml.
empresa.xhtml
<ui:composition template="/resources/template.xhtml">
<ui:define name="title">
<h:outputText value="Cadastro de Empresa"/>
</ui:define>
<ui:define name="content">
<h:form id="formEmpresa">
<ui:include src="/cadastro/pessoa/pessoa.xhtml" />
<h:commandButton id="novo" action="#{empresaMB.newEmpresa}" value="Novo" />
<h:commandButton id="salvar" action="#{empresaMB.insert}" value="Salvar" />
</h:form>
</ui:define>
</ui:composition>
And the #Named managed bean EmpresaMB.
#Named
#SessionScoped
public class EmpresaMB implements Serializable {
#EJB
private EmpresaEJBRemote empresaEJB;
private Empresa empresa;
#Inject
private PessoaMB pessoaMB;
public String insert() {
pessoaMB = this.getPessoaMB();
empresa.setId(pessoaMB.getPessoa().getId());
empresaEJB.insert(empresa);
return "/cadastro/empresa/empresa";
}
public String newEmpresa() {
pessoaMB = new PessoaMB();
return "/cadastro/empresa/empresa";
}
//both empresa and pessoaMB getters and setters has been added to the code
}
I think that the #Inject anotation would do the job, but it doesn't.
What is exactly the problem? Does the pessoaMB instance variable remains null in EmpresaMB? (i.e. does the initial injection fails?)
Or is the problem that you think that doing the new PessoaMB() will have any effect on the session scoped instance?
This last construct doesn't seem to make sense. Namely, CDI is injecting the EmpresaMB instance with an instance of PessoaMB. This is the exact instance used in the session. However, when you create a new instance in newEmpresa() you are simply overwriting the reference with another instance. This has no connection with the session scoped version whatsoever.
If you want the 'master' bean to produce other beans that gets inserted in its scope, you need to annotate the instance field with the #Named #Produces annotations:
#Named
#SessionScoped
public class EmpresaMB implements Serializable {
#EJB
private EmpresaEJBRemote empresaEJB;
private Empresa empresa;
#Named
#Produces
private PessoaMB pessoaMB;
}
See Reza Rahman's article for some additional details about this: Dependency Injection in Java EE 6: Conversations (Part 4)

Why input (for example h:inputText) nested in h:dataTable do not update Bean model? [duplicate]

This question already has an answer here:
Using <h:dataTable><h:inputText> on a List<String> doesn't update model values
(1 answer)
Closed 6 years ago.
I have jsf page:
....
<form jsfc="h:form" action="">
<h:dataTable value="#{newMusician.strings}" var="preferredMusicGenre" id="musicGenresSelectTable">
<h:column>
<h:inputText value="#{preferredMusicGenre}" immediate="true"/>
</h:column>
</h:dataTable>
<p>
<input type="submit" jsfc="h:commandButton" value="Add" action="#{newMusician.saveNewMusician}"/>
</p>
</form>
....
And managed bean that has ArrayList of Strings:
#ManagedBean
#ViewScoped
public class NewMusician {
private ArrayList<String> strings = new ArrayList<String>();
public NewMusician() {
strings.add("olo");
}
public ArrayList<String> getStrings() {
return strings;
}
public void saveNewMusician() {
.....
}
....
}
Problem: When I change text in and press save button, in saveNewMusician() method I can see that ArrayList "strings" contain the same old value "olo", but not that one I inserted in input field.
The same problem if use h:selecOneMenu.
Situation is changed if use not string, but object that aggregate string and set value into string.
So if I'll use some POJO and change inputText to:
<h:inputText value="#{preferredMusicGenrePojo.string}" immediate="true"/>
Everything becomes Ok.
Question:
Why usage of 1 level getter <h:inputText value="#{preferredMusicGenre}"/> is incorrect, but usage of 2 level getter: <h:inputText value="#{preferredMusicGenrePojo.text}"/> is Ok?
A String is immutable. It doesn't have a setter for the value. You need to wrap this around in a bean (or POJO as you call it).
public class Musician {
private String preferredGenre;
// Add/generate constructor, getter, setter, etc.
}
Then change your managed bean as follows.
#ManagedBean
#ViewScoped
public class NewMusician {
private ArrayList<Musician> musicians = new ArrayList<Musician>();
public NewMusician() {
musicians.add(new Musician("olo"));
}
public ArrayList<Musician> getMusicians() {
return musicians;
}
public void saveNewMusician() {
// ...
}
// ...
}
And your datatable:
<h:dataTable value="#{newMusician.musicians}" var="musician">
<h:column>
<h:inputText value="#{musician.preferredGenre}" />
</h:column>
</h:dataTable>

JSF 2.0 h:selectManyListbox f:selectItems - always empty

I've got a JSF creation form using a selectManyListbox with selectItems inside (it should contain all tags available for projects). The selectItems list though is always empty - when the page renders, there's nothing in the Listbox. Still, the list in the backing bean contains 3 entries (checked that). What am I doing wrong?
The backing bean:
#ManagedBean(name = "createProjectBean")
#RequestScoped
public class CreateProjectBean {
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
private Project project;
private IProjectService projectService;
private FacesContext facesContext;
private MessageFactory mf;
private List<Tag> tags;
public CreateProjectBean() {
project = new Project();
projectService = (IProjectService)ServiceFinder.getInstance()
.findBean("projectService");
mf = new MessageFactory("properties.projects.messages");
tags = projectService.getTags();
}
/* should be the source of tags */
public void setTags(List<Tag> tags) {
this.tags = tags;
}
public List<Tag> getTags() {
return tags;
}
}
And the page:
<f:view>
<h:outputText id="error" rendered="false" />
<h:message styleClass="errorMessage" for="error" />
<h:form id="creationForm" >
<h:panelGrid columns="2" width="420">
/* blah, blah, set name and stuff */
<h:selectManyListbox id="box" value = "#{createProjectBean.project.tags}">
<f:converter converterId="tag" />
<f:selectItems value="#{createProjectBean.tags}"
var="tag"
itemValue="#{tag}"
itemLabel="${tag.name}" />
</h:selectManyListbox>
<f:verbatim><br/></f:verbatim>
<h:commandButton value="Create" styleClass="formButton" action="#{createProjectBean.create}"/>
</h:panelGrid>
</h:form>
</f:view>
I tried to do it per analogia to this page:
http://digitaljoel.wordpress.com/2010/01/11/jsf-2-custom-converter/
The converter I've written is yet to be tested.
Your EL is bogus. You should use the #{} notation everywhere. Replace
<f:selectItems value="#{createProjectBean.tags}"
var="tag"
itemValue="#{tag}"
itemLabel="${tag.name}" />
by
<f:selectItems value="#{createProjectBean.tags}"
var="tag"
itemValue="#{tag}"
itemLabel="#{tag.name}" />

Categories

Resources