tags:

views:

16

answers:

1

I'm new to Spring MVC and have the following situation:

In WEB-INF/demoshop-servlet.xml I have the following line:

<bean id="someBean" class="com.xxx.xxx.web.SomeBean" />

I also have a JSP that contains a line like:

${someBean.someAttribute}

I expected that the attribute is read from the bean when the page is rendered but it's not. The expression is just ignored. (While other EL expressions with objects introduced in a ModelMap in the controller are evaluated correctly.)

In order to debug this a little I inserted the following to the JSP:

<% System.out.println(application.getAttribute("someBean")); %>

Which prints null.

What is missing, or what am I doing wrong here?

+1  A: 

Spring beans are not automatically made available to the JSP page, you have to put the bean in the appropriate scope (application, session, request, or page). You ave at least these options:

  1. Add the bean to the Model in your Controller
  2. Extend a View class and add the bean to the Model.
dwb
I tried the second idea, and it worked. Thanks! I added a bean of class InternalResourceViewResolver in the same xml file and added a property 'viewClass' that points to a new sub-class of JstlView which just calls setExposeContextBeansAsAttributes(true) in the constructor.
Wolfgang