免責事項:学校のプロジェクト。
こんにちは、
私は奇妙な問題を抱えており、何日も私を妨げています。
私が取り組んでいるプロジェクトは、車の追跡と支払いシステムです。
道路のある地域を追加する、または既存の地域を編集する Web ページをプロジェクトに追加しようとしています。
領域のみを追加すると、すべてうまくいきます。変数はフォームから取得され、領域がデータベースに保存されます。しかし、道路を選択して追加するためのコードを追加すると、ボタンが機能しなくなります。
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
どんな助け/提案も大歓迎です:)