混合 JSP/HTML 任何新功能或建议

发布于 2024-08-31 06:47:02 字数 1432 浏览 5 评论 0原文

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

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

发布评论

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

评论(3

秋千易 2024-09-07 06:47:02

这些丑陋的东西被称为scriptlet,是的,从标签库和 EL 诞生的那一刻起,十年来它们就被认为是不好的做法。如今,它们可能对快速原型制作很有用,但仅此而已。

如今,您通常使用 JSTL 之类的标签库来控制页面流量和输出。您通常使用 Expresson Language (EL) 来访问后端数据。您可以使用它们在表示层做很多事情。如果您发现它们中的任何一个都不可能,并且您被迫获取scriptlet,那么代码逻辑最终属于真正的 Java 类。您可以使用 Servlet 类来控制/预处理/后处理请求,您可以使用 Filter 类来过滤请求,可以使用 DAO类来做数据库交互,可以使用Javabean 类来存储/传输/访问数据,您可以使用 Domain 类进行业务逻辑,您可以将 Utility 类用于静态工具。使用 JSP 的目的是:一种用于编写 HTML/CSS/Javascript 等模板文本的视图技术,其中提供了通过 taglibs 和 EL 与后端 Java 代码和数据进行交互的可能性。

这些教程非常适合以正确的方式开始使用它们:初级和中级 JSP/ Servlet 教程高级 JSP/Servlet 教程

也就是说,您所讨论的 内容与 Java/JSP 没有 任何关系。这些是 HTML 元素,指示 Javascript 块。这是一种完全独立的语言,其名称的前 4 个字符和或多或少相似的关键字/语法用法是与 Java 的唯一关系。

Those ugly things are called scriptlets and yes, they are considered bad practice since a decade, since the moment that taglibs and EL were born. Nowadays they may be useful for quick prototyping, but really nothing more than that.

Nowadays, you normally use taglibs like JSTL to control the page flow and output. You normally use Expresson Language (EL) to access backend data. You can do fairly a lot at the presentation layer with them. If you comes to a point that it is not possible with either of them and you're forced to grab scriptlets, then the code logic ultimately belongs in a real Java class. You can use a Servlet class to control/preprocess/postprocess requests, you can use a Filter class to filter requests, you can use a DAO class to do the database interaction, you can use a Javabean class to store/transfer/access data, you can use a Domain class for business logic, you can use an Utility class for static tools. Use the JSP where it is intented for: a view technology to write template text like HTML/CSS/Javascript in which provides the possibility to interact with backend Java code and data by taglibs and EL.

Those tutorials are very good to get started with them all the right way: Beginner and Intermediate JSP/Servlet tutorials, Advanced JSP/Servlet tutorials.

That said, the <script></script> things where you're talking about have by the way nothing to do with Java/JSP. Those are HTML elements which indicate a block of Javascript. This is an entirely independent language with the first 4 characters of the name and a less or more simliar keyword/syntax usage as the only relation to Java.

公布 2024-09-07 06:47:02

您可以使用标签库(Java例如,标准标签库)如果您愿意的话。标准标签库可以帮助您从 HTML 中删除那些丑陋的 if 语句。当然,这些语句是作为标签(类似 xml 的东西)存在的,但它更适合 JSP,并且更容易使用 Eclipse 进行处理。

请注意,不要在 JSP 中混合业务逻辑和视图逻辑。这确实会使 JSP 变得不可读。

You could use tag libs (the Java standard tag lib for instance) if you like. The standard tag libs can help you get those ugly if statements out of the HTML. The statements are there of course, as tags (xml like things), but it fit's better in the JSP, and is easier to work with using Eclipse.

Do be aware that you don't mix business logic and view logic in a JSP. That will really make a JSP unreadable.

梦途 2024-09-07 06:47:02

该线程非常类似于:

JSP -将Java与HTML分离

This thread is very similar to:

JSP - separating Java from HTML

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