evaluate jsf bean property based on URL

后端 未结 2 1532
南笙
南笙 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:08

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

             
        
    
    

    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

提交回复
热议问题