Question

Which is the easier and organized way to use Struts2 ? With Annotations or with XML files ?

If with annotations, then with which kind of annotations? With struts-convention-plugin you can even avoid completely writing conventions i.e@results or @action.

What benefits will annotations give over not writing them ?

Was it helpful?

Solution

I've always used XML, and I've started recently using Convention.

I would now say that you can still use XML, but it would be better to use Annotations.

The facts in support to this are that, with Convention plugin;

  1. less code is needed: since Convention plugin will scan some packages looking for Actions, you don't need to declare any action anymore. The same is applied to method name, result declaration, and so on: you will specify only things that differ from the standard behavior, while with XML you will have to write "the obvious" each time;
  2. the knowledge is decentralized (or distributed): the configuration is now in the place it is meant to be; if you are inside an Action, you don't need to open the struts.xml configuration file, find the action element (among many others) to discover how it is configured; you can simply look at the Annotations inside the class to understand immediately how it works. The same is applied to Validation (inside the class and not in the actionName-validation.xml file;

This will help you having more granularity, a cleaner code, smaller files and almost no configuration.

OTHER TIPS

Both configurations using XML files and annotations are equivalent. In my opinion, it's just a matter of feeling and learning-curve.

The major benefit using annotations is that you can a priori get rid of the struts.xml file (yeah, with all the frameworks using XML declarative architecture, removing one of them can alleviate the classpath content). But from my little experience, you can't consider not using the struts.xml file for large projects. Also, as you mentionned, using annotations, you can see the power of inheriting intelligent defaults provided by the framework. Therefore, some annotaions are not mandatory, struts 2 will automatically use a default configuration(it's called zero configuration, or convention over configuration).

The major benefit using XML file is that you have a centralized way to manage your application architectural components, even if you can modulirize your components declaration in multiple XML file (which are referenced in the struts.xml file). I guess using XML file alleviate the learning-curve. XML file also allows you to inherit intelligent defaults.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top