evaluate jsf bean property based on URL

后端 未结 2 1531
南笙
南笙 2021-01-23 11:28

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

Let\'s say I have a JSF page \"details.xhtml\". The managed bean \"detailsBean

相关标签:
2条回答
  • 2021-01-23 12:02

    You can achieve this with plain JSF with the following steps

    1. Capture the ID in the request to determine what object is being queried for in your DetailsBean from the request parameter. There are many ways to achieve this, one of which is adding the following annotation to your managed bean (this is currently only permitted for a @RequestScoped bean, see why here).

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

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

    2. Using the captured Id, setup your object in your bean in a @PostConstruct method

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

      The object retrieved should be assigned as an instance variable of your managed bean

    3. In your view, you could then reference the queried object that has been setup in the backing bean

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

    If your bean is in a scope broader than the request scope, you'll need a combination of constructs to cleanly pull that request parameter before view rendering.

    1. Capture the request parameter within the JSF view itself using

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

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

      What we've done here is specify a method (that captures the id) in the backing bean that must be executed before the view is rendered, ensuring that the id parameter is available as at the time you need it. Proceed with step 3, only if you're using <f:event/> above.

    3. In the backing bean, you now define the setObjectId method

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

    Note that the above option is generally a work around/hack and not a clean solution as such

    0 讨论(0)
  • 2021-01-23 12:08

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

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

    This will grab the URL parameter id from the request URL. E.g. if you request the page this appears on with localhost:8080/mypage.jsf?id=1, then 1 will be handed to the yourObjectConverter and whatever this converter returns will be set in yourBean.yourObject.

    Your backing bean will thus get the converted object. No need to pollute your backing bean over and over again with the same query code.

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

    If your backing bean is view scoped, you may want to use the OmniFaces variant of viewParam instead, since otherwise it will needlessly convert after each postback (if your converter does a DB query, you definitely don't want this).

    Working full examples:

    • http://code.google.com/p/javaee6-crud-example/source/browse/WebContent/user_edit.xhtml
    • http://code.google.com/p/javaee6-crud-example/source/browse/src/backing/UserEdit.java

    Further reading:

    • Communication in JSF 2.0 - Processing GET request parameters
    • Stateless vs Stateful JSF view parameters
    0 讨论(0)
提交回复
热议问题