Struts2 Annotated or XML based ? which is more easier to manage and uncomplicated? [closed]
Asked Answered
S

2

5

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 ?

Swineherd answered 24/10, 2013 at 9:21 Comment(0)
A
5

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.

Alfredalfreda answered 24/10, 2013 at 9:38 Comment(0)
V
3

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.

Vigen answered 24/10, 2013 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.