Using a managed property with CommandButton in Java Server Faces

In addition to my question “Creating an“ Edit my element ”page in Java servers using Facelets,“ I would like to address the issue that was provided.

When I press the CommandButton button, the identifier = 100 is deleted, and the page is updated, and this is Before , it even launches the method, correctly, so this means that I do not have the identifier when I click the button.

How do you solve this?

Having this driven 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
  }
}

And adding

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

On my facelet page. Now I also have the correct information in my faces - config.xml, and when do I access my page using? ID = 100, I get the correct returned item.

+1
3

URL- GET. .

commandLink

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

.

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

.


URL

URL- , URL- . , , .

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

, f: setPropertyActionListener, JSF 1.2 .

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

This solved my problem.

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

Works like a Charm, however, it removes the visible GET parameter, but it still persists, so faces-config can access the parameter.

0
source

Source: https://habr.com/ru/post/1724168/


All Articles