Using static strings to define input field names in JSPs - good idea or not?
I've just be asked to work on a large portal project and have been looking through the established code. I keep finding this in the jsps:
<input class="portlet-form-button"
name="<%=ModifyUserProfile.FORM_FIRST_TIME_LOGIN_SUBMIT%>" type="submit" ...
The authors are using static strings defined in classes to define the names of input fields and buttons in jsp forms.
I've never seen this done before and was wondering if this is common practice. I'm inclined to think not, but I'm asking because, apart from centralising names which I would have thought are not likely to chang开发者_如何学Ce, I can't see the reason why.
Any thoughts on this?
As you've determined, the advantage of this approach is that the compiler ensures the identifier used in the page always matches the one in your Java code (e.g. the portlet processAction
method that reads the submitted field). Changing the value of the String constant automatically updates the view. It may also help identify usage of String constants.
The downside is that it makes for pretty ugly view code and I would avoid scriptlet-style code wherever possible.
Whether I'd change this would probably depend on the cleanliness of the code. If the String identifiers are mixed in with a mess of other constants and the views are thousand line script jungles, I'd probably leave them alone.
Since these are portlets, if I was feeling completely anal about code cleanliness, I'd namespace that input name with an EL function.
The practice is right. Like you said, it helps in centralizing the messages.
The other more helpful advantage is for Internationalization. YOu have multiple properties file, each specific for a locale/language. Use the one specific to the user's locale and you have internationalized messages without changing your jsp.
精彩评论