问题描述
有没有办法将整个List 作为f:param传递?
我有两个通过双向OnetoMany / ManyToOne关系连接的数据库。首先,我具有元素及其属性(名称除外),它通过id与第二个数据库连接,第二个数据库使用不同的语言命名(每种语言都有其自己的id。
我还有两页。首先,我在第一个数据库中搜索符合条件的元素。结果显示在数据表中,最后一列带有“编辑”按钮。
当您单击该行中的“编辑”按钮时,它将在第二页上将您发送给您,该页面上应显示两个数据库中该元素的属性。
使用f:param可以将id发送到其他页面,但是在页面上尚未设置id的加载值,因此无法从第二个数据库加载值(我得到nullpointerexception)。如果我将ID设置为静态。它会加载,但始终相同。
我使用postgresql,primefaces和Wildfly17。如果我忘了提些什么,请告诉我。
我的代码:
SmpestEditable.xhtml:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<f:Metadata>
<f:viewParam name="id" value="#{smpestsEditable.id}" />
<f:viewParam name="code" value="#{smpestsEditable.code}" />
<f:viewParam name="lname" value="#{smpestsEditable.latinName}" />
<f:viewParam name="irecog" value="#{smpestsEditable.imgRecognition}" />
<f:viewAction action="#{smpestsEditable.onload}" />
</f:Metadata>
<h:messages />
<h:head></h:head>
<h:body>
<style type="text/css">
.ui-row-editor .ui-row-editor-pencil {
margin-left:8px;
}
</style>
<h:form id="pestsListEdit">
<p:growl id="msgs" showDetail="true" />
<p:panel id="Searchpest" header="EDIT pest" toggleable="true"
closable="true" toggleSpeed="500" closeSpeed="500" widgetvar="panel"
style="margin-bottom:20px">
<p:panelGrid columns="2">
<h:outputLabel for="Id" value="Id" />
<h:outputText value="#{request.getParameter('id')}"/>
<h:outputLabel for="slo" value="lo" />
<h:outputText value="#{request.getParameter('id')}"/>
<h:outputLabel for="Code" value="Code" />
<p:inputText id="Code" value="#{smpestsEditable.code}" label="Code" />
<h:outputLabel for="lnme" value="Latin Name" />
<p:inputText id="LatinName" value="#{smpestsEditable.latinName}" label="latin" />
<f:facet name="footer">
<p:commandButton value="CHECK"
actionListener = "#{smpestsEditable.saveButton}"
action = "SmpestsList?faces-redirect-true" />
<p:linkButton outcome="SmpestsList" value="CLOSE"
style="margin-right:20px;">
</p:linkButton>
</f:facet>
</p:panelGrid>
</p:panel>
<p:dataTable var="ime" value="#{smpestsEditable.imena}">
<p:column headerText="ID pest NAME">
<h:outputText value="#{ime.idpestName}" />
</p:column>
<p:column headerText="LANGUAGE">
<h:outputText value="Srpski"
rendered="#{ime.idLanguage eq 12}" />
<h:outputText value="Français"
rendered="#{ime.idLanguage eq 13}" />
<h:outputText value="Italiano"
rendered="#{ime.idLanguage eq 8}" />
<h:outputText value="English"
rendered="#{ime.idLanguage eq 4}" />
<h:outputText value="Português"
rendered="#{ime.idLanguage eq 9}" />
<h:outputText value="Deutsch"
rendered="#{ime.idLanguage eq 11}" />
<h:outputText value="Magyar"
rendered="#{ime.idLanguage eq 10}" />
<h:outputText value="Português"
rendered="#{ime.idLanguage eq 14}" />
<h:outputText value="Yкраїнський"
rendered="#{ime.idLanguage eq 15}" />
</p:column>
<p:column headerText="disPLAY NAME">
<p:inplace id="name">
<p:inputText value="#{ime.name}" label="Name" />
</p:inplace>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
SmpestEditable.java:
package si.david;
import java.sql.sqlException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.postconstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.annotation.ManagedProperty;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.FieldResult;
import javax.persistence.Query;
import javax.persistence.sqlresultsetmapping;
import javax.persistence.TypedQuery;
import si.david.Smpests;
import si.david.SmpestsNames;
import org.hibernate.annotations.CreationTimestamp;
import org.junit.Before;
import org.primefaces.event.RowEditEvent;
@Named(value="smpestsEditable")
@RequestScoped
public class SmpestsEditable {
private List<SmpestsNames> pests = new ArrayList<SmpestsNames>();
private EntityManager em =null;
private EntityManagerFactory emf = Persistence.createEntityManagerFactory("tvTutorialPU");
private int stevilka;
private int id;
private int idpest=55;
private String sloName;
Smpests zuzek1=loadpestById(idpest);
private String code = zuzek1.getCode();
private String oldCode = code;
private String latinName = zuzek1.getLatinName();
private String oldLatinName = latinName;
private String refLink = zuzek1.getRefLink();
private String oldRefLink = refLink;
private Integer imgRecognition = zuzek1.getImgRecognition();
private Integer oldImgRecognition = imgRecognition;
private Integer hidescore = zuzek1.getHidescore();
private Integer oldHidescore = hidescore;
private Integer recognitionModel = zuzek1.getRecognitionModel();
private Integer oldRecognitionModel = recognitionModel;
private Double alarmThresholdUp = zuzek1.getAlarmThresholdUp();
private Double oldAlarmThresholdUp = alarmThresholdUp;
private Double alarmThresholdLow = zuzek1.getAlarmThresholdLow();
private Double oldAlarmThresholdLow = alarmThresholdLow;
List<SmpestsNames> imena = zuzek1.getSmpestsNames();
private Long idpestName;
private int idLanguage;
private String name;
private LocalDateTime dInsert;
private Long idInserter;
private LocalDateTime dUpdate;
private Long idUpdater;
Smpests zuzek;
public void onload() {
try {
System.out.println(" ONLOAD: id "+id);
System.out.println(" idpest "+idpest);
} catch (Exception e) {
System.out.println(" Can’t find id!");
}
}
public void saveButton() {
System.out.println("SAVE button");
try {
pestsNames(id);
System.out.println("Sprintano pestnames(id)");
} catch (Exception e) {
System.out.println("Ujej!");
}
}
public void pestsNames(int id) {
EntityManager em =null;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("tvTutorialPU");
em = emf.createEntityManager();
Smpests zuzek = em.find(Smpests.class,id);
try {
List<SmpestsNames> imena = zuzek.getSmpestsNames();
for(SmpestsNames s : imena) {
System.out.print(s.getIdpestName());
System.out.print(" | "+s.getIdLanguage());
System.out.println(" | "+s.getName());}
} catch (Exception e) {
System.out.println("Ujej!");
}
}
private Smpests loadpestById(int id) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("tvTutorialPU");
EntityManager em = emf.createEntityManager();
Smpests zuzek = em.find(Smpests.class,id);
return zuzek;
}
}
SmpestList.xhtml:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<style type="text/css">
.ui-row-editor .ui-row-editor-pencil {
margin-left:8px;
}
</style>
<h:form id="pestsList">
<p:growl id="msgs" showDetail="true" />
<p:panel id="Searchpest" header="Search for pests" toggleable="true"
closable="true" toggleSpeed="500" closeSpeed="500" widgetvar="panel"
style="margin-bottom:20px">
<p:panelGrid columns="2">
<h:outputLabel for="Code" value="Code" />
<p:inputText id="Code" value="#{smpestsList.code}" label="Code" />
<h:outputLabel for="Latin" value="Latin Name" />
<p:inputText id="Latin" value="#{smpestsList.latinName}"
label="Latin name" />
<h:outputLabel for="Recog" value="Image Recognition" />
<p:selectOneMenu id="selectOne">
<f:selectItem itemLabel="" itemValue="" />
<f:selectItem itemLabel="SEGMENTATION" itemValue="1899" />
<f:selectItem itemLabel="R-CNN" itemValue="1900" />
<f:selectItem itemLabel="NONE - copY MARKINGS" itemValue="1951" />
</p:selectOneMenu>
<f:facet name="footer">
<p:commandButton value="Search"
action="#{smpestsList.searchButton}" icon="ui-icon-check"
update=":pestsList:pestsTbl" />
<p:commandButton value="Search and Izpis"
action="#{smpestsList.pestsNames}" icon="ui-icon-check"
update=":pestsList:pestsTbl" />
<p:commandButton value="Search and Izpis arg"
action="#{smpestsList.pestsName(74)}" icon="ui-icon-check"
update=":pestsList:pestsTbl" />
<p:linkButton outcome="SmpestsEdit" value="Add"
style="margin-right:20px;">
</p:linkButton>
</f:facet>
</p:panelGrid>
</p:panel>
<p:dataTable id="pestsTbl" var="zuzek" value="#{smpestsList.pests}" editable="true"
style="margin-bottom:40px">
<f:facet name="header">
pestS
</f:facet>
<p:column headerText="Id">
<h:outputText value="#{zuzek.idpest}" />
</p:column>
<p:column headerText="Code">
<h:outputText value="#{zuzek.code}" />
</p:column>
<p:column headerText="Latin Name">
<h:outputText value="#{zuzek.latinName}" />
</p:column>
<p:column>
<p:linkButton outcome="SmpestsEditable" update="pestsListEdit" value="Edit" style="font-size:3em" title="edit">
<f:param name="id" value="#{zuzek.idpest}" />
<f:param name="code" value="#{zuzek.code}" />
<f:param name="lname" value="#{zuzek.latinName}" />
</p:linkButton>
<p:button outcome="SmpestsEditable" icon="pi pi-trash" style="margin-right:5px;" title="delete">
<f:param name="id" value="30" />
</p:button>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
SmpestList.java:
package si.david;
import java.sql.sqlException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.FieldResult;
import javax.persistence.Query;
import javax.persistence.sqlresultsetmapping;
import javax.persistence.TypedQuery;
import si.david.Smpests;
import si.david.SmpestsNames;
import org.hibernate.annotations.CreationTimestamp;
import org.junit.Before;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;
@Named(value="smpestsList")
@RequestScoped
public class SmpestsList {
private List<Smpests> pests = new ArrayList<Smpests>();
private EntityManager em =null;
private EntityManagerFactory emf = Persistence.createEntityManagerFactory("tvTutorialPU");
private int idpest;
private String latinName;
private String code;
private String srpname;
private Long srpId;
private String fraName;
private Long fraId;
private String itaName;
private Long itaId;
private String engName;
private Long engId;
private String espName;
private Long espId;
private String deuName;
private Long deuId;
private String magName;
private Long magId;
private String porName;
private Long porId;
private String ukrName;
private Long ukrId;
private List<SmpestsNames> smpestsNames = new ArrayList<SmpestsNames>();
public void searchButton() {
EntityManager em =null;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("tvTutorialPU");
em = emf.createEntityManager();
String query;
Smpests zuzek1 = new Smpests();
int parametri = 0;
if(!code.isEmpty()) {
parametri++;
}
if(!latinName.isEmpty()) {
parametri++;
}
System.out.println("parametri:" + parametri);
query ="SELECT disTINCT s FROM Smpests s";
if( getCode() != null && !"".equals(getCode()) ) {
parametri ++;
}
if( getLatinName() != null && !"".equals(getLatinName()) ) {
parametri ++;
}
if(parametri >0) {
query = query + " WHERE ";
if( getCode() != null && !"".equals(getCode()) ) {
parametri --;
query = query +"(code LIKE UPPER('%" + getCode() + "%') OR code LIKE UPPER('" + getCode() + "%'))";
}
if( getCode() != null && !"".equals(getCode()) && getLatinName() != null && !"".equals(getLatinName()) )
query = query + " AND ";
if( getLatinName() != null && !"".equals(getLatinName()) ) {
parametri --;
query = query + "UPPER(latinName) LIKE '" + getLatinName() + "'";
}
}
System.out.println("query: " + query);
try {
List<Smpests> results = em.createquery(query,Smpests.class).getResultList();
for(Smpests s : results) {
Smpests zuzek = new Smpests();
zuzek.setIdpest(s.getIdpest());
zuzek.setCode(s.getCode());
zuzek.setLatinName(s.getLatinName());
try {
List<SmpestsNames> imena = zuzek.getSmpestsNames();
for(SmpestsNames t : imena) {
System.out.print(t.getIdpestName());
System.out.print(" | "+t.getIdLanguage());
System.out.println(" | "+t.getName());
}
} catch (Exception e) {
System.out.println("Oops");
}
pests.add(zuzek);
}} catch (Exception e) {
System.out.println("doesn’t work”);
}
}
public void izpisime() {
System.out.println("pest data: ");
}
public void pestsName(int id) {
EntityManager em =null;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("tvTutorialPU");
em = emf.createEntityManager();
String query;
Smpests zuzek = em.find(Smpests.class,id);
query ="SELECT disTINCT s FROM SmpestNames s";
try {
List<SmpestsNames> imena = zuzek.getSmpestsNames();
for(SmpestsNames s : imena) {
if(s.getIdLanguage()==1) {
sloId=s.getIdpestName();
sloName=s.getName();
}
System.out.print(s.getIdpestName());
System.out.print(" | "+s.getIdLanguage());
System.out.println(" | "+s.getName());}
} catch (Exception e) {
System.out.println("pesetsName(int id) doesn’t work");
}
}
private Smpests loadpestById(int id) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("tvTutorialPU");
EntityManager em = emf.createEntityManager();
Smpests zuzek = em.find(Smpests.class,id);
System.out.println("naslooo");
try {
System.out.print(zuzek.getIdpest());
System.out.print(" | "+zuzek.getCode());
System.out.print(" | "+zuzek.getLatinName());
System.out.print(" | "+zuzek.getImgRecognition());
} catch (Exception e) {
System.out.println("Can’t fin pest id: " + id);
}
return zuzek;
}
private SmpestsNames loadNamesById(int id) {
System.out.println("loading pest");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("tvTutorialPU");
EntityManager em = emf.createEntityManager();
SmpestsNames imena = em.find(SmpestsNames.class,id);
System.out.println("naslooo");
try {
System.out.print(imena.getIdpestName());
} catch (Exception e) {
System.out.println("Ni bla- ja!");
}
return imena;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)