注意:另请参阅related question。
我有一个简单的Spring Bean myUrl
,它使用来自首选项位置(Windows注册表)的URL(例如http://yourdomain.is.here/something
)初始化:
<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文件中使用它(因此,我不想尝试将其包含在特定控制器的模型中):
<%@ 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}
表达式中。我已完成this question并修改了我的ViewResolver
,看起来像这样:
<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>
但是,这个价值还没有显现出来。我怀疑我忘记了一些非常基本的东西,但我不知道是什么。任何人都可以帮助我吗?
答案 0 :(得分:3)
您可以创建一个控制器init方法,该方法获取myUrl bean并将其存储在http会话中。
session.setAttribute("myUrl", myUrl);
然后在JSP代码中,您需要引用bean。
<jsp:useBean id="myUrl" class="java.lang.String" scope="session"/>
例如获取值:
<myTags:cssPath hostURL="<%= myUrl %>" ...