从JSP访问Spring Bean? [英] Access a Spring Bean from JSP?

查看:49
本文介绍了从JSP访问Spring Bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:另请参见相关问题.

我有一个简单的Spring Bean myUrl,它使用首选项"位置(Windows注册表)中的URL(例如http://yourdomain.is.here/something)进行了初始化:

I have a simple Spring Bean myUrl which is initialized with a URL (say http://yourdomain.is.here/something) from a Preferences location (Windows Registry):

<beans:bean id="myUrl" class="java.lang.String" >
    <beans:constructor-arg type="java.lang.String">
        <beans:value>${my.registry.location:some.url}</beans:value>
    </beans:constructor-arg>
</beans:bean>

bean工作正常,但是我想直接在多个位置包含的JSP文件中使用它(因此,我不想尝试将其包含在特定控制器的模型中):

The bean works fine, but I want to use it directly in a JSP file which is included in multiple locations (hence, I don't want to try including it in a specific controller's model):

<%@ taglib prefix="myTags" tagdir="/WEB-INF/tags" %>
<myTags:cssPath hostURL="${myUrl}"  relativePath="jquery-ui/css/smoothness/jquery-ui-1.10.3.custom.css" />
<myTags:cssPath hostURL="${myUrl}"  relativePath="style.css" />
<myTags:cssPath hostURL="${myUrl}"  relativePath="tableSorter.css" />

到目前为止,我没有尝试过使myUrl bean的值显示在${myUrl}表达式中.我经历了这个问题,并将我的ViewResolver修改为这个:

So far, nothing I have tried makes the value of the myUrl bean show up in the ${myUrl} expression. I've gone through this question and modified my ViewResolver to look like this:

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

但仍然没有显示该值.我怀疑我已经忘记了一些非常基础的东西,但是我不知道是什么.谁能帮我吗?

But still, the value doesn't show up. I suspect I've forgotten something horribly basic, but I don't know what. Can anyone help me?

推荐答案

您可以创建一个控制器init方法,该方法获取myUrl bean并将其存储在http会话中.

You can create a controller init method that gets the myUrl bean and stores it on the http session.

session.setAttribute("myUrl", myUrl);

然后在JSP代码中需要引用bean.

Then in the JSP code you need to reference the bean.

<jsp:useBean id="myUrl" class="java.lang.String" scope="session"/>

例如获取值:

<myTags:cssPath hostURL="<%= myUrl %>" ...

这篇关于从JSP访问Spring Bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆