Creating the Edit My Element Page in Java Servers Using Facelets

Say you have the following Facelet (Using Facelets 1.1.12):

edit_item.xhtml which i access with edit_item.jsf 

Now I have another page sending me edit_item.jsf with the identifier of the GET parameter, which uri looks like this: http://mysite.com/edit_item.jsf?ID=200

How do you access the Bean and extract the information and display it on the request page using JSF and Facelets? Is there any way to start Bean when the page loads?

+4
source share
1 answer

You can use the faces-config.xml configuration to enter an identifier from a 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 can enter an identifier 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> 

Lyceum 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 is not the only way to do this (you can look up the ID using, for example, FacesContext ).

+5
source

All Articles