Creating an “Edit my Item”-page in Java Server Faces with Facelets

谁说我不能喝 提交于 2019-11-29 07:56:39

You can use the faces-config.xml configuration to inject the ID from the param map.

For this simple bean:

public class BeanWithId implements Serializable {
  private String id;
  private String info;

  private void populateInfo() {
    info = "Some info from data source for id=" + id;
  }

  public String getId() { return id; }

  public void setId(String id) {
    this.id = id;
    populateInfo();
  }

  public String getInfo() { return info; }
  public void setInfo(String info) { this.info = info; }

  public String save() {
    System.out.println("Saving changes to persistence store");
    return null; // no navigation
  }
}

You could inject the ID using this definition:

  <managed-bean>
    <managed-bean-name>beanWithId</managed-bean-name>
    <managed-bean-class>datasource.BeanWithId</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
      <property-name>id</property-name>
      <property-class>java.lang.String</property-class>
      <value>#{param.ID}</value>
    </managed-property>
  </managed-bean>

Facelets form:

<h:form>
  <p>ID: <h:outputText value="#{beanWithId.id}" /></p>
  <p>Info: <h:inputText value="#{beanWithId.info}" /></p>
  <p><h:commandLink action="#{beanWithId.save}" value="Save">
    <f:param name="ID" value="#{param.ID}" />
  </h:commandLink></p>
</h:form>

This isn't the only way to do it (you could look the ID up directly using the FacesContext for example).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!