ManagedBean函数不会被调用

时间:2015-05-13 13:51:26

标签: jsf submit managed-bean

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

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

<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>

地址的那个看起来像这样:

<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>

用户的相应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);
  }
}

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);
  }

}

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

1 个答案:

答案 0 :(得分:1)

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

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

并将其替换为:

pattern="MM/dd/yyyy"

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