• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding c:set

 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can c:set be used to set a property of a bean that has only a getter method for that property?

<c:set target="$(person}" property="name" value="abc"/>

Assuming there is a "person" attribute in some scope, will this work if the Person class has only a getName() method but not a setName() method? ie it's name property is read-only?

Its intuitive to figure out that jsp:setProperty won't work

<jsp:setProperty name="person" property="name" value="abc"/>

will get translated to person.setName("abc") which won't compile because person does not have a setName method.

But what will happen with c:set? will it give an error?

Appreciate your responses.
 
Sebastien Col
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, your bean must expose a setter for the property you're setting through the <c:set>.
I tried with a public property, it doesn't work either.

Sebastien
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for trying it out and responding Sebastien !!
 
Sehrish Hasan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I thought a bean is not a bean if it doesnt expose the getter and the setter method both. It shouldnt work because of this reason right???

Would <jsp:useBean id=......> work in this case? I thought it wouldnt if its not exposing the setter method? Please clarify...

Thanks,
M
 
Sergey Tyulkin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a confusing question.. As far as i remember Java Beans specification requires both setter and getter. But in the JSP/Servlet technology we can use beans, that can setter or getter alone, if the user doesn't use them in opposite way.
For example, if the user calls only <jsp:getProperty>, then container (or compiler, or anything) will not complain, if the bean have no setter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic