我正在开展一个学校项目,我有一个前端项目和一个后端项目。 在我的前端项目中,我使用的是Bootsfaces 1.2.0版和jsf版。后端我有一个webservice和一个postgres数据库。
我正在尝试通过我的webservice通过我的前端项目中的表单添加对象(在本例中是汽车),并删除这些对象。我的问题是它有时只能工作(通常只有一次)。我尝试了很多不同的解决方案,并且还浏览了谷歌几天..
这是一些代码(简化)。
前端html-page:
<b:dataTable value="#{leasingCarBean.carList}" var="car"
id="carsTable">
<b:dataTableColumn value="#{car.licensenumber}" />
<b:dataTableColumn value="#{car.brand}" />
<b:dataTableColumn value="#{car.model}" />
<b:dataTableColumn value="#{car.color}" />
<b:dataTableColumn value="#{car.year}" />
<b:dataTableColumn label="Delete">
<b:commandButton value="Delete" onclick="ajax:leasingCarBean.deleteCar(car.licensenumber)" update="carsTable"/>
</b:dataTableColumn>
</b:dataTable>
<b:button size="lg" look="info" value="New car"
onclick="$('.modalPseudoClass').modal();ajax:leasingCarBean.createNewCar()"
/>
<b:modal id="amodal" title="New car" styleClass="modalPseudoClass">
<h:form>
<b:inputText placeholder="Licensenumber"
value="#{leasingCarBean.newCar.licensenumber}" />
<b:inputText placeholder="Brand"
value="#{leasingCarBean.newCar.brand}" />
<b:inputText placeholder="Model"
value="#{leasingCarBean.newCar.model}" />
<b:inputText placeholder="Color"
value="#{leasingCarBean.newCar.color}" />
<b:inputText placeholder="Year"
value="#{leasingCarBean.newCar.year}" />
</h:form>
<f:facet name="footer">
<b:button value="Close" dismiss="modal" />
<b:commandButton value="Add" look="primary" dismiss="modal"
onclick="ajax:leasingCarBean.postNewCar()" update="carsTable" />
</f:facet>
</b:modal>
Backingbean前端
@Named
@ViewScoped
public class LeasingCarBean implements Serializable{
private static final long serialVersionUID = 1L;
private Cardto newCar;
private Customerdto newCustomer;
private WebTarget leasingCarTarget = ClientBuilder.newClient().target("http://localhost:8080/leasingcarbackend/leasingcar");
public LeasingCarBean() {}
@PostConstruct
public void init() {
newCar = new Cardto();
newCustomer = new Customerdto();
}
public List<Cardto> getCarList() {
return leasingCarTarget.path("/carList").request(MediaType.APPLICATION_JSON).get(new GenericType<List<Cardto>>() {});
}
public void deleteCar(String licensenumber) {
leasingCarTarget.path("/deleteCar/{licensenumber}").resolveTemplate("licensenumber", licensenumber).request(MediaType.APPLICATION_JSON).delete();
}
public void postNewCar() {
leasingCarTarget.path("/newCar").request(MediaType.APPLICATION_JSON).post(Entity.json(newCar));
newCar = null;
}
public void createNewCar() {
newCar = new Cardto();
}
public Cardto getNewCar() {
return newCar;
}
}
我的一些错误:
当我添加汽车,然后单击删除按钮(在数据表中的任何汽车上)时,它会评估错误的EL表达式,leasingCarBean.newCar.licensenumber而不是leasingCarBean.deleteCar(car.licensenumber)
当我按下“新车”按钮时,它会在我按下“添加”按钮之前调用postNewCar方法,这意味着我得到空错误,因为我没有填写表格。
有时当我尝试添加新车时,即使填写了表格,我也会收到此错误:
javax.el.PropertyNotFoundException: /admin/allCars.xhtml @100,57
value="#{leasingCarBean.newCar.licensenumber}": Target Unreachable, 'null' returned null
有关如何执行此操作的任何提示? 我将不胜感激任何帮助:)
干杯!
答案 0 :(得分:1)
所以,我解决了我的问题,如果其他人对解决方案感兴趣,我想在此发布。
我的html页面:
//My datatable stays the same
<b:dataTable value="#{leasingCarBean.carList}" var="car"
id="carsTable">
<b:dataTableColumn value="#{car.licensenumber}" />
<b:dataTableColumn value="#{car.brand}" />
<b:dataTableColumn value="#{car.model}" />
<b:dataTableColumn value="#{car.color}" />
<b:dataTableColumn value="#{car.year}" />
<b:dataTableColumn label="Delete">
<b:commandButton value="Delete" onclick="ajax:leasingCarBean.deleteCar(car.licensenumber)"
oncomplete="javascript:location.reload();"/>
</b:dataTableColumn>
</b:dataTable>
<b:button size="lg" look="info" value="New car"
onclick="$('.modalPseudoClass').modal()" />
//Took away createNewCar()
<b:modal id="amodal" title="New car" styleClass="modalPseudoClass">
<h:form>
<b:inputText placeholder="Licensenumber"
value="#{leasingCarBean.newCar.licensenumber}" />
<b:inputText placeholder="Brand"
value="#{leasingCarBean.newCar.brand}" />
<b:inputText placeholder="Model"
value="#{leasingCarBean.newCar.model}" />
<b:inputText placeholder="Color"
value="#{leasingCarBean.newCar.color}" />
<b:inputText placeholder="Year"
value="#{leasingCarBean.newCar.year}" />
</h:form>
<f:facet name="footer">
<b:button value="Close" dismiss="modal" />
<b:commandButton value="Add" look="primary"
onclick="$('.modalPseudoClass').modal('hide');ajax:leasingCarBean.postNewCar()"
//Dismiss modal do not work for commandButton
update="carsTable" />
</f:facet>
</b:modal>
支持bean前端:
@Named
@ViewScoped
public class LeasingCarBean implements Serializable{
private static final long serialVersionUID = 1L;
private Cardto newCar;
private Customerdto newCustomer;
private WebTarget leasingCarTarget = ClientBuilder.newClient().target("http://localhost:8080/leasingcarbackend/leasingcar");
public LeasingCarBean() {}
@PostConstruct
public void init() {
newCar = new Cardto();
newCustomer = new Customerdto();
}
public List<Cardto> getCarList() {
return leasingCarTarget.path("/carList").request(MediaType.APPLICATION_JSON).get(new GenericType<List<Cardto>>() {});
}
public void deleteCar(String licensenumber) {
leasingCarTarget.path("/deleteCar/{licensenumber}").resolveTemplate("licensenumber", licensenumber).request(MediaType.APPLICATION_JSON).delete();
}
public void postNewCar() {
leasingCarTarget.path("/newCar").request(MediaType.APPLICATION_JSON).post(Entity.json(newCar));
newCar = new Cardto(); //Instead of = null
}
//This method is not needed anymore
public void createNewCar() {
newCar = new Cardto();
}
public Cardto getNewCar() {
return newCar;
}
希望这将有助于未来的某人:)