Pieter
Pieter

Reputation: 32755

Accessing data from session-scoped beans in JSP files

I'm trying to get started with session-scoped beans in Spring Web MVC 3. I put this line in my dispatcher configuration:

<bean id="userInfo" class="net.sandbox.sessionbeans.UserInfo" scope="session" />

Here is net.sandbox.sessionbeans.UserInfo:

package net.sandbox.sessionbeans;

public class UserInfo {
    public String username;

    public UserInfo() {
        this.username = "Unregistered User";
    }
}

How can I access session-scoped beans inside the JSP files that represent the View part of my application? I tried this...

<p align="right">${userInfo.username}</p>

... but that didn't give me the expected result, i.e.

<p align="right">Unregistered User</p>

Instead I just get

<p align="right"></p>

What am I doing wrong?

Upvotes: 5

Views: 14988

Answers (6)

UniversE
UniversE

Reputation: 2507

An update to this answer for those, who want to use Spring 5 Java configuration. Addding this to your WebMvcConfigurer

@Override
public void configureViewResolvers(ViewResolverRegistry registry){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver("/WEB-INF/view", ".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    registry.viewResolver(resolver);
}

is equivalent to this XML config mentioned by others:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
    <property name="exposeContextBeansAsAttributes" value="true" />
</bean>

Note, that the "convenient" fluent API of the registry (registry.jsp(). ...) does not offer you the possibility to configure the exposeContextBean.... properties.

If possible, you should consider using exposeContextBeanNames. Use constants as much as possible for your bean names, though, to avoid duplicating string literals in your code. So maybe define a String Array within some class, which collects all theses constants and exposes them to your view resolver.

Upvotes: 0

MattC
MattC

Reputation: 6324

You can expose individual beans to JSTL with Spring.

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="exposedContextBeanNames">
        <list>
            <value>someBean</value>
            <value>someOtherBean</value>

        </list>
    </property>
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

Upvotes: 3

ams
ams

Reputation: 62632

Elaborating on @sinuhepop suggestion below.

An easy way to do this is to configure spring so that it exposes the spring bean ids as varibales to JSTL this can be done by setting the exposeContextBeansAsAttributes property to true

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="exposeContextBeansAsAttributes" value="true"/>
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

If you have a bean configured like so

@Component
@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS,value=WebApplicationContext.SCOPE_SESSION)
public class SomeBean {
}

Then in the JSP page you can access the bean using the name ${someBean.someProp} because that is the name that Spring will auto assign to the SomeBean unless the bean is defined with a name such as @Component("someName") then the JSTL would be ${someName.someProp}

Upvotes: 1

danny.lesnik
danny.lesnik

Reputation: 18639

You need to make sure that you have

<aop:scoped-proxy/>

enabled in your xml configuration.

For Example:

<!-- an HTTP Session-scoped bean exposed as a proxy -->
  <bean id="userPreferences" class="com.foo.UserPreferences" scope="session">

        <!-- this next element effects the proxying of the surrounding bean -->
        <aop:scoped-proxy/>
  </bean>

You can read more about it in Spring reference guide, "Bean scopes" chapter.

Upvotes: 1

sinuhepop
sinuhepop

Reputation: 20297

You can do it as you show in your question. The problem is probably in your configuration. Look if you expose your beans in the view, like this:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
    <property name="exposeContextBeansAsAttributes" value="true" />
</bean>

Upvotes: 12

Pieter
Pieter

Reputation: 32755

This answer is partially based on some advice that was posted in the question's comments but was later deleted by the poster. I added this to every JSP page that needs to use the bean:

<jsp:useBean id="userInfo" scope="session" class="net.sandbox.sessionbeans.UserInfo" />

I then found this article detailing how you can use beans in a JSP page.

Upvotes: 1

Related Questions