使用静态字符串在 JSP 中定义输入字段名称 - 好主意吗?

发布于 2024-09-01 10:45:09 字数 346 浏览 2 评论 0原文

我刚刚被要求从事一个大型门户项目,并且一直在查看已建立的代码。我不断在jsp 中发现这一点:

<input class="portlet-form-button"
    name="<%=ModifyUserProfile.FORM_FIRST_TIME_LOGIN_SUBMIT%>" type="submit" ...

作者使用类中定义的静态字符串来定义jsp 表单中的输入字段和按钮的名称。

我以前从未见过这样做,想知道这是否是常见做法。我倾向于认为不会,但我之所以这么问,是因为除了我认为不太可能改变的集中名称之外,我看不出原因。

对此有什么想法吗?

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 change, I can't see the reason why.

Any thoughts on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠书 2024-09-08 10:45:09

正如您所确定的,这种方法的优点是编译器确保页面中使用的标识符始终与 Java 代码中的标识符相匹配(例如,读取提交字段的 portlet processAction 方法)。更改字符串常量的值会自动更新视图。它还可能有助于识别字符串常量的用法。

缺点是它会产生相当难看的视图代码,我会尽可能避免使用 scriptlet 样式的代码。

我是否会改变这一点可能取决于代码的整洁程度。如果字符串标识符与一堆其他常量混合在一起,并且视图是千行脚本丛林,我可能会不理会它们。

由于这些是 portlet,如果我对代码的清洁度感到非常不满,我会使用 EL 函数为输入名称命名空间。

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.

牛↙奶布丁 2024-09-08 10:45:09

做法是对的。
正如您所说,它有助于集中消息。

另一个更有用的优势是国际化。您有多个属性文件,每个属性文件都特定于一种区域设置/语言。使用特定于用户区域设置的消息,您无需更改 jsp 即可获得国际化消息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文