Reputation: 7041
I am trying to get the name by searching for a reference. The pertinent method is getReference() that calls the EM and searched for an Object with the key Reference. I then try to get one fo the proprieties Name of the returned object. package MBJSFControllers;
import entities.Armazem;
import MBJSFControllers.util.JsfUtil;
import MBJSFControllers.util.PaginationHelper;
import SessionBeans.ArmazemFacade;
import java.io.Serializable;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import javax.persistence.EntityManager;
@ManagedBean(name = "armazemController")
@SessionScoped
public class ArmazemController implements Serializable {
private int Reference;
private String replyWith;
private Armazem current;
private DataModel items = null;
@EJB
private SessionBeans.ArmazemFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;
public ArmazemController() {
}
public Armazem getSelected() {
if (current == null) {
current = new Armazem();
selectedItemIndex = -1;
}
return current;
}
private ArmazemFacade getFacade() {
return ejbFacade;
}
public PaginationHelper getPagination() {
if (pagination == null) {
pagination = new PaginationHelper(10) {
@Override
public int getItemsCount() {
return getFacade().count();
}
@Override
public DataModel createPageDataModel() {
return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
}
};
}
return pagination;
}
public String prepareList() {
recreateModel();
return "List";
}
public String prepareView() {
current = (Armazem) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "View";
}
public String prepareCreate() {
current = new Armazem();
selectedItemIndex = -1;
return "Create";
}
public String create() {
try {
getFacade().create(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ArmazemCreated"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public String prepareEdit() {
current = (Armazem) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "Edit";
}
public String update() {
try {
getFacade().edit(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ArmazemUpdated"));
return "View";
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public String destroy() {
current = (Armazem) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
performDestroy();
recreatePagination();
recreateModel();
return "List";
}
public String destroyAndView() {
performDestroy();
recreateModel();
updateCurrentItem();
if (selectedItemIndex >= 0) {
return "View";
} else {
// all items were removed - go back to list
recreateModel();
return "List";
}
}
private void performDestroy() {
try {
getFacade().remove(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ArmazemDeleted"));
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
}
}
private void updateCurrentItem() {
int count = getFacade().count();
if (selectedItemIndex >= count) {
// selected index cannot be bigger than number of items:
selectedItemIndex = count - 1;
// go to previous page if last page disappeared:
if (pagination.getPageFirstItem() >= count) {
pagination.previousPage();
}
}
if (selectedItemIndex >= 0) {
current = getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0);
}
}
public DataModel getItems() {
if (items == null) {
items = getPagination().createPageDataModel();
}
return items;
}
private void recreateModel() {
items = null;
}
private void recreatePagination() {
pagination = null;
}
public String next() {
getPagination().nextPage();
recreateModel();
return "List";
}
public String previous() {
getPagination().previousPage();
recreateModel();
return "List";
}
public SelectItem[] getItemsAvailableSelectMany() {
return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
}
public SelectItem[] getItemsAvailableSelectOne() {
return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
}
/**
* @return the Reference
*/
public int getReference() {
if(Reference > 0){
replyWith = "Duck";
}else{
replyWith = "Fruck";
}
EntityManager em;
Armazem find = em.find(Armazem.class, Reference);
replyWith = find.getNome();
return Reference;
}
/**
* @param Reference the Reference to set
*/
public void setReference(int Reference) {
this.Reference = Reference;
}
@FacesConverter(forClass = Armazem.class)
public static class ArmazemControllerConverter implements Converter {
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
ArmazemController controller = (ArmazemController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "armazemController");
return controller.ejbFacade.find(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuffer sb = new StringBuffer();
sb.append(value);
return sb.toString();
}
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Armazem) {
Armazem o = (Armazem) object;
return getStringKey(o.getIdarmazem());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + ArmazemController.class.getName());
}
}
}
}
Get reference is being called as an ajax function in this JSF page:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Hello from Facelets
<p>Numero de armazens</p>
<h:form>
<h:panelGrid>
<h:inputText value="#{armazemController.reference}" >
<f:ajax event="keyup"/>
</h:inputText>
<h:outputText id="text" value="#{armazemController.replyWith}" />
</h:panelGrid>
</h:form>
</h:body>
</html>
I can't seem to get this working. What am I doing wrong? I am trying to accomplish a simple task, given a reference parameter lookup the entities that exist and display the name associated with that reference. I have searched and read many articles but I still have not figured out how JSF and MB are supposed to work, I have created JPA and JSF+Controllers using NB wizards, I need to have JSF pages that use more than one bean, I have not found a simple way to do this.
Upvotes: 0
Views: 446
Reputation: 1108722
Here,
/**
* @return the Reference
*/
public int getReference() {
if(Reference > 0){
replyWith = "Duck";
}else{
replyWith = "Fruck";
}
EntityManager em;
Armazem find = em.find(Armazem.class, Reference);
replyWith = find.getNome();
return Reference;
}
you have implemented the "business logic" in the getter method. This is absolutely not right. Getters and setters should not contain business logic at all. Your concrete problem is caused because this getter is never called on ajax request. Only the setter of the input is called. Also, you are not rendering anything by ajax, so the enduser get no visual feedback at all. The answer of Zak covers that, but this alone is not sufficient. It would only call the getter of the text, not the setter of the input.
You need to perform the business logic in a normal action method instead. You can then bind that method to the listener
attribute of <f:ajax>
.
<h:inputText value="#{armazemController.reference}" >
<f:ajax event="keyup" listener="#{armazemController.referenceOnkeyup}" render="text" />
</h:inputText>
<h:outputText id="text" value="#{armazemController.replyWith}" />
with
private int reference;
private String replyWith;
public void referenceOnkeyup() {
replyWith = em.find(Armazem.class, Reference).getNome();
}
public int getReference() {
return reference;
}
public void setReference(int reference) {
this.reference = reference;
}
public String getReplyWith() {
return replyWith;
}
Upvotes: 2
Reputation: 327
Try this:
<h:inputText value="#{armazemController.reference}" >
<f:ajax event="keyup" render="text"/>
</h:inputText>
<h:outputText id="text" value="#{armazemController.replyWith}" />
You seem to skipping the render part of ajax. Currently the inputText is being processed on the server but nothing gets re-rendered afterwards.
Upvotes: 1