如何将对象传递给JSP标记? [英] How can I pass an object to a JSP tag?

查看:114
本文介绍了如何将对象传递给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.

这篇关于如何将对象传递给JSP标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆