File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the best practice of handling attribute names?

 
Marcin Mogiela
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Scope attribute names in Servlets/Jsp are strings. Due to typing errors it is bad to use them as such.

It would be good to have an enum or an interface with string constants. But having such an interface it

becomes a dependency magnet, every servlet and jsp in the system depends on it and requires

recompilation on every attribute name change or attribute addition.

It there any best practice or pattern to handle scope attribute names?

Thanks,
Marcin
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marcin,

from my personal experience I'd suggest you to keep the "string" properties where they belong to. In my opinion it makes everything more difficult if create separate interfaces or classes to hold these constants.

Why not just create public static string constants in those classes where you need them? This is a common practice in many swing components which also use strings as property names for listeners and other components which depend on these names. So you have only one place where you really define the names and all other classes only have to rely on some public static final string constants.

Marco
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a modern web application, the largest consumer of scoped variable names is EL expressions. And since the EL does not natively have any way to access string constants, defining such constants for scoped variable names is problematic.

If you really really really want to do it that way, you can read this article in which I devise a way to expose constants to the EL, but I never use it for scoped variable names -- I think it creates too messy a level of indirection.

Personally, when it comes to scoped variable names, I just type carefully.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't think about EL expression. In this case my advice is obviously not the very best idea

Marco
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic