4

假设您有以下 Facelet ( Using Facelets 1.1.12 ):

edit_item.xhtml which i access with edit_item.jsf

现在我有另一个页面将我发送到带有 GET 参数 ID 的 edit_item.jsf,uri 看起来像这样:http://mysite.com/edit_item.jsf?ID=200

您如何访问 Bean 并获取信息,并使用 JSF 和 Facelets 将其显示在请求页面上?有没有办法在页面加载时运行 bean?

4

1 回答 1

5

您可以使用faces-config.xml配置从param映射中注入 ID。

对于这个简单的 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
  }
}

您可以使用以下定义注入 ID:

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

小面形式:

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

这不是唯一的方法(例如,您可以直接使用FacesContext示例查找 ID)。

于 2009-10-06T15:45:19.670 回答