@RequestScoped Bean 没有按预期工作? (日本联合会)
@RequestScoped Bean not working as expected? (JSF)
我有一个 JSF 页面,我可以在其中添加或删除我的数据库 (JBDC) 中的 "planes"。为了解决这个问题,我有两个 bean(都是@RequestScoped),一个 "controller" 和一个 "backing bean"。
这是 JSF 页面:
还有一些输入:
输入信息并添加照片后,"airplane" 已添加到我的页面,我重新加载页面。
页面提交后的样子如下:
我的问题是,输入文本字段从带有信息的 bean 获取 "populated",并且删除飞机字段也填充了飞机 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>
您的浏览器缓存输入。
JSF 2.2 解决方案
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
...
<f:passThroughAttribute name="autocomplete" value="off"/>
...
jQuery 解法:
<h:form styleClass="form">...</h:form>
<script>
$(function(){
$(".form").attr("autocomplete", "off");
});
</script>
您可以找到更多信息here。
我有一个 JSF 页面,我可以在其中添加或删除我的数据库 (JBDC) 中的 "planes"。为了解决这个问题,我有两个 bean(都是@RequestScoped),一个 "controller" 和一个 "backing bean"。
这是 JSF 页面:
还有一些输入:
输入信息并添加照片后,"airplane" 已添加到我的页面,我重新加载页面。
页面提交后的样子如下:
我的问题是,输入文本字段从带有信息的 bean 获取 "populated",并且删除飞机字段也填充了飞机 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>
您的浏览器缓存输入。
JSF 2.2 解决方案
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
...
<f:passThroughAttribute name="autocomplete" value="off"/>
...
jQuery 解法:
<h:form styleClass="form">...</h:form>
<script>
$(function(){
$(".form").attr("autocomplete", "off");
});
</script>
您可以找到更多信息here。