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

jsp:useBean and jsp:setProperty question  RSS feed

 
Henrik Engert
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the following code:


According to this there is a conditional statement when putting the <jsp:setProperty/> inside the body of <jsp:useBean/>

In my mind this means that if I don't set this property in the request like this:



The resulting value should be "Fred", but I get a nullpointer exception...

Why?

[ January 05, 2007: Message edited by: Henrik Engert ]
[ January 05, 2007: Message edited by: Henrik Engert ]
 
Joel Jorgensen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "code" between of the <jsp:useBean> and </jsp:useBean> tags only gets evaluated if there is no bean of the specified class in the specified scope. This may not be the best analogy, but I tend to think of it as sort of a constructor for the bean if it isn't there. I suspect that in your case the bean is there, but that particular property of the bean is null. So the code to populate that property does not get evaluated (the bean already exists, so the conditional "creation" code between the jsp:useBean tags isn't executed).

I recommend looking into Expression Language (EL) and the JSTL core library tags for conditionals that you can use to see whether the property has been populated or not. There may be other ways to do it as well (especially if you're using Struts), but I'm a huge EL fan (it generally seems much tidier and more intuitive to me as a programmer) so that's what I'm recommending
[ January 05, 2007: Message edited by: Joel Jorgensen ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you testing this?

In your useBean tag, you've set the scope to "page".
In the Java code, you're binding the person object to "request".
 
Henrik Engert
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

I guess what I am asking is the following:

If I use the following code:



and I don't create a person to set with request.setAttribute("person", p);

I would think I would get Fred as a result.....

[ January 08, 2007: Message edited by: Henrik Engert ]
[ January 08, 2007: Message edited by: Henrik Engert ]
 
Joel Jorgensen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henrik Engert:
Hi again,

I guess what I am asking is the following:

If I use the following code:



and I don't create a person to set with request.setAttribute("person", p);

I would think I would get Fred as a result.....

[ January 08, 2007: Message edited by: Henrik Engert ]

[ January 08, 2007: Message edited by: Henrik Engert ]


In this case, the problem is that you are not specifying a 'class' attribute for the new bean to have. It tries to cast it to be type com.example.model.Person, but without a 'class' attribute it doesn't know what class to use to create the new bean. If you add a class attribute of com.example.model.Person, it should resolve your null pointer exception problem (or at least get you closer
[ January 08, 2007: Message edited by: Joel Jorgensen ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!