【发布时间】:2017-04-18 08:36:33
【问题描述】:
我有一个 JSF 页面,我可以在其中添加或删除我的数据库 (JBDC) 中的“飞机”。为了帮助解决这个问题,我有两个 bean(两个 @RequestScoped),一个“控制器”和一个“支持 bean”。
这里是 JSF 页面:
还有一些输入:
输入信息并添加照片后,“飞机”被添加到我的页面,我重新加载页面。
这是提交后页面的外观:
我的问题是,输入文本字段从 bean 中“填充”了信息,并且删除平面字段也填充了飞机 ID。我希望这些字段在刷新后为空,这就是为什么我认为使用 @RequestScoped 会很有用。此外,如果我尝试从网络浏览器刷新页面,它只会尝试重新发送表单。
如何避免使用 bean 中的数据填充字段?
另外,这里是 bean 代码:
@Named
@RequestScoped
public class AddAirplaneCtrl implements Serializable{
@Inject
private FlightModel flightModel;
private ListAirplaneBB listAirplaneBB;
protected AddAirplaneCtrl(){
;
}
@Inject
public void addListAirplaneBB(ListAirplaneBB listAirplaneBB){
this.listAirplaneBB = listAirplaneBB;
}
public String addPlane(){
listAirplaneBB.setError(null);
listAirplaneBB.setMessage(null);
if(failed any of the validation parts){
return /some/site?faces-redirect=false";
}
//add plane
return /some/site?faces-redirect=true";
}
}
还有支持 Bean:
@Named
@RequestScoped
public class ListAirplaneBB implements Serializable{
@Inject
private FlightModel flightModel;
//private variable fields
public List<Airplane> getAllPlanes(){
return flightModel.getAirplaneList().findAll();
}
public int getTotalPlanes(){
return getAllPlanes().size();
}
//GETTERS and SETTERS
}
最后是我的 jsf 页面:
<div class="contbox">
<h3>
<h:outputLabel value="Edit Planes" />
</h3>
<c:if test="#{not empty listAirplaneBB.error}">
<div class="alert alert-danger"
id="success-alert">
<span class="glyphicon glyphicon-remove" />
<h:outputText value="#{listAirplaneBB.error}" />
<button type="button"
class="close"
data-dismiss="alert">
<h:outputLabel value="x" />
</button>
</div>
</c:if>
<c:if test="#{not empty listAirplaneBB.message}">
<div class="alert alert-success">
<span class="glyphicon glyphicon-remove" />
<h:outputText value="#{listAirplaneBB.message}" />
<button type="button"
class="close"
data-dismiss="alert">
<h:outputLabel value="x" />
</button>
</div>
</c:if>
<h4>Add A Plane</h4>
<h:form enctype="multipart/form-data">
<table id="addtable"
class="table">
<thead>
<tr>
<th>
<h:outputLabel value="Plane Make" />
</th>
<th colspan="1">
<h:outputLabel value="Plane Model" />
</th>
<th colspan="1">
<h:outputLabel value="Plane Seats" />
</th>
<th colspan="1">
<h:outputLabel value="Plane Photo" />
</th>
</tr>
</thead>
<tr>
<td>
<h:inputText value="#{listAirplaneBB.make}">
</h:inputText>
</td>
<td>
<h:inputText value="#{listAirplaneBB.model}">
</h:inputText>
</td>
<td>
<h:inputText value="#{listAirplaneBB.seats}">
</h:inputText>
</td>
<td>
<h:inputFile value="#{listAirplaneBB.file}" >
</h:inputFile>
</td>
<td>
<h:commandButton value="Add Plane"
class="btn btn-primary"
action="#{addAirplaneCtrl.addPlane()}" >
</h:commandButton>
</td>
</tr>
</table>
</h:form>
<h4>Delete Plane</h4>
<h:form>
<h:panelGrid columns="3" class="table">
<h:outputLabel value="Plane ID" />
<h:inputText value="#{listAirplaneBB.airPlaneId}"/>
<h:commandButton value="Delete"
class="btn btn-primary"
action="#{addAirplaneCtrl.deleteAirplane}" >
</h:commandButton>
</h:panelGrid>
</h:form>
</div>
【问题讨论】:
-
@BalusC 你有什么可以提供帮助的信息吗?
标签: jsf primefaces jsf-2