如何在中调用类标签

发布于 2024-10-09 20:46:32 字数 196 浏览 0 评论 0原文

我的 JSP 中的代码是这样的:

<tag:loggedin>
    <a href="./logout">logout</a>
</tag:loggedin>

当我无法使用 <% 脚本元素时,如何调用类或将脚本代码放入标记内?

My code in my JSP is something like this:

<tag:loggedin>
    <a href="./logout">logout</a>
</tag:loggedin>

How can I call a class or put scripting code inside tags when I can't use <% scripting elements?

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

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

发布评论

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

评论(1

您不能使用 scriptlet 可能是有原因的 (<%)。

也许您团队中的架构师禁止通过 web.xml 中的声明来使用它,或者您正在使用 jspx(jsp 文档,又名 jsp 的 XML 版本)?后者在技术上允许您插入脚本,但在实践中它太尴尬而无法真正工作。

因此,你最后一个问题的答案很简单:不要。

不要试图找到一种将脚本代码放入 JSP 中的方法,这是一个不好的做法。

相反,使用 Servlet 将某些内容放入请求范围中。然后,可以通过 JSP 页面上的 EL 表达式来引用其中放置的任何内容。 Servlet 可以调用任何它想要的 Java 类或直接执行任何类型的 Java 代码。

在大多数情况下,使用 JSF、Struts 或 Tapestry 等 Web 框架可能比使用 Servlet 和 JSP 页面自行拼凑更容易。对于最小的网络项目来说,“旧方法”可能仍然很有趣,可以了解一下较低级别的东西是如何工作的。

There probably is a reason why you can't use scriptlets (<%).

Maybe the architect on your team has forbidden to use this via a declaration in web.xml, or you are using jspx (jsp documents, aka the XML version of jsp)? The latter technically allows you to insert scriptlets, but it's too awkward in practice to really work.

The answer to your last question is thus a simple: don't.

Do not try to find a way to put scripting code in a JSP, it's a bad practice.

Instead, use a Servlet to put something in the request scope. Then whatever has been put there can be referenced via an EL expression on your JSP page. The Servlet can call any Java class it wants or execute any kind of java code directly.

In most cases it's probably easier to use a web framework like JSF, Struts or Tapestry then to cobble something together yourself using Servlets and JSP pages. For the smallest of web projects the 'old way' might still be interesting to learn a little how the lower level stuff works.

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