1

私の質問「Creating an “Edit my Item”-page in Java Server Faces with Facelets」に加えて、これが提供した問題をカバーしたいと思います。

commandButton を押すと、ID=100 が削除され、ページが更新されます。これは、メソッドを実行するです。つまり、ボタンを押したときに 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
  }
}

そして追加

<p><h:commandButton action="#{beanWithId.save}" value="Save" /></p>

私のフェイスレットページへ。これで、faces-config.xml にも正しい情報があり、?ID=100 を使用してページにアクセスすると、正しいアイテムが返されます。

4

3 に答える 3

1

元の GET URL から ID を保持するには、いくつかの方法があります。私は包括的であろうとしているわけではありません。

パラメータをcommandLink

<h:commandLink action="#{beanWithId.save}" value="Save">
  <f:param name="ID" value="#{param.ID}" />
</h:commandLink>

リンクがクリックされるたびに、ID がパラメーターから設定されます。

隠しフィールドを使用する

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

フォームが投稿されるたびに、フォームから ID が設定されます。


URL の保持

フォームの URL には元のクエリが含まれていないため、投稿すると、ブラウザー バーの URL から ID が削除されます。これは、アクションの実行後にサーバー側のリダイレクトを使用することで修正できます。

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

  private void redirect() {
    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext ext = context.getExternalContext();
    UIViewRoot view = context.getViewRoot();
    String actionUrl = context.getApplication().getViewHandler().getActionURL(
        context, view.getViewId());
    try {
      // TODO encode id value
      actionUrl = ext.encodeActionURL(actionUrl + "?ID=" + id);
      ext.redirect(actionUrl);
    } catch (IOException e) {
      throw new FacesException(e);
    }
  }
于 2009-10-07T12:30:35.990 に答える
0

これは私の問題を解決しました

<h:commandLink action="#{beanWithId.save}" value="">
    <f:verbatim><input type="button" value="Save"/></f:verbatim>
    <f:param name="id" value="#{beanWithId.id}"/>
</h:commandLink>

チャームのように機能しますが、表示されているGETパラメータは削除されますが、faces-configがparam.idにアクセスできるように保存されます。

于 2009-10-07T12:23:56.857 に答える
0

JSF 1.2 以降を使用している場合は、f:setPropertyActionListener を使用してプロパティを設定できます。

<h:commandButton value="Save" action="#{beanWithId.save}">
     <f:setPropertyActionListener target="#{beanWithId.id}" value="100" />
</h:commandButton>

JSF 1.1以前を使用している場合は、使用できます

<f:param name="reqId" value="100" />

ただし、今回はパラメーターを取得して、次のようにアクションで手動で設定する必要があります。

public String save() {
String idParam
=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("reqId");
setId(idParam);
return null;
}
于 2009-10-07T11:21:27.293 に答える