How can I pass an object to a JSP tag?
Asked Answered
W

6

31

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.

Whore answered 16/9, 2008 at 18:46 Comment(4)
Note, i don't want my object converted to a string and passed as a string, i want my tag handler to actually have access to the object.Whore
Do you care to choose the right answer?Nieman
@RubensMariuzzo All of them are bad.Managing
@peterh, Well they aren't all that bad.Exsert
S
44

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.

Saire answered 4/8, 2009 at 15:2 Comment(1)
Not this was the question, -1.Managing
V
11
<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.

Villeinage answered 10/12, 2008 at 6:28 Comment(3)
First, the paged scope is a very bad thing. Second: it first serializes myObject and then deserializes. This solution is much worser as a simple scriptlet.Managing
1. What is wrong with page scoped beans? 2. There is no serialisation taking place here. Why do you think that?Hyohyoid
Finally I understood you have right, so you have the bounty.Managing
R
5

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

So

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

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

Reannareap answered 16/9, 2008 at 19:9 Comment(3)
Link is broken. See the Web Archive version here.Antimonyl
It first serializes myObject and then deserializes. Very bad solution.Managing
Had a similar problem, this solved it. Didn't get the problem on serialization and deserialization, since it is transparent -- didn't had to do anything on the tag handler.Sadie
K
3

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.

Kwok answered 16/9, 2008 at 19:48 Comment(1)
It first serializes myObject and then deserializes. Very bad solution.Managing
O
1

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 %>"/>
Obduliaobdurate answered 10/12, 2014 at 8:46 Comment(0)
T
0

Use expression language:

    <wf:my-tag obj="${myObject}" />
Tani answered 16/9, 2008 at 19:1 Comment(2)
We need to add that in some context, prior using it.Villeinage
It first serializes myObject and then deserializes. Very bad solution.Managing

© 2022 - 2024 — McMap. All rights reserved.