mehdi shahdoost
mehdi shahdoost

Reputation: 1489

use spring bean in JSP

i have bean that contain method [void return] and want access to this bean in JSP.

public class A {

  public void run() {}
}

add below code to spring config file.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
     <bean id="a" class="com.example.A"
 >
</bean>

now in my JSP page :

${a.run}

but this solution not work.please help me for access spring bean on JSP page.

Upvotes: 4

Views: 18806

Answers (5)

SergeyB
SergeyB

Reputation: 9868

Have you tried this?

${a.run()}

I haven't used org.springframework.web.servlet.view.InternalResourceViewResolver (use it's superclass instead), but this works if your controller injects a Java object profile which has toJson() method implemented. Don't see why the same syntax wouldn't work as long as a is accessible to the JSP and implements run().

<script>
$(function() {
    var profileJson = ${profile.toJson()};
    ....
})
</script>

This is how is pre-load my page with initial content and save a trip to the back-end on page load.

Upvotes: 0

Amit Parashar
Amit Parashar

Reputation: 1457

@ Dave Newton's comment : "There's still zero reason to be making the service call directly from the view layer". Consider a scenario, where you want to develop a custom tag (say a dropdown which fetches values from a service class based upon the tag's attribute value, in your core web project) . and you provide the TAG implementation in a .tag file. Keeping the service call in the .tag file seems preferable than to update the model in every controller, called prior to render the view which uses the tag. What do you suggest, Using an onload AJAX call in .tag file to fetch the dorpdown content?

Upvotes: 0

Fortunato
Fortunato

Reputation: 1350

You cannot call a method with ${a.run} you need to do #{a.run}.

Upvotes: 0

Aravind A
Aravind A

Reputation: 9707

You could write a scriptlet for this something like

<%
   ApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
   A a = (A) ctx.getBean("yourBeanName");
%>

or use WebApplicationContextUtils if the requests are routed through DispatcherServlet.

You should look into Spring MVC . This would suit you more.

Upvotes: -1

Dave Newton
Dave Newton

Reputation: 160321

Inject the bean into your controller and expose it as part of the model.

But why do you need to call run from the JSP?

JSP EL expects to follow JavaBean naming conventions; this example won't work the way you expect. The easiest option is to rename the method, or provide an additional method, that follows the JavaBean naming convention and calls run.

Edit to reply to comment.

If you need to call a method from a link, you have two (reasonable) options: link to a controller action that calls the injected service, or make an Ajax call to a controller method that calls the injected service.

There's still zero reason to be making the service call directly from the view layer (the JSP).

Upvotes: 7

Related Questions