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

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

问题描述:

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

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.

例如我想完成此任务:

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

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

<wf:my-tag obj=myObject />

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

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

我在这里寻找一个稍微不同的问题:你怎么通过对象到标记文件?

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

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

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

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

类型默认为java.lang.String ,如果没有它,如果你试图访问对象字段说它可以找不到String类型的字段。

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.