aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Set JavaBean property with set action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Set JavaBean property with set action" Watch "Set JavaBean property with set action" New topic
Author

Set JavaBean property with set action

Edmund Yong
Ranch Hand

Joined: Nov 16, 2003
Posts: 164
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.


SCJP 1.2, SCWCD 1.4
Sergey Tyulkin
Ranch Hand

Joined: May 10, 2005
Posts: 87
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

Joined: Nov 16, 2003
Posts: 164
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

Joined: Nov 16, 2003
Posts: 164
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

Joined: Nov 16, 2003
Posts: 164
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

Joined: May 10, 2005
Posts: 87
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

Joined: Nov 16, 2003
Posts: 164
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" />
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set JavaBean property with set action