0

我试图通过搜索参考来获取名称。相关的方法是调用 EM 并使用键 Reference 搜索对象的 getReference()。然后我尝试获取一个返回对象的属性名称。包 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 在此 JSF 页面中被称为 ajax 函数:

<?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>

我似乎无法让这个工作。我究竟做错了什么?我正在尝试完成一个简单的任务,给定一个引用参数查找存在的实体并显示与该引用关联的名称。我已经搜索并阅读了很多文章,但我仍然没有弄清楚 JSF 和 MB 应该如何工作,我已经使用 NB 向导创建了 JPA 和 JSF+Controllers,我需要有使用多个 bean 的 JSF 页面,我有没有找到一个简单的方法来做到这一点。

4

2 回答 2

2

这里,

/**
 * @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;
}

您已经在 getter 方法中实现了“业务逻辑”。这是绝对不对的。Getter 和 setter 根本不应该包含业务逻辑。导致您的具体问题是因为从未在 ajax 请求上调用此 getter。仅调用输入的设置器。此外,您没有通过 ajax 渲染任何内容,因此最终用户根本得不到任何视觉反馈。Zak 的回答涵盖了这一点,但仅此还不够。它只会调用文本的 getter,而不是输入的 setter。

您需要改为在正常操作方法中执行业务逻辑。然后,您可以将该方法绑定到 的listener属性<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}" /> 

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;
}
于 2012-08-20T11:43:34.087 回答
1

尝试这个:

            <h:inputText value="#{armazemController.reference}" > 
                <f:ajax event="keyup" render="text"/> 
            </h:inputText> 
            <h:outputText id="text" value="#{armazemController.replyWith}" /> 

您似乎跳过了 ajax 的渲染部分。目前正在服务器上处理 inputText,但之后不会重新渲染任何内容。

于 2012-08-17T23:27:45.300 回答