我在 liferay6.1 上使用 primefaces 3.5。我有以下代码
<?xml version="1.0"?>
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head />
<h:body>
<h:form id="form">
<p:commandLink
action="/views/initialMenu.xhtml?javax.portlet.faces.PortletMode=view&javax.portlet.faces.WindowState=normal">
<h:graphicImage url="/Home.png" styleClass="basicImageStyle" />
</p:commandLink>
<br />
<br />
<p:panel header="Step 4">
<p:panel header="Align application unique settings to common terms">
<p:dataTable id="settings" var="setting"
value="#{step4Bean.settings}" rowKey="#{setting.name}"
selection="#{step4Bean.selectedSetting}" selectionMode="single"
emptyMessage="No settings found" style="width:85px;">
<p:ajax event="rowSelect" listener="#{step4Bean.onRowSelect}"
update=":form:settingDialog " oncomplete="settingDialog.show()" />
<p:column headerText="Application unique setting name">
<h:outputText value="#{setting.name}" />
</p:column>
<p:column headerText="Setting description">
<h:outputText value="#{setting.description}" />
</p:column>
<p:column headerText="Setting value space">
<h:outputText value="#{setting.valueSpace}" />
</p:column>
<p:column headerText="Default value">
<h:outputText value="#{setting.value}" />
</p:column>
<p:column headerText="Mapped to the common term">
<h:outputText value="#{setting.mapping}" />
</p:column>
<p:column headerText="Exact matching">
<p:selectBooleanCheckbox value="#{setting.exactMatching}"
disabled="true" />
</p:column>
<p:column headerText="Comments">
<h:outputText value="#{setting.comments}" />
</p:column>
</p:dataTable>
</p:panel>
<p:commandButton id="saveButton" value="Save alignment"
actionListener="#{step4Bean.saveAlignment}" />
<p:commandButton value="Previous" id="prevBut" ajax="false"
action="/views/step3View.xhtml?javax.portlet.faces.PortletMode=view&javax.portlet.faces.WindowState=normal" />
</p:panel>
<p:dialog id="settingDialog" modal="true"
header="Setting #{step4Bean.selectedSetting.name}"
widgetVar="settingDialog" resizable="false" width="500"
showEffect="clip" hideEffect="fold">
<h:panelGrid columns="2" columnClasses="label, value"
styleClass="grid">
<h:outputText value="Solution unique setting name: " />
<h:outputText value="#{step4Bean.selectedSetting.name}" />
<h:outputText value="Setting description: " />
<h:outputText value="#{step4Bean.selectedSetting.description}" />
<h:outputText value="Setting value space: " />
<h:outputText value="#{step4Bean.selectedSetting.valueSpace}" />
<h:outputText value="Setting description: " />
<h:outputText value="#{step4Bean.selectedSetting.description}" />
<h:outputText value="Default value: " />
<h:outputText value="#{step4Bean.selectedSetting.value}" />
<h:outputText value="Mapping: " />
<p:selectOneMenu value="#{step4Bean.selectedSetting.mapping}">
<f:selectItems value="#{step4Bean.selectedSetting.sortedMappings}" />
</p:selectOneMenu>
<h:outputText value="Exact matching: " />
<p:selectBooleanCheckbox
value="#{step4Bean.selectedSetting.exactMatching}">
<p:ajax update="settingComments" />
</p:selectBooleanCheckbox>
<h:outputText value="Comments: " />
<p:inputText label="settingComments" id="settingComments"
value="#{step4Bean.selectedSetting.comments}"
disabled="#{step4Bean.selectedSetting.exactMatching}">
</p:inputText>
<p:commandButton value="Propose new registry term" id="proposeBut"
oncomplete="settingDialog2.show()" />
</h:panelGrid>
<p:commandButton value="OK" id="okBut"
update=":#{p:component('form')}" oncomplete="settingDialog.hide()" />
</p:dialog>
<p:dialog id="settingDialog2" header="New Registry Term" modal="true"
widgetVar="settingDialog2" resizable="false" width="500"
showEffect="clip" hideEffect="fold">
<h:panelGrid columns="2" columnClasses="label, value"
styleClass="grid">
<h:outputText value="Enter the registry term name: " />
<p:inputText label="registryTermName" id="registryTermName"
value="#{step4Bean.selectedSetting.mapping}">
</p:inputText>
</h:panelGrid>
<p:commandButton value="OK" id="ok2But"
actionListener="#{step4Bean.addProposedTerm}"
update=":#{p:component('settingDialog')},:#{p:component('form')}"
oncomplete="settingDialog2.hide()" />
</p:dialog>
</h:form>
</h:body>
</f:view>
使用相应的 SessionScoped bean:
package org.test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.test.ontology.Ontology;
import org.test.utils.StringComparison;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import com.hp.hpl.jena.ontology.impl.IndividualImpl;
@ManagedBean(name = "step4Bean")
@SessionScoped
public class Step4Bean {
private List<String> registryItemsList = new ArrayList<String>();
private Ontology ontology;
private List<Setting> settings;
private Setting selectedSetting;
private String newRegistryTerm = "";
public Step4Bean() {
super();
try {
FacesContext context = FacesContext.getCurrentInstance();
Step1Bean step1Bean = (Step1Bean) context.getApplication()
.evaluateExpressionGet(context, "#{step1Bean}",
Step1Bean.class);
ontology = step1Bean.getOntology();
step3Bean step3Bean = (step3Bean) context.getApplication()
.evaluateExpressionGet(context, "#{step3Bean}",
step3Bean.class);
settings = step3Bean.getSettings();
} catch (Exception ex) {
ex.printStackTrace();
}
registryItemsList = ontology.getInstancesOfRegistryClass();
for (int i = 0; i < settings.size(); i++) {
Setting set = settings.get(i);
SortedMap map = new TreeMap();
for (int j = 0; j < registryItemsList.size(); j++) {
map.put(registryItemsList.get(j),
StringComparison.CompareStrings(set.getName(),
registryItemsList.get(j)));
}
SortedSet map2 = entriesSortedByValues(map);
Iterator itr = map2.iterator();
String str = "";
List<String> list = new ArrayList<String>();
while (itr.hasNext()) {
str = itr.next().toString().split("=")[0];
list.add(str);
}
set.setMapping(str);
List<String> list2 = reverseList(list);
set.setSortedMappings(list2);
}
}
private List reverseList(List myList) {
List invertedList = new ArrayList();
for (int i = myList.size() - 1; i >= 0; i--) {
invertedList.add(myList.get(i));
}
return invertedList;
}
<K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> entriesSortedByValues(
Map<K, V> map) {
SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(
new Comparator<Map.Entry<K, V>>() {
@Override
public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
int res = e1.getValue().compareTo(e2.getValue());
return res != 0 ? res : 1; // Special fix to preserve
// items with equal values
}
});
sortedEntries.addAll(map.entrySet());
return sortedEntries;
}
public List<String> getRegistryItemsList() {
return registryItemsList;
}
public void setRegistryItemsList(List<String> registryItemsList) {
this.registryItemsList = registryItemsList;
}
public Ontology getOntology() {
return ontology;
}
public void setOntology(Ontology ontology) {
this.ontology = ontology;
}
public List<Setting> getSettings() {
return settings;
}
public void setSettings(List<Setting> settings) {
this.settings = settings;
}
public Setting getSelectedSetting() {
return selectedSetting;
}
public void setSelectedSetting(Setting selectedSetting) {
this.selectedSetting = selectedSetting;
}
public String getNewRegistryTerm() {
return newRegistryTerm;
}
public void setNewRegistryTerm(String newRegistryTerm) {
this.newRegistryTerm = newRegistryTerm;
}
public void saveAlignment() {
System.out.println("SAVE");
}
public void onRowSelect(SelectEvent event) {
}
public void addProposedTerm() {
selectedSetting.getSortedMappings()
.add(0, selectedSetting.getMapping());
}
}
问题是如果表格为空或未进行选择,commandButtons 将不起作用。
我错过了什么吗?非常感谢!!!