我应该如何编写 JSP 文件 - Java、JSTL 或其他文件?
我从事 Java 开发人员已有 14 年了,我的大多数项目都是我们必须完成工作,而不是能够规划和设计。 目前一切都很平静,为了公司(和我的简历),我想把事情做好。
在编写 JSP 时,我通常在 <% %> 顶部声明一些 Java 类,然后插入一些 Java 代码来迭代 ArrayList/HashMap,或者当我需要在标记中选择特定项时。
我过去曾使用过 JSTL,但它让我疯狂地尝试操纵该构造来完成我想做的事情;用 Java 编写等效的代码是微不足道的。
我应该做什么? JSTL 是“正确”的做事方式吗? 事情的进展是否远不止于此,我应该使用另一种方式来编写 JSP?
我目前使用 Struts(是的,甚至不是 Struts 2),如果可以的话,希望迁移到 Spring/Struts 2 。但如果我连骑自行车都不能正确的话,我开法拉利就没有意义了。
非常欢迎想法、建议、批评。
I've been a Java developer for 14 years, and most of my projects are where we had to get things done, rather than be able to plan and design.
Things are quiet at the moment, and for the sake of the company (and my CV), I want to do things right.
When writing JSPs, I usually declare a few Java classes at the top in <% %>, and then insert bits of Java code to iterate through ArrayLists/HashMaps, or when I need to SELECT a particular in a tag.
I've used JSTL in the past, but it drove me mad trying to manipulate the construct to do what I wanted to do; writing the equivalent in Java was trivial.
What should I be doing? Is JSTL the "correct" way of doing things?
Have things moved on far more than that, that I should be using another way to write JSPs?
I currently use Struts (yep, not even Struts 2), and would like to move to Spring/Struts 2 if I could. But there's no point me driving a Ferrari if I can't even ride my pushbike correctly.
Thoughts, suggestions, criticisms are all very welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 scriptlet 是一种非常过时的做法。迭代集合就像
一样简单。只要集合是可迭代的,集合的类型并不重要。如果不了解更多关于脚本中实际执行的操作或原因,除了指手画脚之外,很难提供更多帮助。
像决定当前是否选择某个选项之类的事情几乎总是在自定义标记中处理,而不是手动处理 - 通常您根本不需要考虑它,更不用说决定如何自己处理它了。 (Struts 1 标签也是如此;您不必手动执行此操作。)
Using scriptlets is a pretty antiquated practice. Iterating over a collection is as simple as
<c:forEach ...>
. The type of the collection doesn't matter as long as it's iterable.Without knowing more about what you're actually doing in the scriptlets, or why, it's tough to help much beyond finger-wagging.
Things like deciding if an option is currently selected are almost always handled in custom tags, not manually--normally you don't have to think about it at all, let alone decide how to handle it yourself. (That's true of Struts 1 tags as well; you shouldn't have to do that manually.)