从 JSP 文件中的会话范围 bean 访问数据

发布于 2024-11-05 21:44:07 字数 780 浏览 0 评论 0原文

我正在尝试在 Spring Web MVC 3 中开始使用会话范围的 bean。我将这一行放入我的调度程序配置中:

<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- JSP 文件中代表应用程序视图部分的作用域 bean?我尝试了这个......

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

但这并没有给我预期的结果,即

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

相反我只是得到

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

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

篱下浅笙歌 2024-11-12 21:44:07

您可以按照问题中的说明进行操作。问题可能出在您的配置上。看看你是否在视图中暴露了你的bean,如下所示:

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

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>
乖乖兔^ω^ 2024-11-12 21:44:07

您可以使用 Spring 将各个 bean 公开给 JSTL。

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

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>
熊抱啵儿 2024-11-12 21:44:07

该答案部分基于问题评论中发布的一些建议,但后来被发帖者删除。我将其添加到需要使用 bean 的每个 JSP 页面中:

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

然后我发现 这篇文章详细介绍了如何在 JSP 页面中使用 bean。

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.

雪若未夕 2024-11-12 21:44:07

您需要确保您已

<aop:scoped-proxy/>

在 xml 配置中启用。

例如:

<!-- 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>

您可以在 Spring 参考指南的“Bean 范围”一章中阅读更多相关信息。

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.

灰色世界里的红玫瑰 2024-11-12 21:44:07

下面详细阐述@sinuhepop 建议。

一个简单的方法是配置 spring,使其将 spring bean id 作为变量公开给 JSTL,这可以通过将 ExposureContextBeansAsAttributes 属性设置为 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>

如果您有一个像这样配置的 bean,

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

那么在 JSP 页面中您可以访问bean 使用名称 ${someBean.someProp} 因为这是 Spring 将自动分配给 SomeBean 的名称,除非该 bean 是使用以下名称定义的@Component("someName") 那么 JSTL 将是 ${someName.someProp}

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}

念﹏祤嫣 2024-11-12 21:44:07

对于那些想要使用 Spring 5 Java 配置的人来说,这个答案的更新。将其添加到您的 WebMvcConfigurer

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

相当于其他人提到的 XML 配置:

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

请注意,注册表的“方便”流畅 API (registry.jsp(). ...) 不提供配置 exposeContextBean... 属性的可能性。

如果可能,您应该考虑使用 exposeContextBeanNames。不过,请尽可能多地对 bean 名称使用常量,以避免在代码中重复字符串文字。因此,也许在某个类中定义一个字符串数组,它收集所有这些常量并将它们公开给您的视图解析器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文