Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Spring and the fly likes  Accessing Spring beans from jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark " Accessing Spring beans from jsp" Watch " Accessing Spring beans from jsp" New topic
Author

Accessing Spring beans from jsp

Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90

Hi,

I'm doing a course in Spring and my Spring MVC program prints the standard 'Hello World' message on the jsp page. Following are the
files concerned:

controller.HelloWorldController.java
--------------------------------------------------

Dispatcher servlet config file: dispatcher-servlet.xml
-------------------------------------------------------------------------

Entry point for application:index.jsp
--------------------------------------------------

helloworld.jsp
---------------------

web.xml
-------------

and successfully displays the 'Hello World' message on http://localhost:8080/FirstWebAppUsingSpringMVC/helloworld.htm

I was curious if Spring beans could be accessed from the jsp (googled and did some research on the web in this regard) and was
surprised that they indeed could be done (as provided by Spring designers in the form of exposedContextBeanNames and
exposeContextBeansAsAttributes properties of InternalResourceViewResolver).

[sidebar]
Whether business components should be exposed to the view is, I guess, a topic for another thread. I've initiated this topic as I'm more
concerned with the 'how to do it' for learning purposes.
[/sidebar]

To implement the same, I modified
controller.HelloWorldController.java
----------------------------------------------------

Dispatcher servlet config file: dispatcher-servlet.xml
-------------------------------------------------------------------------

helloworld.jsp
---------------------

web.xml
-------------


and introduced
service.HelloWorldService.java (Service bean)
-------------------------------------------------------------------

applicationContext.xml
----------------------------------


However, on running the project, the output on http://localhost:8080/FirstWebAppUsingSpringMVC/helloworld.htm is



Why is the 'Hello World' message not displayed? Where have I gone wrong??.......have been trying to work around this but unable
to find solution.

Much appreciated if the forum members / experts could help in resolving the same.

Thanks,
Sudhir





Ayan mallick
Ranch Hand

Joined: Nov 17, 2012
Posts: 44
<body>
<!-- ${beanName.beanProperty}-->
${helloWorldController.helloWorldService}
</body>



let me try in this way

make your service class a java bean. declare message as a property and generate setter & getter.

then access the property not the object itself what you are doing here.



then ithink it would print the message.

but could you tell me please why do we need to expose a controller or service bean i mean from business perspective.any scenario where we need to enable such functionality.

Regards

Ayan

Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90
Thank you for your response. As suggested
Ayan mallick wrote:
make your service class a java bean. declare message as a property and generate setter & getter.

then access the property not the object itself what you are doing here.



then ithink it would print the message.


I'll try out the same and get back to you.

Ayan mallick wrote:but could you tell me please why do we need to expose a controller or service bean i mean from business perspective.


As I'd mentioned in my original post, spring does provide the properties that enables spring beans - specific beans or all the beans in the application - to be exposed to the view. I tried implementing the same for learning purposes only (exposing business components to the view is debatable and not generally encouraged, from what I've learned on the web).

There are always exceptions to the rule and
Ayan mallick wrote:any scenario where we need to enable such functionality.

this could be a possible explanation (in relation to a specific query by a poster on another forum.
Whether you agree or disagree is entirely up to you ).

Regards,
Sudhir

Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90

Accessing the bean property

Sudhir Srinivasan wrote:Thank you for your response. As suggested
Ayan mallick wrote:
make your service class a java bean. declare message as a property and generate setter & getter.

then access the property not the object itself what you are doing here.



then ithink it would print the message.


I'll try out the same and get back to you.


instead of the bean itself works (although my original intent to expose just the object does not seem to be implementable....). A quick summary of how it works:

a) declare property 'message' in the service bean
b) generate setter & getter for the same
c) controller bean to reference service bean in the dispatcher servlet config file
d) in applicationContext.xml, define 'message' property in bean 'helloWorldService' and inject the "Hello World" message into the property.


Regards,
Sudhir
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing Spring beans from jsp
 
Similar Threads
org.springframework.web.servlet.DispatcherServlet noHandlerFound
No mapping found for HTTP request with URI - Why erreur404
Neither BindingResult nor plain target object for bean name 'command' available as request attribute
Problem running app under Tomcat v7
Spring MultiActionController is not working