gpt4 book ai didi

java - 通过 EntityManager 查找在 JSF 页面上使用 AJAX 设置标签

转载 作者:行者123 更新时间:2023-12-02 00:14:58 26 4
gpt4 key购买 nike

我正在尝试通过搜索引用来获取名称。相关方法是 getReference(),它调用 EM 并搜索带有关键字 Reference 的对象。然后,我尝试获取返回对象的属性名称。 封装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());
}
}
}
}

在此 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 页面,我有没有找到一个简单的方法来做到这一点。

最佳答案

这里,

/**
* @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 根本不应该包含业务逻辑。你的具体问题是因为这个 getter 永远不会在 ajax 请求上调用。仅调用输入的 setter 。另外,您没有通过 ajax 渲染任何内容,因此最终用户根本得不到任何视觉反馈。扎克的答案涵盖了这一点,但仅此还不够。它只会调用文本的 getter,而不调用输入的 setter。

您需要在正常的操作方法中执行业务逻辑。然后您可以将该方法绑定(bind)到 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;
}

关于java - 通过 EntityManager 查找在 JSF 页面上使用 AJAX 设置标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12014003/

26 4 0