ManagedBean函数不会被调用[重复]

时间:2021-05-30 16:47:03

This question already has an answer here:

这个问题在这里已有答案:

I do have two JSF pages, one for "User" and one for "Address".

我有两个JSF页面,一个用于“用户”,一个用于“地址”。

The one for User looks like this:

用户的那个看起来像这样:

<h:body>
   <h:form id="UserForm" name="UserCreate">
<p:panelGrid id="userPanel" rendered="true" columns="4" layout="tabular">
<p:outputLabel escape="true" rendered="true" for="firstName" value="firstName"/> <p:inputText maxlength="200" size="0" immediate="false" required="false" disabled="false" id="firstName" readonly="false" rendered="true" value="#{userCRBean.user.firstName}"/>
<p:outputLabel escape="true" rendered="true" for="lastName" value="lastName"/> <p:inputText maxlength="200" size="0" immediate="false" required="false" disabled="false" id="lastName" readonly="false" rendered="true" value="#{userCRBean.user.lastName}"/>
<p:outputLabel escape="true" rendered="true" for="username" value="username"/> <p:inputText maxlength="200" size="0" immediate="false" required="false" disabled="false" id="username" readonly="false" rendered="true" value="#{userCRBean.user.username}"/>
<p:outputLabel escape="true" rendered="true" for="password" value="password"/> <p:inputText maxlength="200" size="0" immediate="false" required="false" disabled="false" id="password" readonly="false" rendered="true" value="#{userCRBean.user.password}"/>
<p:outputLabel escape="true" rendered="true" for="id" value="id"/> <p:inputText maxlength="200" size="0" immediate="false" required="false" disabled="false" id="id" readonly="true" rendered="true" value="#{userCRBean.user.id}"/>
<p:outputLabel escape="true" rendered="true" for="createdOn" value="createdOn"/> <p:calendar disabledWeekends="false" effectDuration="normal" maxHour="23" maxlength="2147483647" maxMinute="59" maxSecond="59" minHour="0" minMinute="0" minSecond="0" mode="popup" navigator="false" pages="1" pattern="MM/dd/yyyy" readonlyInput="false" selectOtherMonths="false" showButtonPanel="false" showOn="both" showOtherMonths="false" showWeek="false" stepHour="1" stepMinute="1" stepSecond="1" timeOnly="false" immediate="false" required="false" disabled="false" id="createdOn" readonly="false" rendered="true" value="#{userCRBean.user.createdOn}"/>
<p:outputLabel escape="true" rendered="true" for="lastModified" value="lastModified"/> <p:calendar disabledWeekends="false" effectDuration="normal" maxHour="23" maxlength="2147483647" maxMinute="59" maxSecond="59" minHour="0" minMinute="0" minSecond="0" mode="popup" navigator="false" pages="1" pattern="MM/dd/yyyy" readonlyInput="false" selectOtherMonths="false" showButtonPanel="false" showOn="both" showOtherMonths="false" showWeek="false" stepHour="1" stepMinute="1" stepSecond="1" timeOnly="false" immediate="false" required="false" disabled="false" id="lastModified" readonly="false" rendered="true" value="#{userCRBean.user.lastModified}"/>
<p:commandButton id="submit" type="submit" style="" value="Submit" icon="ui-icon-disk" actionListener="#{userCRBean.saveRecord}"/>
</p:panelGrid>
</h:form>
</h:body>

The one for Address looks like this:

Address的地址如下所示:

<h:body>
<h:form id="AddressForm" name="AddressCreate">
<p:panelGrid id="addressPanel" rendered="true" columns="4" layout="tabular">
<p:outputLabel escape="true" rendered="true" for="city" value="city"/> <p:inputText maxlength="200" size="0" immediate="false" required="false" disabled="false" id="city" readonly="false" rendered="true" value="#{addressCRBean.address.city}"/>
<p:outputLabel escape="true" rendered="true" for="country" value="country"/> <p:inputText maxlength="200" size="0" immediate="false" required="false" disabled="false" id="country" readonly="false" rendered="true" value="#{addressCRBean.address.country}"/>
<p:outputLabel escape="true" rendered="true" for="street" value="street"/> <p:inputText maxlength="200" size="0" immediate="false" required="false" disabled="false" id="street" readonly="false" rendered="true" value="#{addressCRBean.address.street}"/>
<p:outputLabel escape="true" rendered="true" for="id" value="id"/> <p:inputText maxlength="200" size="0" immediate="false" required="false" disabled="false" id="id" readonly="true" rendered="true" value="#{addressCRBean.address.id}"/>
<p:outputLabel escape="true" rendered="true" for="createdOn" value="createdOn"/> <p:calendar disabledWeekends="false" effectDuration="normal" maxHour="23" maxlength="2147483647" maxMinute="59" maxSecond="59" minHour="0" minMinute="0" minSecond="0" mode="popup" navigator="false" pages="1" pattern="MM/dd/yyyy" readonlyInput="false" selectOtherMonths="false" showButtonPanel="false" showOn="both" showOtherMonths="false" showWeek="false" stepHour="1" stepMinute="1" stepSecond="1" timeOnly="false" immediate="false" required="false" disabled="false" id="createdOn" readonly="false" rendered="true" value="#{addressCRBean.address.createdOn}"/>
<p:outputLabel escape="true" rendered="true" for="lastModified" value="lastModified"/> <p:calendar disabledWeekends="false" effectDuration="normal" maxHour="23" maxlength="2147483647" maxMinute="59" maxSecond="59" minHour="0" minMinute="0" minSecond="0" mode="popup" navigator="false" pages="1" pattern="MM/dd/yyyy" readonlyInput="false" selectOtherMonths="false" showButtonPanel="false" showOn="both" showOtherMonths="false" showWeek="false" stepHour="1" stepMinute="1" stepSecond="1" timeOnly="false" immediate="false" required="false" disabled="false" id="lastModified" readonly="false" rendered="true" value="#{addressCRBean.address.lastModified}"/>
<p:commandButton id="submit" type="submit" style="" value="Submit" icon="ui-icon-disk" actionListener="#{addressCRBean.saveRecord}"/>
</p:panelGrid>
</h:form>
</h:body>

The according Bean for User:

根据用户的Bean:

@ManagedBean(name = "userCRBean")
@ViewScoped
public class UserCRBean implements Serializable {

  @ManagedProperty(value = "#{userService}")
  private UserService userService = new UserService(); 

  private User user = new User();

  @PostConstruct
  public void init() {
    kuser.setCreatedOn(new Date());
    kuser.setLastModified(new Date());
  }

  public UserService getUserService() {
    return userService;     
  }

  public void setUserService(UserService userService) {
    this.userService = userService;
  }

  public User getUser() {
    return this.user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  public void saveRecord() {
    System.out.println("Entity = ");
    System.out.println(this.user.toString());
    userService.save(user);
  }
}

And the Bean for Address:

和地址的豆:

@ManagedBean(name = "addressCRBean")
@ViewScoped
public class AddressCRBean implements Serializable {

  @ManagedProperty(value = "#{addressService}")
  private AddressService addressService = new AddressService(); 

  private Address address = new Address();

  @PostConstruct
  public void init() {
    address.setCreatedOn(new Date());
    address.setLastModified(new Date());
  }

  public AddressService getAddressService() {
    return addressService;      
  }

  public void setAddressService(AddressService addressService) {
    this.addressService = addressService;
  }

  public Address getAddress() {
    return this.address;
  }

  public void setAddress(Address address) {
    this.address = address;
  }

  public void saveRecord() {
    System.out.println("Entity = ");
    System.out.println(this.address.toString());
    addressService.save(address);
  }

}

Funny thing is, clicking the submit button on the "Address" page works fine. Clicking the submit button on the "User" page does not call the according "saveRecord" method and doesn't throw any exception. Any idea what might be the difference between the two submit buttons?

有趣的是,单击“地址”页面上的提交按钮工作正常。单击“用户”页面上的提交按钮不会调用相应的“saveRecord”方法,也不会抛出任何异常。知道两个提交按钮之间可能有什么区别吗?

1 个解决方案

#1


The reason it doesn't work is because of an incorrect date pattern in the User page. Look for :

它不起作用的原因是因为用户页面中的日期模式不正确。寻找 :

pattern="MM/dd/yyyy`enter code here`"

And replace it by :

并替换为:

pattern="MM/dd/yyyy"

Also when "nothing happens", have a look at the JavaScript console. Very often you'll find something helpful to troubleshoot your problem.

此外,当“没有任何反应”时,请查看JavaScript控制台。通常,您会找到一些有助于解决问题的方法。

#1


The reason it doesn't work is because of an incorrect date pattern in the User page. Look for :

它不起作用的原因是因为用户页面中的日期模式不正确。寻找 :

pattern="MM/dd/yyyy`enter code here`"

And replace it by :

并替换为:

pattern="MM/dd/yyyy"

Also when "nothing happens", have a look at the JavaScript console. Very often you'll find something helpful to troubleshoot your problem.

此外,当“没有任何反应”时,请查看JavaScript控制台。通常,您会找到一些有助于解决问题的方法。