Disclaimer: school project.
Hello,
I'm having a strange issue which has hold me back for days.
The project im working on is a car tracking and paying system.
I'm trying to add a web page to my project which adds a region with roads, or edits an existing region.
When adding only a region everything goes fine, The variables are taken from the form and the region is saved to the database. But when i add the code for selecting and adding the roads the buttons are not working anymore.
I found out that when i only remove the SelectOneMenu the page loads, and submits the region (withouth roads), but i couldn't find a mistake in the SelectOneMenu
I did research and found out that the problem is caused by a validation error when the button validates the form. Source 1: link
I tried to fix this problem by checking if i did anything wrong as said in source 2 but with no success:
Source 2:link
Here the body code of the 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>
And here is the code of the 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!");
}
}
}
So my exact question is: What am i doing wrong with the SelectOneMenu that makes the buttons in the form to stop working?
I'm sorry it is so much to read, Please ask if you require some extra explanation on the system.
I am using:
Netbeans 7.2.1
Glassfish 3.1.2
Primefaces 3.5
Any help/suggestion is very much appreciated :)