File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes difference between c:set with-body and no-body versions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "difference between c:set with-body and no-body versions" Watch "difference between c:set with-body and no-body versions" New topic
Author

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

Dinkar Chaturvedi
Ranch Hand

Joined: Mar 27, 2007
Posts: 42

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

Joined: Jan 10, 2002
Posts: 61193
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dinkar Chaturvedi
Ranch Hand

Joined: Mar 27, 2007
Posts: 42

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

Joined: Jan 10, 2002
Posts: 61193
    
  66

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: difference between c:set with-body and no-body versions