0

I'm new to JSF so sorry if my question is trivial.

I have to present a list of items of three different types: let's say books, software and courses with different metadata (attributes) and requiring different visual representation.

Each of these items inherits from an abstract Item class, so I've a list of Items.

I thought to declare an abstract method getHtml() in the Item class so that each subclass can implement it's own "renderer" . Then in the XHTML page I'll call the getHtml() method using EL. The question is: is this the correct way? I see some drawbacks:

  1. model mixed with view
  2. HTML generated by means of Java code (quite difficult to maintain).

Any suggestion is appreciated.

I don't know if this synthetic presentation is clear enough: in case, please ask for clarifications.

Thanks in advance.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
baronKarza
  • 513
  • 6
  • 23
  • Thanks a lot, BalusC! You have driven me on the right rail. I knew what I need, but I didn't know what to ask. The annswers you pointed out are what I was looking for. Thanks again. – baronKarza Jul 11 '16 at 06:00

1 Answers1

0
  1. Create a page for choosing between the three, say, a drop dow list.
  2. For the view, create three files: book.xhtml, boftware.xhtml, and courses.xhtml.
  3. Then create one managed bean.

    @ManagedBean
    public class BkSoCoSelector {
    private String myChoice = null;
    //add setter and getter. 
    //add no-arg constructor. 
    }
    
  4. Create a method in the above class to make dynamic choice[see example].note: The choice below is based on #1 above, the drop down list.

    public String pageSelector(){ //if null, return nothing. //if this.myChoice == book, return book; //no file extension needed. //do the same for the other files. }

  5. On main page, #1, add the following

    <h:commandButton value = "Submit" action = "#{bkSoCoSelector.pageSelector()}"/>

To Summarise:

  • Give the user a choice
  • Set the chosen value to the instance variable. //done behind the scene.
  • Get the now set instance variable.
  • Base on the value of the instance variables, select a a page to display.
elha2en
  • 192
  • 8
  • Xenwar, thanks for the quick answer. I can not apply it to my problem because the choice of the item type is not feasable. The list is a result set originating from Elasticsearch, and so I need to present all the items together. The BalusC comment indicated the correct way to dinamically load a XHTML fragment that can present all the required information in a list of results. However, thanks again for your proposal. – baronKarza Jul 11 '16 at 06:11