我正在构建一个web应用程序(Spring 4,PrimeFaces5.1和JPA)。
虽然我的xhtml页面之一在使用它的ManagedBean时没有问题,但其他页面却不能正常工作。具体来说,UI输入组件(如inputText)不会调用托管Beans中的任何setter。我已经在调试模式下运行了Tomcat (8),这表明页面调用get-方法,但根本没有set-方法。如果我试图持久化某个值,那么所有的值都是空的(如果是int值,则为0 )。(它甚至使用空值将一个对象保存到数据库中,尽管我声明了一些@NotNull约束,这些约束应该通过JPA传递到数据库配置中)
因此,问题是:如何使我的inputFields与我的ManagedBean字段一起工作?(Eclipse还在编辑器中向我展示了,理论上讲,与字段的连接是存在的,它知道托管bean、字段和get-/set-方法)
SoftwareManagedBean.java
@ManagedBean(name = "swmb")
@ViewScoped
public class SoftwareManagedBean extends AssetManagedBean implements
Serializable {
private String bezeichnung;
private Software neueSoftware;
// +some more private fields, every single one with its get-/set-method
@Override
public String getBezeichnung() {
return super.getBezeichnung();
}
@Override
public void setBezeichnung(final String bezeichnung) {
super.setBezeichnung(bezeichnung);
}
//instantiante the field "neueSoftware"
public void createEmptySoftware(){
if(neueSoftware != null)
return;
this.neueSoftware = new Software();
}
//Persist Software with values from inputFields
public void addSoftware() {
createEmptySoftware();
neueSoftware.setBezeichnung(getBezeichnung());
softwareService.addSoftware(neueSoftware);
//...
neueSoftware = null;
}viewSoftware.xhtml
<h:body>
<p:dialog header="Neue Software anlegen" widgetVar="SwNeuDialog" width="60%"
closeOnEscape="true" draggable="false" resizable="false" position="center">
<h:form id="SwDlgForm">
<h:panelGrid columns="3" border="0" >
<p:outputLabel for="swBezeichnung" value="Bezeichnung: " />
<p:inputText id="swBezeichnung" value="#{swmb.bezeichnung}"
label="Bezeichnung" required="true" />
<f:verbatim/>
<p:outputLabel for="swKategorie" value="Kategorie: " />
<p:selectOneMenu id="swKategorie" value="#{swmb.kategorie}" label="Kategorie" required="true" >
<f:selectItem itemLabel="Kategorie wählen" value="#{null}" noSelectionOption="true"/>
<f:selectItems value="#{swmb.kategorieListe}" var="kat" itemLabel="#{kat.bezeichnung}" itemValue="#{kat}"/>
</p:selectOneMenu>
<p:commandButton value="neue Kategorie hinzufügen" />
<!-- + some more input fields -->
<p:commandButton value="Speichern" action="#{swmb.addSoftware()}" onclick="PF('SwNeuDialog').hide()" resetValues="true" process="@this"/>
<p:commandButton value="Abbrechen" onclick="PF('SwNeuDialog').hide()" resetValues="true" process="@this"/>
</h:panelGrid>
</h:form>
</p:dialog>
<!-- dataTable -->
</h:body>AssetManagedBean.java
@ManagedBean
public abstract class AssetManagedBean {
//name of the hard-/software
private String bezeichnung;
//+ some more fields with get-/set methods
public String getBezeichnung() {
return bezeichnung;
}
public void setBezeichnung(String bezeichnung) {
this.bezeichnung = bezeichnung;
}我希望代码足够看到问题,因为其余的代码都遵循相同的结构。我认为,问题可能在xhtml文件中,但我不知道在哪里,也不知道为什么。我已经得到了SpringBeanFacesELResolver (或者不管它是如何调用的),我已经查看了代码,并将其与另一个xhtml页面及其托管Bean进行了比较,但是已经没有什么区别了。(虽然一个人在工作,但一个人不工作)
我的调试器展示了工作类/页(viewAdministration.xhtml)如何调用托管bean的get-/set方法:
在viewSoftware.xhtml上,它看起来如下:
正如你所看到的,当我尝试提交时,没有设置或获取。
因此,概括地说:
希望你能帮我。如果这篇文章缺少一些信息,我很抱歉,我尽力不让这篇文章太大,并且尽可能多地获取相关信息。
发布于 2014-11-11 14:11:58
所以,我发现了我的错误(至少,我认为我错了)。我只花了两个星期,但无论如何.
我试图更具体地测试它,编写测试类和xhtml页面。没有什么起作用的(从简单的日期输入到自己的类)。
这个问题的解决方案是在我的(ajax="false"). commandButton上禁用ajax。当我试图了解更多内容时,我意识到打开对话框窗口的commandButton是嵌套在dataTable内部的一个方面中的,因此它在正确设置输入字段的值方面存在问题。
所以谢谢你的帮助。也许/希望这能或将来也能帮助到其他人。
发布于 2014-10-29 12:55:42
从第一眼看代码,没有阅读它的全部,尝试把process="SwDlgForm"放在您的命令按钮,而不是process="@this"。如果这不能解决问题,我会更仔细地阅读并尝试帮助。
https://stackoverflow.com/questions/26630551
复制相似问题