3

JSF 2.0 comes up with annotation. However in JSF 1.2 we defined attributes in faces-config.xml file.

In JSF 2.0 we have two options, either make use of annotation or use faces-config.xml. What is better approach? Writhing the properties in faces-config.xml is easily manageable, whereas writing annotation in each file is somewhat not easily manageable.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ravi
  • 6,140
  • 18
  • 77
  • 154
  • 1
    Related: [What is the use of faces-config.xml in JSF 2?](http://stackoverflow.com/questions/7583038/what-is-the-use-of-faces-config-xml-in-jsf-2) and [Migrating from JSF 1.2 to JSF 2.0](http://stackoverflow.com/questions/4441713/migrating-from-jsf-1-2-to-jsf-2-0) – BalusC Feb 21 '12 at 12:58

2 Answers2

7

Annotations are generally preferable because they keep information about a class with the code of the class, so you don't have to look in another place to understand it. It also reduces duplication of information because you don't have to write out the class name to specify what class the annotation belongs to.

Then again, for some things it can be valuable to have all information of a certain kind collected in one place (e.g. URL mappings). Fortunately, annotations and XML configuration are compatible, so you can put some things into XML and use annotations for everything else.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 1
    May i write complete properties in java code as annotation? If yes, then is there any need of faces-config.xml? May i ignore faces-config.xml? i mean if i don't create this file, will it effect the code? – ravi Feb 21 '12 at 11:26
  • 1
    @Ravi Joshi: yes, it's possible to put all JSF configration into annotations and not have a faces-config.xml file at all. I'm not sure whether this is true for every singe exotic configuration, though. – Michael Borgwardt Feb 21 '12 at 11:39
3

Well, this is absolutely depends on you, but annotations are the "modern" way of programming. Code is becoming more readable and maintainable with them. IMHO annotations are less vulnerable to errors because you don't have to maintain a huge file of XML entries, but you have just few lines of annotations per each file(also it's nice when you open a file and immediately see that this class is a managed bean). so code is becoming more self-commenting.

If I were you I would definitely go with annotations, IMHO it's far more easy to manage them.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115