Rate jsf bean property based on url

Is there a way to display a specific JSF page based on the request URL?

Say I have a JSF page " details.xhtml ". The managed bean " detailsBean " has a list of objects, each of which has its own identifier. Now, if the user requests the page " ../details.xhtml?id=1 ", a list should be requested for the object with identifier 1, and a page of the resulting data of this object should be displayed.

I have already written a converter implementation class that can convert from an object to an ID and vice versa, but I do not know how to use it correctly. Do I have to work through the JAX-RS specification for this to work or is there a simpler solution?

+1
source share
2 answers

You can achieve this with a simple JSF with the following steps.

  • Grab the identifier in the request to determine which object is being requested in your DetailsBean from the request parameter. There are many ways to achieve this, one of which adds the following annotation to your managed bean (this is currently only allowed for the @RequestScoped bean, see Why here ).

      @ManagedProperty(value="#{param.id}") int requiredObjectId; 

    The annotations above will capture the id parameter from the request and assign it to the requiredObjectId variable.

  • Using the captured id, configure your object in a bean using the @PostConstruct method

      @PostConstruct public void queryForObject(){ //use the requiredObjectId variable to query and setup the object in the backing bean } 

    The selected object must be assigned as an instance variable of your managed bean

  • In your view, you can refer to the requested object that was configured in the bean backup

      <h:panelGrid columns="2"> <h:outputText value="Title"/> <h:outputText value="#{detailsBean.selectedObject.title}"/> </h:panelGrid> 

If your bean is in an area wider than the request area, you will need a combination of constructs to clear this request parameter before rendering.

  • Capturing a request parameter in the JSF view itself with

     <f:metadata> <f:viewParam name="id" value="#{detailsBean.requiredObjectId}" required="true" requiredMessage="You must provide an Object Id"/> </f:metadata> **OR** 
  • Due to the nature of the JSF Lifecycle processing, doing the above may not make the value available for your use in time for setting up the object. Instead, you can use the following.

      <f:metadata> <f:event type="preRenderView" listener="#{detailsBean.setObjectId}" /> </f:metadata> 

    What we have done here is to specify the method (which captures the id ) in the backup bean that must be executed before rendering the view, ensuring that the id parameter is available as you need it at that time. Go to step 3 only if you use <f:event/> above.

  • In the bean backup, you now define the setObjectId method

      public void setObjectId(){ Map<String,String> requestParams = FacesContext.getExternalContext().getRequestParameterMap(); requiredObjectId = Integer.parseInt(requestParams.get("id")); } 

Please note that the above option usually works around / breaks, and is not a clean solution as such

-1
source

In JSF, you can do this using a so-called view parameter. You declare them in the metadata section of your Facelet:

 <f:metadata> <f:viewParam name="id" value="#{yourBean.yourObject}" label="id" converter="yourObjectConverter" /> </f:metadata> 

This will get the id url from the request url. For instance. if you request the page on which it appears using localhost:8080/mypage.jsf?id=1 , then 1 will be passed to yourObjectConverter , and everything that will be returned by this converter will be set to yourBean.yourObject .

This way your bean support will get the converted object. No need to foul your bean support again and again with the same request code.

 @ManagedBean public class YourBean { private SomeObject someObject; public void setYourObject(SomeObject someObject) { this.someObject = someObject; } } 

If your backup bean is presented as a scope, you can use the OmniFaces viewParam option viewParam , because otherwise it will be useless to convert after each postback (if your converter performs a DB request, you definitely don't want to).

Working full examples:

Further reading:

+4
source

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


All Articles