Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - bean access in JSP

I have configured a bean like this and I have forum.host.url in the file properly

<bean id="forum_host_url" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="forum.host.url"/>
        <property name="resourceRef" value="true"/>
</bean>

I need to access this bean value from a JSP, I have tried

${forum_host_url}

in my jsp file but its not getting any value. what is the correct way?

like image 213
Buddhi Avatar asked Dec 09 '10 10:12

Buddhi


People also ask

Can we use jsp with spring?

properties, Spring MVC will look for view-books. jsp inside the /WEB-INF/jsp/ directory. The above example shows us how to use the JSTL <c:url> tag to link to external resources such as JavaScript and CSS. We normally place these under the ${project.

What is jsp useBean?

The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the Bean class is already created, it doesn't create the bean depending on the scope. But if object of bean is not created, it instantiates the bean.


1 Answers

If you are using InternalResourceViewResolver you can do something like this:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
    <property name="exposedContextBeanNames">
        <list><value>forum_host_url</value></list>
    </property>
</bean>

If you prefer, you can use exposeContextBeansAsAttributes property and JSPs will be able to access all your beans.

like image 69
sinuhepop Avatar answered Sep 23 '22 02:09

sinuhepop