将标签等放入属性文件中是否是一种反模式(与 JSP 和 Web 开发相关)
我看到很多 J2EE 开发人员将标签放在属性文件中,但不使用不同的区域设置。 因此,您会遇到很多丢失财产的例外情况。 最主要的是它使得调试和读取 JSP 页面变得困难。 因此,随着时间的推移,您将拥有数千行属性文件,这些文件可能会或可能不会与 JSP 文件一起使用。
对我来说,这似乎是一个糟糕的设计,特别是如果您不打算使用不同语言的属性文件并根据区域设置更改为英语或法语。
我只是想知道您是否也有同样的感觉,是否有 J2EE/JSP 反模式的列表或 URL。
I see a lot of J2EE developers put labels in property files but don't use different Locales. So, you get a lot of missing property exceptions. And the main thing is that it makes it hard to debug and read a JSP page. So over time, you have thousands of lines of property files that may or may not be used with the JSP file.
For me, it seems like a bad design, especially if you don't intend to use a property file with different languages and change to say english or french depending on Locale.
I was just wondering if you felt the same and is there a list or URL of J2EE/JSP anti-patterns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将内容与模板分离始终是一个很好的做法。 这样,您就不需要为每个愚蠢的上下文更改/打字错误/打嗝而重建、重新部署和/或重新启动整个系统。
ResourceBundle
API(这是 JSTL 背后使用的标准fmt
taglib 和其他 i18n/l10n taglib) 是 智能足以在每次更改时动态重新加载资源文件(至少,如果您使用的是 JDK 1.6 或更高版本,它具有 这些内置增强功能)。另外,每当您想要使用 i18n 或想要从属性文件更改为数据库表或其他内容时,您不需要更改模板来从中提取内容 - 如果您这样做,这会让您更加痛苦之后它。
将模板中的内容和位置相互关联起来只需一点工作,我可以想象这是开发人员/维护人员最担心的事情。 我自己编写键,使它们大致匹配
pagename.parentid.elementtype.elementname.contenttype
(大致;并非所有这些都是必要的,但它给出了一个想法),这样就可以立即清楚它所属的位置。例如,指向
home.jsp
的home.login.label.username.tooltip
键:保持此约定一致,您会发现它变得更容易维持这一切。
Separation of content from template is always a good practice. This way you don't need to rebuild, redeploy and/or restart the whole thing for every stupid contextual change/typo/hiccup. The
ResourceBundle
API (which is standard been used behind JSTL'sfmt
taglib and other i18n/l10n taglibs) is smart enough to reload resource files dynamically on every change (at least, if you're using JDK 1.6 or newer, which has those enhancements builtin).Also, whenever you want to go i18n or want to change from a propertiesfile to a database table or something else, then you don't need to change the template to extract the content from it --which would bite you much more if you do it afterwards.
It's only a bit of work to correlate the content and location in template with each other, I can imagine that this is the major fear among the developers/maintainers. I myself compose keys so that they roughly match
pagename.parentid.elementtype.elementname.contenttype
(roughly; not all of them is necessary, but it gives an idea) so that it's already immediately clear where it belongs.E.g. a
home.login.label.username.tooltip
key which points to ahome.jsp
with:Keep this convention consistently and you'll find that it becomes more easy to maintain this all.
将标签放入属性文件中绝对是一个好习惯。 即使您现在不打算国际化,这也可能在未来发生。 它还可以帮助您在页面之间使用一致的命名。
我不知道为什么你会得到财产例外。 在大多数框架中,如果未找到用户区域设置的属性文件,系统将读取默认(英语)文件。
您需要习惯于读取包含从外部属性文件读取的字段的 JSP 页面。 这并不难,而且好处远大于麻烦。
It's definitely a good practice to put labels in property files. Even if you don't plan to internationalize now, this may happen in the future. It also helps you to use a consistent naming across your pages.
I don't know why are you getting property exceptions. In most frameworks the system will read the default (english) file, if a properties file for user's locale isn't found.
You need to get used on reading JSP pages with fields read from an external properties file. It isn't that hard and the benefits far out-weight the hassle.