Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Other Application Frameworks and the fly likes DWR and JdbcTemplate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "DWR and JdbcTemplate" Watch "DWR and JdbcTemplate" New topic
Author

DWR and JdbcTemplate

Charles Flynn
Greenhorn

Joined: Oct 15, 2007
Posts: 9
Hello,
I just started with Spring and I'm trying get DWR working. Can I use a JdbcTemplate with DWR?
I set up a datasource in my applicationContext.xml but when I try and run the app it never finds the datasource. Let me know if I need to post any code, I thought maybe someone would know right away what my problem was.


Thanks
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Buddy,

does your JDBCTemplate (or your class that uses it) work at all?

Herman
Charles Flynn
Greenhorn

Joined: Oct 15, 2007
Posts: 9
Hi Herman,
Yes, it will work if I set the datasource properties myself:


Thanks for the help
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Buddy/Enoch,

Did you look at http://getahead.org/dwr/server/spring

Just define the class that you made as a "Spring bean", and this site will tell you all that you need.
Most examples will call this file applicationContext.xml

Actually: you can also define dataSource, jdbctemplate etc as "Spring beans"
instead of coding it like you did

Herman
Charles Flynn
Greenhorn

Joined: Oct 15, 2007
Posts: 9
Hi Herman,
So I would define my JdbcTemplate and datasource in the dwr.xml instead of the applicationContext.xml? Would it look something like this?



Thanks again
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Enoch,

First: define your Spring beans in some .xml file
(and make sure that they work without use if DWR).

Second: make them accessible to Javascript with by DWR


Herman

I'm not sure though why you want to call "JdbcTemplate" from javascript.
It's just a helper class, it does not know which SQL to execute by itself.
Charles Flynn
Greenhorn

Joined: Oct 15, 2007
Posts: 9
Hi Herman,
Not sure what I'm doing wrong, it's still having a problem. I put a try-catch block to see what error it was throwing, the error gets caught but the error message is always null.

Thanks
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
Enoch,

probably NullPointerException then.

But where in your code, and how do you call that code.
Are all the Spring beans you defined created? (check your logs)

Herman
[ October 18, 2007: Message edited by: Herman Scheltinga ]
Charles Flynn
Greenhorn

Joined: Oct 15, 2007
Posts: 9
Herman,
I'll post part of my code, can you take a look and see what I've done wrong.

Thanks




Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi,

Looks OK to me, it would be a little better if HomeController was in a package.
I'll try this at home later (probably tonight).

I still don't know which line of your code throws the NullPointerException, and don't know what program you ran that caused the exception.

And did you test this code without DWR (just with Spring)?

Herman
Charles Flynn
Greenhorn

Joined: Oct 15, 2007
Posts: 9
Hi Herman,
It fails when it calls


I have another class I tried in Spring and it works.

Thanks for taking the trouble to look at this
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi,

glad you got it working.

Can you tell us why it is working now: what's different in this other class?

Herman
Charles Flynn
Greenhorn

Joined: Oct 15, 2007
Posts: 9
Herman,
Sorry, I guess I didn't explain it clearly. It's working in the other class with Spring because it's not using DWR it's using MVC. For what ever reason it's not seeing the DataSource or the JdbcTemplate I have defined in the config files when I use DWR.
However I added this code im my class and I'm able to get the datasource and I can run the query. Do you think that the "Context.xml" is not being loaded and that's why the DataSource is null?

Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Enoch,

yes, I do think that Context.xml is not being loaded.

Default name for that file is applicationContext.xml, default location is in WEB-INF. If you want it differently, add to you web.xml:


Also: Spring needs commons-logging.jar (typically in WEB_INF/lib), if you want to see any logging. Without further configuration it will end-up in your console, which is fine for studying.

Like
INFO: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [dataSource,jdbcTemplate,HomeController]; root of BeanFactory hierarchy]

Herman
Charles Flynn
Greenhorn

Joined: Oct 15, 2007
Posts: 9
Hi Herman,
I'm just going in circles with this. I added the commons-logging.jar and you were right about the NullPointerException. I can post the log if that would be helpful, or do you have a working example that you would be willing to post.

Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: DWR and JdbcTemplate
 
Similar Threads
autowiring remote beans
Why is HibernateTemplate not created using IOC in the reference document ?
Spring Question: How to realize IOC in stand alone programs?
DataSource null in DWR
Spring Struts implementation for Database conncetion problem in Login module