如何将对象传递给 JSP 标记?

发布于 2024-07-04 03:56:48 字数 313 浏览 8 评论 0原文

我有一个 JSP 页面,其中包含一个 scriplet,我在其中实例化一个对象。 我想将该对象传递给 JSP 标记而不使用任何缓存。

例如,我想实现这一点:

<%@ taglib prefix="wf" uri="JspCustomTag" %>

<% 
 Object myObject = new Object();
%>

<wf:my-tag obj=myObject />

我试图避免直接与任何缓存(页面、会话、servletcontext)交互,我宁愿让我的标签处理它。

I have a JSP page that contains a scriplet where I instantiate an object. I would like to pass that object to the JSP tag without using any cache.

For example I would like to accomplish this:

<%@ taglib prefix="wf" uri="JspCustomTag" %>

<% 
 Object myObject = new Object();
%>

<wf:my-tag obj=myObject />

I'm trying to avoid directly interacting with any of the caches (page, session, servletcontext), I would rather have my tag handle that.

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

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

发布评论

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

评论(6

猫瑾少女 2024-07-11 03:56:48

您可以使用“<%= %>” 直接在标签中获取对象值:

    <wf:my-tag obj="<%= myObject %>"/>

并获取该对象内任何变量的值,您可以使用“obj.parameter”获取该值,如下所示:

<wf:my-tag obj="<%= myObject.variableName %>"/>

You can use "<%= %>" to get the object value directly in your tag :

    <wf:my-tag obj="<%= myObject %>"/>

and to get the value of any variable within that object you can get that using "obj.parameter" like:

<wf:my-tag obj="<%= myObject.variableName %>"/>
我恋#小黄人 2024-07-11 03:56:48

我在这里寻找的一个略有不同的问题:“如何将对象传递给标记文件?”

答案:使用属性指令的“type”属性:

<%@ attribute name="field" 
              required="true"
              type="com.mycompany.MyClass" %>

type 默认为 java.lang.String,因此如果没有它,如果您尝试访问对象字段,则会收到错误消息,指出无法找到 String 类型的字段。

A slightly different question that I looked for here: "How do you pass an object to a tag file?"

Answer: Use the "type" attribute of the attribute directive:

<%@ attribute name="field" 
              required="true"
              type="com.mycompany.MyClass" %>

The type defaults to java.lang.String, so without it you'll get an error if you try to access object fields saying that it can't find the field from type String.

桃酥萝莉 2024-07-11 03:56:48
<jsp:useBean id="myObject" class="java.lang.Object" scope="page" />
<wf:my-tag obj="${myObject}" />

不鼓励在 JSP 页面中使用 Scriptlet。 它扼杀了模板语言的目的。

<jsp:useBean id="myObject" class="java.lang.Object" scope="page" />
<wf:my-tag obj="${myObject}" />

Its not encouraged to use Scriptlets in JSP page. It kills the purpose of a template language.

木落 2024-07-11 03:56:48

最初的语法是重用 '<%= %>'

因此,

<wf:my-tag obj="<%= myObject %>" />

请参阅Sun 标签库教程的这一部分获取示例

The original syntax was to reuse '<%= %>'

So

<wf:my-tag obj="<%= myObject %>" />

See this part of the Sun Tag Library Tutorial for an example

半葬歌 2024-07-11 03:56:48

对我来说,只有当我使该变量可访问时,表达式语言才起作用,例如将其放入页面上下文中。

<%  Object myObject = new Object();
    pageContext.setAttribute("myObject", myObject);
%>
<wf:my-tag obj="${myObject}" />

否则 tas 收到 null。

无需额外努力即可工作。 还有<%=%> 提供 jsp 编译时类型验证,而 El 仅在运行时验证。

For me expression language works only if I make that variable accessible, by putting it for example in page context.

<%  Object myObject = new Object();
    pageContext.setAttribute("myObject", myObject);
%>
<wf:my-tag obj="${myObject}" />

Otherwise tas receives null.

And <wf:my-tag obj="<%= myObject %>" /> works with no additional effort. Also <%=%> gives jsp compile-time type validation, while El is validated only in runtime.

像你 2024-07-11 03:56:48

使用表达语言:

    <wf:my-tag obj="${myObject}" />

Use expression language:

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