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.

peterh
  • 11,875
  • 18
  • 85
  • 108
Joe Bienkowski
  • 311
  • 1
  • 3
  • 3

6 Answers6

43

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.

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
dfrankow
  • 20,191
  • 41
  • 152
  • 214
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.

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
  • 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. – peterh Dec 09 '14 at 16:05
  • 2
    1. What is wrong with `page` scoped beans? 2. There is no serialisation taking place here. Why do you think that? – Steve C Dec 10 '14 at 05:13
  • Finally I understood you have right, so you have the bounty. – peterh Dec 12 '14 at 09:33
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

Garth Gilmour
  • 11,124
  • 5
  • 25
  • 35
  • Link is broken. See the Web Archive version [here](http://web.archive.org/web/20090130012629/http://java.sun.com/products/jsp/tutorial/TagLibraries16.html). – Behrang Mar 05 '14 at 10:08
  • It first serializes myObject and then deserializes. Very bad solution. – peterh Dec 09 '14 at 16:04
  • 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. – Rafael Santos Aug 16 '18 at 11:45
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.

Pavel Feldman
  • 4,621
  • 7
  • 30
  • 31
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 %>"/>
Mike Clark
  • 1,860
  • 14
  • 21
0

Use expression language:

    <wf:my-tag obj="${myObject}" />
Brian Matthews
  • 8,506
  • 7
  • 46
  • 68