Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsp:param with Java class

I have a JSP file that includes another JSP file. The first JSP should pass an instance of a Java class (widget) to the second JSP file.

This is what I have:

The first JSP:

<jsp:include page="/container/SpecialWidget.jsp">
     <jsp:param name="widget" value="${widget}"/> // widget is a .Java POJO
</jsp:include>

The second JSP:

${param.widget.id}

The problem is that this code gives an error (it says it doesn't know ID). If I omit the ".id" part, the page prints the Java code for the Java class, which means the class has been transferred correctly. If I change the ${widget} rule of the first page in, for example, ${widget.id} and I try to print ${param.widget}, everything works fine.

My question: Why can't I pass a Java class and directly call upon its attributes? What am I doing wrong?

Edit: error message: Caused by: javax.el.PropertyNotFoundException: Property 'id' not found on type java.lang.String

like image 625
John Hendrik Avatar asked Nov 21 '12 15:11

John Hendrik


People also ask

What is use of param in jsp?

When jsp:include or jsp:forward is executed, the included page or forwarded page will see the original request object, with the original parameters augmented with the new parameters and new values taking precedence over existing values when applicable.

What is the difference between jsp param and jsp params elements?

jsp:element: is used for defining XML elements dynamically. jsp:plugin: is used for embedding other components (applets). jsp:param: is used for setting the parameter for (forward or include) value.

What is request getParameter in jsp?

getParameter is a function name in JSP which is used to retrieve data from an HTML/JSP page and passed into the JSP page. The function is designated as getParameter() function. This is a client-side data retrieval process. The full function can be written as request. getParameter().


2 Answers

I managed to fix my problem with the following code:

<c:set var="widget" value="${widget}" scope="request" />
<jsp:include page="/SOMEWHERE/SpecialWidget.jsp"/>

Thank you both for your help:) It saved my day

like image 159
John Hendrik Avatar answered Oct 02 '22 19:10

John Hendrik


When you pass the variable ${widget} it is translated at request time to a string (widget.toString()). This value is then passed to the second JSP as a String, not as the original java object.

One approach to access the object's values is setting the parameter's value with the attribute's value:

<jsp:param name="widgetId" value="${widget.id}"/>

Then use the code bellow on the second JSP:

${param.widgetId}

You can also set widget as an request attribute and use it on the second page as ${widget.id} or ${request.widget.id}. I suggest you use the second approach.

like image 26
Rafael Borja Avatar answered Oct 02 '22 20:10

Rafael Borja