Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8419657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:40:28+00:00 2026-06-10T02:40:28+00:00

I am trying to get the name by searching for a reference. The pertinent

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:40:29+00:00Added an answer on June 10, 2026 at 2:40 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the process name in android by using the method
Im trying to get the name of a user by looking in up with
I'm trying to get my name and trip input fields to line, but they're
I am trying to get the name of a file when I select it
I had been trying to get the tag name and its value in java
I'm trying to get the instance name of my class. The way I do
I'm very new to c++ and boost. I'm trying to get the host name
Im trying to get some checkbox with a specific name document.getElementsByName(test); Unfortunatley i cant
Ok I am trying to get the users First Name the form gets their
In C++ with .NET, I'm trying to get the IP address or interface name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.