• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Dinkar Chaturvedi
Ranch Hand
Posts: 42
Android Java Spring
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 42
Android Java Spring
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic