4

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.

mehdi shahdoost
  • 1,469
  • 5
  • 17
  • 27

5 Answers5

7

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).

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • i have Service method that call automatically [Scheduling with spring] but i need run this method manually with button from JSP page.i call method that return String value but EL syntax don't show anything or evaluate any result :(. – mehdi shahdoost Jan 09 '12 at 12:42
  • @mehdishahdoost No, it won't; I said why not. But this is still an inappropriate way of doing it--see edit. – Dave Newton Jan 09 '12 at 12:49
0

@ 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?

Amit Parashar
  • 1,447
  • 12
  • 15
  • I didn't see this, because it's not an answer to the question, and it wasn't a comment on my question. But: 1) I wouldn't consider that making the call in the JSP since the call itself is hidden in the tag, e.g., the data retrieval mechanism is not the responsibility of the view writer, rather the tag. 2) In a properly-architected app adding additional data to the view layer shouldn't be all that difficult anyway. 3) Not that much of this matters 10-ish years later. – Dave Newton Aug 10 '20 at 21:36
  • 1
    @DaveNewton Things have changed now. Probably not relevant now. – Amit Parashar Aug 11 '20 at 03:59
0

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.

SergeyB
  • 9,478
  • 4
  • 33
  • 47
0

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

Fortunato
  • 1,360
  • 10
  • 9
-1

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.

Aravind A
  • 9,507
  • 4
  • 36
  • 45
  • 1
    i test solution but get this error : java.lang.IllegalStateException: No WebApplicationContext found: not in a DispatcherServlet request? – mehdi shahdoost Jan 09 '12 at 13:04
  • @Mehdi - For this the requests should be routed through DispatcherServlet . You may as well use Spring MVC . Else take a look at ContextSingletonBeanFactoryLocator . Discussed at http://stackoverflow.com/questions/129207/getting-spring-application-context and a sample at http://www.captaindebug.com/2011/04/using-spring-contextsingletonbeanfactor.html . Try it as a scriptlet or something . I haven't used it - Just a pointer . – Aravind A Jan 09 '12 at 13:17
  • @mehdishahdoost If you're accessing a JSP page directly (*another* thing not to do) this will not work. You seriously need to reconsider your approach. This question is tagged with Spring MVC--are you using it or not? If not, remove the tag. If so, use the framework. – Dave Newton Jan 09 '12 at 13:24
  • Never said it wasn't, in fact I didn't say anything about your answer at all. – Dave Newton Jan 09 '12 at 19:28
  • @DaveNewton My apologies . Slight misunderstanding on my part :) . – Aravind A Jan 09 '12 at 19:40
  • @AravindA Thanks! I had a similar question and your solution worked for me. –  Jul 04 '19 at 19:03