免责声明:学校项目。
您好,
我遇到了一个奇怪的问题,困扰了我好几天。
我正在做的项目是一个汽车跟踪和支付系统。
我正在尝试向我的项目中添加一个网页,该网页添加了一个带有道路的区域,或者编辑了一个现有的区域。
仅添加一个区域时一切正常,变量取自表单并将区域保存到数据库中。但是当我添加用于选择和添加道路的代码时,按钮不再起作用。
我发现当我只删除 SelectOneMenu 页面加载并提交区域(没有道路)时,但我在 SelectOneMenu 中找不到错误
我做了研究,发现问题是由按钮验证表单时的验证错误引起的。来源1:链接
我试图通过检查我是否像源 2 中所说的那样做错任何事情来解决这个问题,但没有成功:
源 2:链接
这里是 AddRegion.xhtml 的正文代码:
<body>
<ui:composition template="./Templates/AdministrationTemplate.xhtml">
<!-- side menu -->
<ui:define name="menu">
<ui:include src="./Templates/RegionMenu.xhtml"/>
</ui:define>
<ui:define name="content">
<f:metadata>
<!-- Region id for editing an existing region -->
<f:viewParam name="regionid" value="#{addRegionBean.regionid}" required="false"/>
</f:metadata>
<h:form>
<table>
<tr>
<td>
<p:outputLabel value="Naam: "/>
</td>
<td>
<p:inputText value="#{addRegionBean.name}"/>
</td>
</tr>
<tr>
<td>
<p:outputLabel value="Start datum: "/>
</td>
<td>
<p:calendar value="#{addRegionBean.startDate}" showOn="button"/>
</td>
</tr>
<tr>
<td>
<p:outputLabel value="Tarief: "/>
</td>
<td>
<p:inputText value="#{addRegionBean.rate}"/>
</td>
</tr>
<tr>
<td>
<p:outputLabel value="Tarief type: "/>
</td>
<td>
<p:selectOneMenu value="#{addRegionBean.rateType}">
<f:selectItems value="#{addRegionBean.rateList}"/>
</p:selectOneMenu>
<h:outputText value="<br/>" escape="false" />
</td>
</tr>
<tr>
<td>
</td>
<td><p:outputLabel value="Wegen: "/>
<p:dataTable value="#{addRegionBean.roads}" var="road">
<p:column headerText="Regio ID + naam" >
<p:commandLink value="#{road.roadID}"/>
</p:column>
<p:column headerText="Lengte" >
<p:outputLabel value="#{road.distance}"/>
</p:column>
<p:column headerText="Maximum snelheid" >
<p:outputLabel value="#{road.maxSpeed}"/>
</p:column>
</p:dataTable>
</td>
<td>
<!-- This is where it goes wrong, when only the SelectOneMenu is removed, the buttons work -->
<p:outputLabel value="Voeg een weg toe: " rendered="#{addRegionBean.askAddview()}" /> <br/>
<p:selectOneMenu converter="roadConverter" value="#{addRegionBean.selectedRoad}" rendered="#{addRegionBean.askAddview()}">
<f:selectItems value="#{addRegionBean.giveAllRoads()}" var="selRoad" itemLabel="#{selRoad.roadID}" itemValue="#{selRoad}"/>
</p:selectOneMenu> <br/>
<p:commandButton value="Voeg toe" action="#{addRegionBean.addRoadToRegion}" rendered="#{addRegionBean.askAddview()}"/>
</td>
</tr>
<tr>
<td>
<p:commandButton value="#{addRegionBean.submitSort}" action="#{addRegionBean.addRegion()}" />
</td>
<td></td>
</tr>
</table>
</h:form>
</ui:define>
</ui:composition>
</body>
这是 Bean 的代码:
package BillingAdministrationSystem.beans;
import Common.Administration.Enum.ERateType;
import Common.Administration.IRegion;
import Common.Communication.Interfaces.IRegionService;
import Common.Communication.Interfaces.IRoadService;
import Common.TrafficManagement.IRoad;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named(value = "addRegionBean")
@RequestScoped
public class AddRegionBean implements Serializable {
private double rate;
private ERateType rateType;
private Date startDate;
private long regionid;
private String name;
private IRegion region = null;
private List<IRoad> allRoads;
private List<IRoad> roads = new ArrayList<IRoad>();
private IRoad selectedRoad = null;
@EJB
private IRegionService service;
@EJB
private IRoadService roadService;
@PostConstruct
public void init() {
rate = 0;
rateType = ERateType.CORDON;
startDate = new Date();
InitAllRoads();
}
/*
* Adds or changes a region. called in the xhtml.
*/
public void addRegion() {
if (region != null) {
ChangeRegion();
} else {
service.addRegion(name, rate, rateType, startDate, roads);
//region = service.getRegion(regionid);
}
}
/**
* Saves changes to the region.
*/
public void ChangeRegion() {
service.changeBaseRate(regionid, rate, rateType, startDate);
}
/**
* Gets the text for the submit button.
* @return returns "toevoegen" if this is a new region and "opslaan" if it is a region change.
*/
public String getSubmitSort() {
if (askAddview()) {
return "Toevoegen";
} else {
return "Opslaan";
}
}
/**
*
* @return Returns true if the region is loaded and thus is not a new region.
*/
public boolean askAddview() {
return (region == null ? true : false);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
/**
* Gets a list of the possible rate sorts.
*
* @return List with "Cordon" and "Corridor" in it.
*/
public List<String> getRateList() {
List<String> strings = new ArrayList<String>();
strings.add("Cordon");
strings.add("Corridor");
return strings;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public String getRateType() {
return rateType.toString();
}
public void setRateType(String rateType) {
if (rateType.equals("Cordon")) {
this.rateType = ERateType.CORDON;
} else if (rateType.equals("Corridor")) {
this.rateType = ERateType.CORRIDOR;
} else {
System.out.println("Could not find rateType!");
}
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public String getRegionid() {
return regionid + "";
}
public void setRegionid(String regionid) {
this.regionid = Long.parseLong(regionid);
System.out.println("Region ID is: " + regionid);
setallFields();
}
/**
* Sets the fields of the region if a region which must be edited is
* specified.
*/
private void setallFields() {
if (regionid != 0) {
region = service.getRegion(regionid);
rate = region.getCurrentBaseRate().getBaseRateID();
startDate = region.getCurrentBaseRate().getStartDate();
rateType = region.getCurrentBaseRate().getType();
roads = region.getRoads();
name = region.getRegionName();
}
}
public List<IRoad> getRoads() {
return roads;
}
public void setRoads(List<IRoad> roads) {
this.roads = roads;
}
public IRoad getSelectedRoad() {
if (selectedRoad != null) {
System.out.println("Getting selected road " + selectedRoad.getRoadID());
} else {
System.out.println("Getting selected road which is NULL.");
if (allRoads != null) {
selectedRoad = (IRoad) allRoads.get(0);
} else{
System.out.println("SelectedRoad and allRoads are NULL.");
}
}
return selectedRoad;
}
public void setSelectedRoad(IRoad selectedRoad) {
System.out.println("Setting selected road " + selectedRoad.getRoadID());
this.selectedRoad = selectedRoad;
}
/**
* Gives all available roads.
*
* @return all roads in the system.
*/
public List<IRoad> giveAllRoads() {
return allRoads;
}
/**
* fills the allRoads variable with all the roads reachable by the roadService.
*/
private void InitAllRoads()
{
allRoads = roadService.getRoads();
}
/**
* Adds the currently selected road to the region.
*/
public void AddRoadToRegion() {
if (selectedRoad != null) {
System.out.println("Adding road " + selectedRoad.getRoadID());
roads.add(selectedRoad);
} else {
System.out.println("Cannot add road to region, selectedRoad is NULL!");
}
}
}
所以我的确切问题是:我在使表单中的按钮停止工作的 SelectOneMenu 上做错了什么?
很抱歉读了这么多,请问您是否需要对系统进行一些额外的解释。
我正在使用:
Netbeans 7.2.1
Glassfish 3.1.2
Primefaces 3.5
非常感谢任何帮助/建议:)