• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

difference between c:set with-body and no-body versions

 
Ranch Hand
Posts: 49
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Folks,
given the scriptlet code in a jsp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<%Person p = new Person();
p.name = "John";
Dog d = new Dog();
d.setBreed("Poodle");
request.setAttribute("personObj",p);
request.setAttribute("dogObj",d);%>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

and assuming that Person class is a bean with two properties name(String) and dog(Dog)

why does this work:
<c:set target="${personObj}" property="dog" value="${dogObj}" />

but this doenst:
<c:set target="${personObj}" property="dog">
${dogObj}
</c:set>

With the second version, I get the following exception:
javax.servlet.ServletException: Attempt to convert String "com.example.model.Dog@106989e" to type "com.example.model.Dog", but there is no PropertyEditor for that type

As far as I understood, its because the no-body version will accept any java objects whereas the body version will accept only strings unless something called a "Property Editor" is not constructed for it.
Is my understanding correct or am i missing something?
 
Sheriff
Posts: 67642
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dinkar Chaturvedi:
As far as I understood, its because the no-body version will accept any java objects whereas the body version will accept only strings



Your understanding is not correct. What's happening is that you are trying to feed the property, which accepts a Dog, a string. And a string cannot be converted to a Dog.

When you use the "body version" of the tag, it will take whatever is inside the tag as the value. But what is inside the tag? renderred template text! Your body content, to include any whitespace and line terminators, is converted to text for emission. So the content you are feeding the tag in this case:



is: a line tyerminator followed by the renderred value of the expression ${dogObj} (which is whatever toString() on that object evaluates to) and another line terminator.

If that's not what you want, don't use that format. That format is to be used when you want the value to be the renderred version of whatever is in the body of the tag. It's not just an alternate notation for the no-body format.
 
Dinkar Chaturvedi
Ranch Hand
Posts: 49
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Bear,
thanks for the explanation. It helped me look at the code with a new perspective.

But does that mean that I cant use the "with-body" version for setting non-string java objects as bean property(as in this case the Dog object)?

Besides the "no-body" version, that i mentioned in my previous post, is there any other way of writing the ${dogObj} so that it takes it as an object and not as redendered text?
 
Bear Bibeault
Sheriff
Posts: 67642
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dinkar Chaturvedi:

But does that mean that I cant use the "with-body" version for setting non-string java objects as bean property(as in this case the Dog object)?



That is correct. A renderred JSP fragment is always text.

Besides the "no-body" version, that i mentioned in my previous post, is there any other way of writing the ${dogObj} so that it takes it as an object and not as redendered text?



The value attribute is the other means.
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic