Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set JavaBean property with set action

 
Edmund Yong
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a simple JavaBean like this:

<%!
class Dog {
private String name;
public Dog() {}
public void setName(String name) { this.name = name; }
public String getName() { return name; }
}
%>

In the same JSP, I tried to set the name property using the <c:set> tag:

<%
Dog dog1 = new Dog();
%>

<c:set target="dog1" property="name" value="snoopy" />

Tomcat complains that "name" is an invalid property.

I tried with

<c:set target="${dog1}" property="name" value="snoopy" />

but still no good. I think target evaluates to null.

So what's the correct way to set the property using <c:set>?

Thanks in advance.
 
Sergey Tyulkin
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
scriptlet variable are not visible to jstl expressions, You should put the object into one of the scopes.
For example,
<%
Dog dog1 = new Dog();
request.setAttribute("dog1", dog1);
%>

then you can use the SECOND example (first is invalid)
<c:set target="${dog1}" property="name" value="snoopy" />
 
Edmund Yong
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I followed your suggestion, but I got an exception on the page:

java.lang.IllegalAccessException: Class org.apache.taglibs.standard.tag.common.core.SetSupport can not access a member of class org.apache.jsp.result_jsp$Dog with modifiers "public"
 
Edmund Yong
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know where my problem is. I should declare the Dog class as public.

Also, I've realized that you cannot specify the scope attribute together with the target attribute. In other words, you can't choose which scope you want. For example, in the following, you can only set the dog1's name, but there is no way to set dog2's name.

<%
Dog dog1 = new Dog();
Dog dog2 = new Dog();
pageContext.setAttribute("dog", dog1);
request.setAttribute("dog", dog2);
%>

<c:set target="${dog}" property="name" value="snoopy" />
 
Edmund Yong
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you don't even need to put the JavaBean object into one of the scope. The JSTL can still recognize the object:

<%
Dog dog1 = new Dog();
%>

<c:set target="<%= dog1 %>" property="name" value="snoopy" />

<%
out.print(dog1.getName()); // prints "snoopy"
%>

The target attribute actually refers to the actual object itself.
 
Sergey Tyulkin
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote "scriptlet variables are not visible to jstl expressions"

Of course, you can use them in JSTL custom tags

But JSTL expression is something within "${}". And i think you already see, that you cannot use your last dog1 variable in this action:

<c:set target="${dog1}" property="name" value="snoopy" />

BTW, how do you realize, that "you cannot specify the scope attribute together with the target attribute"?

It's easy, man:
<c:set target="${requestScope.dog1}" property="name" value="snoopy"
<c:set target="${sessionScope.dog1}" property="name" value="snoopy"
[ August 11, 2005: Message edited by: Sergey Tyulkin ]
 
Edmund Yong
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, how do you realize, that "you cannot specify the scope attribute together with the target attribute"?

It's easy, man:
<c:set target="${requestScope.dog1}" property="name" value="snoopy"
<c:set target="${sessionScope.dog1}" property="name" value="snoopy"


Oh, I didn't know that you can do that! What I meant previously is that you can't use the "scope" attribute together with "target". So the following is illegal:

<c:set target="${dog1}" property="name" value="snoopy" scope="request" />
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic