Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass an object to a JSP tag?

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.

like image 904
Joe Bienkowski Avatar asked Sep 16 '08 18:09

Joe Bienkowski


2 Answers

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.

like image 147
dfrankow Avatar answered Sep 20 '22 19:09

dfrankow


<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.

like image 36
Adeel Ansari Avatar answered Sep 24 '22 19:09

Adeel Ansari