• Post Reply Bookmark Topic Watch Topic
  • New Topic

EL expression not working in jsp:setProperty  RSS feed

 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Tomcat 6, JSP 2.5 and JSTL 1.1. Trying to initialize some beans in a foreach loop. I created a small test app to try and figure this out, but I'm stuck after looking at this code for so long. This bean just has a single string property called first name. Creating beans like so:



Put the bean names in an array:


And I can successfully loop through them like so. The bean names print out as expected.


So far, so good. So why does this code throw a null pointer exception?


I can see that the array is being created and read correctly, and if I replace the EL expression in the setProperty action with the actual bean name, the firstName property is set correctly. So why isn't the EL expression in the setProperty action being replaced with the string from my array?
 
Mike Zal
Ranch Hand
Posts: 144
Fedora Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are already using JSTL, why don't you just use the set tag?


I thought that setProperty could not accept EL because it was supposed to be used in conjunction with the useBean tag.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the set and get property actions are so 2001!

But... target, not taget.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some attributes that support runtime expressions, and some that do not.

In the case of the setProperty tag, the "name" attribute is not allowed to be dynamic.

Instead, you could use the JSTL <c:set> tag.
The <c:set> tag requires an actual bean as its target.
The array we are looping on only has names, so we look up the bean by its name using the implicit variable "pageScope"


 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone! I was not familiar with the c:set tag. I learn something new everyday; that's what keeps this job interesting.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest two things:
  • Read through the JSTL Specification to learn what's available. It's a fairly easy read.
  • Avoid scriptlets. They've been discredited since 2002.
  •  
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!