参照を検索して名前を取得しようとしています。適切なメソッドは、EMを呼び出し、キーReferenceを使用してオブジェクトを検索するgetReference()です。次に、返されたオブジェクトのプロパティ名を1つ取得しようとします。パッケージ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 +Controllerを作成しました。複数のBeanを使用するJSFページが必要です。これを行う簡単な方法が見つかりませんでした。