aspose file tools*
The moose likes Spring and the fly likes Call spring bean inside JSP/struts2 bean from a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Call spring bean inside JSP/struts2 bean from a JSP" Watch "Call spring bean inside JSP/struts2 bean from a JSP" New topic
Author

Call spring bean inside JSP/struts2 bean from a JSP

Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
Hi,
I want to call a spring bean from a jsp page.
I know that spring org.springframework.web.context.support.ServletContextAttributeExporter will do the trick,
but I want to call the spring bean from from the model.
If I use ClassPathXmlApplicationContext in the bean class, then I cannot call the spring session bean.

Anyone help?
thanks

Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
You should not call Spring managed beans from JSPs.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
Kengkaj Sathianpantarit wrote:You should not call Spring managed beans from JSPs.


That's why I wanna use ClassPathXmlApplicationContext,
but it only allows me to load applicationContext.xml from classpath.

Anyway to load the spring /WEB-INF/applicationConext.xml from java bean?
I hope FileSystemXmlApplicationContext wouldn't be the answer since I don't want full path to be used.

thanks
Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
okay,
I solved the problem if I implements ApplicationContextAware to the java bean.

thanks
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
I don't know what you are trying to do. Could you please to post source code and explain what you want to achieve and what is the problem?
Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If the bean is exposed as a Struts 2 action property you shouldn't need to do anything special anyway, but it's unclear what your usecase is.
Patrick Kok
Ranch Hand

Joined: Nov 12, 2009
Posts: 42
thanks
We use sitemesh for managing our layout, which are jsp in template format.
Since they are not struts2 action, I need to invoke spring bean from jsp page. This was what I was trying to do.
This jsp page is to list all products information in table form with search and sort. There will be one struts2 action and one bean working together on this page.
Now, I use a struts2 bean ( or jsp bean) which implements ApplicationContextAware to get application context and invoke method I need.

thanks again
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What's a "Struts 2 bean"?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Check out the Core Spring documentation, it shows how to integrate with Struts, although I know for Struts 1, but not sure about Struts2/WebWorks(I forgot the real name of the product that changed their name to Struts 2)

Also

That's why I wanna use ClassPathXmlApplicationContext,
but it only allows me to load applicationContext.xml from classpath.


You can load from anywhere, not just ClassPath. Just prefix your location with file: or http: and you can access it by file location, or from within your WEB-INF directory.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call spring bean inside JSP/struts2 bean from a JSP