This week's giveaway is in the Java/Jakarta EE forum.
We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!
See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

evaluate dynamic expression language  RSS feed

Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a top level JSP that <jsp:include>'s a JSP which contains form fields. This second included JSP contains common fields that are reused among other top level JSP pages. The different jsp pages. are backed by different (Struts) form beans, and have different levels of nested beans.

What I did to make the form consistent was pass in a parameter for a "prefix", that is the notation of the nested beans up to the nested bean that contains these form fields.

so I have

on the first invocation, and

on the second top level form, which has different nested bean structure, but the same leaf bean instance the form is working with, an instance of a mailing address bean.

then in the included form JSP:

This part works nicely.

Now I am adding some conditional logic, like

where form is a page context attribute of the form bean instance.
This works in the first case, where the prefix represents the simple single bean name (address), but fails when it is a compound value (location.address).

I have been able to get around the expression language not working by using a scriptlet that calls the beanutils:

In other cases, I would also want to do something like this with indexed beans,
such as when prefix = "locations.item[1].locationModel".

I also tried writing the entire expression out.

I am also able to use the scriptlet calling beanutils like I did above to get around this, but it is not as elegant as I would like it to be,
and this really gets complicated when I would want to use the variable expression with <c:forEach items="${form[prefix].config.columns}" type of places.

So I was wondering if there was a better way to do this using the expression language syntax.
That is, it is possible for the expression language evaluate the value of a variable expression ?

I am using Servlet 2.4, JSP 1.2, JSTL 1.1
[ July 09, 2006: Message edited by: Travis Hein ]
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Travis Hein:

So I was wondering if there was a better way to do this using the expression language syntax.

Both EL and JSTL are best used in a true MVC environment.
In an MVC environment, all the HTTP flow issues are handled in a servlet and the real business functionality (model) is handled in beans. Only when all the processing is done does the context get passed to a JSP (view) for display. JSTL and EL allow the JSP developer to iterate over and do some simple scripting with the results in order to present it in the form of a web page.

If you find yourself needing to do the complex things you're asking about, you may want to rethink your architecture. Perhaps a lot of that logic could be worked out before forwarding to the JSP in the first place.

This next part is my opinion:
If you're going to use scriptlets in your JSPs, don't bother to use EL and JSTL. The only thing worse than seeing scriptlets in a JSP is seeing a combination of all of the available syntaxes (sp) in one page. Either go with scriptless pages and allow no scriptlets or stick to scriptlets alone.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!