I’m currently developing a web site using servlets & spring framework. As usual it contains lots of files (jsp, js, css, images, various resources etc).
I’m trying to avoid writing any hardcoded path, or domain in any file …
For example as you may know when a request is handled you ‘forward’ it to a jsp page (it’s path probably will be hardcoded). Other examples are imports images/css/js etc in jsp files …
Is there any general way (or tools) to avoid hardcoded paths/urls so any refactorings won’t cause troubles?
EDIT
I use netbeans 7.1.2 … Unfortunately netbeans only helps with pure java code. When working with jsp files things are limited, and if you add custom tag files and Jsp 2.0 EL is like programming in console mode :p
Actually I just came up with an idea. Since netbeans does analysis and shows dependencies on java code, maybe it’s better to handle all paths & domains as java variables.
I’ve created a package on my project named FileResolver and inside I have one class for each file type on my project (eg one class for Jsp files, one for Css files etc). Inside those files I’ll record & hardcode all paths of all files in public static final String variables. Sample:
All over my project I should use the variables instead of paths. Then anytime I refactor a file, I’ll have only one file to change is the mapping value in those variables …
And if somethime I need to change the variable, netbeans will refactor all of them in the project at once …
I think this will work just fine since I keep my project clean from file paths and the only thing I have to worry about is the mapping in that file of the variables to appropriate file paths.
EDIT
I’ll write a simple parser to create those java files instead of writting by hand for all files … I’ll update when I finish it
UPDATE
Here is my FileResolverGenerator
Just to be specific … This scans all files under “/WEB-INF/jsp/” and creates a java file having all jsp files ‘registered’ to public static final String variables with each path … The idea is to use the generated java file as reference for all jsps are in project … always use these variables instead of hardcoded paths ..
This has nothing to do with the project or any project. It’s just a tool which saves you
time, instead of doing this by hand for every file in the project.
I also created another class ResolverConsistencyChecker, which takes all variables and checks if the filepath is correct (file exists) … since we didn’t made any changes to filenames and filepaths all tests are passed.
This method should run when testing project for ‘errors’