私はPrimefacesでNetbeansを使用しています。私の問題は、インスタントを使用できないことです。行をクリックしたときに p:dialog が空であることを除いて、すべてが正常に進んでいます。
getCurrent
このメソッドが Primefaces なしでは機能するのに、Primefaces では機能しない理由がわかりません!
コードは以下のとおりです。
あなたの助けはとても感謝されます.
ありがとう、
ArabaController.java コードは次のとおりです。
@ManagedBean(name = "arabaController")
@SessionScoped
public class ArabaController implements Serializable {
private primeController mediumCarsModel;
private primeController arabaList;
private primeController getprimeAraba;
private List<Araba> carsSmall;
private ArabaController y;
private Araba current;
private Araba selectedCar;
private DataModel items = null;
private String Marka;
private String Model;
private String Renk;
private int Yil;
private DataModel sel;
public static int deneme;
@EJB
private session.ArabaFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;
public Araba getSelected() {
if (current == null) {
current = new Araba();
selectedItemIndex = -1;
}
return current;
}
private ArabaFacade getFacade() {
return ejbFacade;
}
public PaginationHelper getPagination() {
if (pagination == null) {
pagination = new PaginationHelper(20) {
@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();
System.out.println("selim");
return "List";
}
public String prepareView() {
current = (Araba) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "View";
}
public Araba getCurrent(){
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
current = (Araba) getItems().getRowData();
System.out.println(selectedItemIndex) ;
System.out.println(current);
System.out.println("current");
return current;
}
//////////////////////////////////////////////////////////////////////
public void onRowSelect(SelectEvent event) {
FacesMessage msg = new FacesMessage("Seçilen Araba", ((Araba) event.getObject()).getMarka());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void getSelectedMarka(SelectEvent event)
{
Marka=((Araba) event.getObject()).getMarka();
return;
}
public String getSelectedModel(SelectEvent event)
{
Model=((Araba) event.getObject()).getModel();
return Model;
}
public String getSelectedRenk(SelectEvent event)
{
Renk=((Araba) event.getObject()).getRenk();
return Renk;
}
public int getSelectedYil(SelectEvent event)
{
Yil=((Araba) event.getObject()).getYil();
return Yil;
}
/////////////////////////////////////////////////////////////////////////////////////
public String prepareCreate() {
current = new Araba();
selectedItemIndex = -1;
return "Create";
}
public String create() {
try {
getFacade().create(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ArabaCreated"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public String prepareEdit() {
current = (Araba) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "Edit";
}
public String update() {
try {
getFacade().edit(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ArabaUpdated"));
return "View";
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public String destroy() {
current = (Araba) 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("ArabaDeleted"));
} 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);
}
public Object getRowDataTable()
{
return sel.getRowData();
}
@FacesConverter(forClass = Araba.class)
public static class ArabaControllerConverter implements Converter {
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
ArabaController controller = (ArabaController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "arabaController");
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 Araba) {
Araba o = (Araba) object;
return getStringKey(o.getIdaraba());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + ArabaController.class.getName());
}
}
}
////////////////////////////////////////////////////////////////////////////////////
public Araba getSelectedCar() {
return selectedCar;
}
public void setSelectedCar(Araba selectedCar) {
this.selectedCar = selectedCar;
}
public primeController getMediumCarsModel() {
mediumCarsModel = new primeController(ejbFacade.findAll());
return mediumCarsModel;
}
///////////////////////////////////////////////////////////////////////////////////////
}
primecontroller.java は次のとおりです。
@ManagedBean(name = "primeController")
@SessionScoped
public class primeController extends ListDataModel<Araba> implements SelectableDataModel<Araba> {
private session.ArabaFacade x;
private Araba current;
private DataModel items = null;
@EJB
private session.ArabaFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;
public primeController() {
}
public primeController(List<Araba> data) {
super(data);
}
public List<Araba> sendAraba()
{
List<Araba> cars = x.findAll();
return cars;
}
@Override
public Araba getRowData(String rowKey) {
//In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data
List<Araba> cars = x.findAll();
System.out.print("olmalı idi");
for(Araba car : cars) {
if(car.getModel().equals(rowKey))
return car;
}
return null;
}
@Override
public Object getRowKey(Araba car) {
controller.ArabaController.deneme=3;
return car.getModel();
}
}
araba.java (モデル) コードは以下のとおりです。
@Entity
@Table(name = "araba")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Araba.findAll", query = "SELECT a FROM Araba a"),
@NamedQuery(name = "Araba.findByIdaraba", query = "SELECT a FROM Araba a WHERE a.idaraba = :idaraba"),
@NamedQuery(name = "Araba.findByMarka", query = "SELECT a FROM Araba a WHERE a.marka = :marka"),
@NamedQuery(name = "Araba.findByRenk", query = "SELECT a FROM Araba a WHERE a.renk = :renk"),
@NamedQuery(name = "Araba.findByYil", query = "SELECT a FROM Araba a WHERE a.yil = :yil"),
@NamedQuery(name = "Araba.findByModel", query = "SELECT a FROM Araba a WHERE a.model = :model")})
public class Araba implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "idaraba")
private Integer idaraba;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 15)
@Column(name = "MARKA")
private String marka;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 15)
@Column(name = "RENK")
private String renk;
@Basic(optional = false)
@NotNull
@Column(name = "YIL")
private int yil;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 15)
@Column(name = "MODEL")
private String model;
public Araba() {
}
public Araba(Integer idaraba) {
this.idaraba = idaraba;
}
public Araba(Integer idaraba, String marka, String renk, int yil, String model) {
this.idaraba = idaraba;
this.marka = marka;
this.renk = renk;
this.yil = yil;
this.model = model;
}
public Integer getIdaraba() {
return idaraba;
}
public void setIdaraba(Integer idaraba) {
this.idaraba = idaraba;
}
public String getMarka() {
return marka;
}
public void setMarka(String marka) {
this.marka = marka;
}
public String getRenk() {
return renk;
}
public void setRenk(String renk) {
this.renk = renk;
}
public int getYil() {
return yil;
}
public void setYil(int yil) {
this.yil = yil;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
@Override
public int hashCode() {
int hash = 0;
hash += (idaraba != null ? idaraba.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Araba)) {
return false;
}
Araba other = (Araba) object;
if ((this.idaraba == null && other.idaraba != null) || (this.idaraba != null && !this.idaraba.equals(other.idaraba))) {
return false;
}
return true;
}
@Override
public String toString() {
return "model.Araba[ idaraba=" + idaraba + " ]";
}
}
セッションabstractfacade.java:
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<Araba> findAll() {
// javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
//cq.select(cq.from(entityClass));
//return getEntityManager().createQuery(cq).getResultList();
CriteriaBuilder cb= getEntityManager().getCriteriaBuilder();
CriteriaQuery<Araba> cq= cb.createQuery(Araba.class);
Root<Araba> araba=cq.from(Araba.class);
cq.select(araba);
TypedQuery<Araba> q=getEntityManager().createQuery(cq);
List<Araba> tumArabalar=q.getResultList();
System.out.println("current");
return tumArabalar;
}
public List<Araba> findRange(int[] range) {
// javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
// cq.select(cq.from(entityClass));
// javax.persistence.Query q = getEntityManager().createQuery(cq);
CriteriaBuilder cb= getEntityManager().getCriteriaBuilder();
CriteriaQuery<Araba> cq= cb.createQuery(Araba.class);
Root<Araba> araba=cq.from(Araba.class);
cq.select(araba);
TypedQuery<Araba> q=getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}
arabafacade.java:
@Stateless
public class ArabaFacade extends AbstractFacade<Araba> {
@PersistenceContext(unitName = "WebApplication1PU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public ArabaFacade() {
super(Araba.class);
}
}
List.XHTML ファイル:
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="/template.xhtml">
<ui:define name="title">
<h:outputText value="#{bundle.ListArabaTitle}"></h:outputText>
</ui:define>
<ui:define name="body">
<h:form id="form" styleClass="jsfcrud_list_form">
<p:growl id="growl" showDetail="true"/>
<h:panelGroup id="messagePanel" layout="block">
<h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
</h:panelGroup>
<h:outputText escape="false" value="#{bundle.ListArabaEmpty}" rendered="#{arabaController.items.rowCount == 0}"/>
<h:panelGroup rendered="#{arabaController.items.rowCount > 0}">
<h:outputText value="#{arabaController.pagination.pageFirstItem + 1}..#{arabaController.pagination.pageLastItem + 1}/#{arabaController.pagination.itemsCount}"/>
<h:commandLink action="#{arabaController.previous}" value="#{bundle.Previous} #{arabaController.pagination.pageSize}" rendered="#{arabaController.pagination.hasPreviousPage}"/>
<h:commandLink action="#{arabaController.next}" value="#{bundle.Next} #{arabaController.pagination.pageSize}" rendered="#{arabaController.pagination.hasNextPage}"/>
<p:dataTable id="araba" var="item" value="#{arabaController.mediumCarsModel}" rowKey="#{item.idaraba}" paginator="true" rows="20"
selection="#{arabaController.selected}" selectionMode="single" >
<p:ajax event="rowSelect" listener="#{arabaController.onRowSelect}"
update=":form:display :form:growl" oncomplete="carDialog.show()" />
<p:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListArabaTitle_idaraba}"/>
</f:facet>
<h:outputText value="#{item.idaraba}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListArabaTitle_marka}"/>
</f:facet>
<h:outputText value="#{item.marka}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListArabaTitle_renk}"/>
</f:facet>
<h:outputText value="#{item.renk}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListArabaTitle_yil}"/>
</f:facet>
<h:outputText value="#{item.yil}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListArabaTitle_model}"/>
</f:facet>
<h:outputText value="#{item.model}"/>
</p:column>
</p:dataTable>
<p:dialog id="dialog" header="Car Detail" widgetVar="carDialog" resizable="false"
showEffect="explode" hideEffect="explode">
<h:panelGrid id="display" columns="2" cellpadding="4">
<h:outputText value="#{bundle.ViewArabaLabel_idaraba}"/>
<h:outputText value="#{arabaController.current.idaraba}" title="#{bundle.ViewArabaTitle_idaraba}"/>
<h:outputText value="#{bundle.ViewArabaLabel_marka}"/>
<h:outputText value="#{arabaController.current.marka}" title="#{bundle.ViewArabaTitle_marka}"/>
<h:outputText value="#{bundle.ViewArabaLabel_renk}"/>
<h:outputText value="#{arabaController.current.renk}" title="#{bundle.ViewArabaTitle_renk}"/>
<h:outputText value="#{bundle.ViewArabaLabel_yil}"/>
<h:outputText value="#{arabaController.current.yil}" title="#{bundle.ViewArabaTitle_yil}"/>
<h:outputText value="#{bundle.ViewArabaLabel_model}"/>
<h:outputText value="#{arabaController.current.model}" title="#{bundle.ViewArabaTitle_model}"/>
</h:panelGrid>
</p:dialog>
</h:panelGroup>
<br />
<h:commandLink action="#{arabaController.prepareCreate}" value="#{bundle.ListArabaCreateLink}"/>
<br />
<br />
<h:commandLink value="#{bundle.ListArabaIndexLink}" action="/index" immediate="true" />
</h:form>
</ui:define>
</ui:composition>
</html>