Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 663
Pieter Avatar asked May 08 '11 09:05

Pieter


1 Answers

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>
like image 134
sinuhepop Avatar answered Dec 09 '22 07:12

sinuhepop