• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP not setting Bean properties  RSS feed

 
Mam Kat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am facing this problem for a while and could find any answer. Please help me with this.
I am using Tomcat5.0, j2sdk1.4.2
I am having the useBean syntax as
<jsp:useBean id="mybean" scope="session" class="hello.plans">
<jsp:setProperty name="mybean" property="*"/>
</jsp:useBean>

in my main jsp page.
i am including another jsp page using <%@include file="sub.jsp"%>

and in this sub.jsp page i gave the form action as another jsp page. but when the submit button in the form of 'sub.jsp' is pressed, the values in "mybean" are not getting updated. I made bean property names for 'hello.plans' same as form field names in 'sub.jsp'. Is there something that i am not doing? or doing wrong? any help is appreciated.
Thanks,
Mamatha.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure of your page structure, you might want to post your code.

It sounds like you have the:
<jsp:setProperty name="mybean" property="*"/>
tag in the page with the form. It should be in the page that in that page that receives the post (not the one that sends it).

Also: Java conventions dictate that class names start with an upper case letter. Not following the conventions makes it very difficult for others to read your code.
 
Mam Kat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to set some properties from main.jsp page and some more from sub.jsp page. So i did put <jsp:setProperty name="mybean" property="*"/> in both the pages. Can i do something like that?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The <jsp:setProperty name="mybean" property="*"/> tag works by parsing the HTTP form parameters, matching them up to the setter methods in your bean, and setting those properties with the form values.

That tag would need to be in the JSP that is handling the form request.

The <%@ include ...%> merges the code before the generated servlet code is created. This means, that the two pages are, essentially, one. You wouldn't need to have the setProperty tag or the useBean tag in each of them. Just make sure that the setProperty tag comes before the include directive.

If all of this seems confusing, it's because it is.
This is why you will often see me (and several of the more seasoned developers who hang around here) recommending that new developers first learn servlets. Once you understand the underlying concepts these tags (which are abstractions of the underlying concepts) are a lot easier to learn.
 
Mam Kat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben thanks for your reply.
I know that <%@include makes it a single page. As it's not setting the properties i just tried by putting <jsp:setProperty/> in both the pages.
Right now i am explictly calling request.getParameter() for different form fields and setting the corresponding bean properties. don't know why introspection is not working by itself.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My larger point is that, if Page A contains a form who's action is Page B,
putting a setProperty tag in Page A (or one of it's includes) is pointless.

The parameter values do not exits in the request object until the form has been posted. Page B would be able to retrieve them as request parameters.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you defined appropriate getters/setters for the properties?

ie

jsp:setProperty works with the methods, not with the actual properties.
 
Mam Kat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan,
I defined getter and setter methods properly. thanks for your suggestion.
Ben,
You are right. thanks for the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!