0

I have some experience with JSF but I want to learn some Spring MVC now. I wish to display the options to the user to change the language my website is displayed in. To accomplish this I want to define the languages in XML and set them in a bean, then in a JSP iterate over that list to show the languages options to the user.

This is what my XML looks like:

<bean id="languagesSupportedBean" class="be.maxcorp.Util.LanguageBean">
    <property name="languagesSupported">
        <array>
            <value>en</value>
            <value>nl</value>
        </array>
    </property>
</bean>

This is my LanguagesSupportedBean class:

@Component
public class LanguageBean {
    public String[] languagesSupported;

    public String[] getLanguagesSupported() {
        return languagesSupported;
    }

    public void setLanguagesSupported(String[] languagesSupported) {
        this.languagesSupported = languagesSupported;
    }
}

In my JSP I'd like to do something like this:

<c:forEach items="${languageBean.LanguagesSupported}" var="language">
    ${language}
</c:forEach>

Because Spring MVC is request-based and not component-based I suppose this approach won't work unless I add the LanguageBean as attribute to every Model param in every controller method?

I'd greatly appreciate any tips on accomplishing this.

Mekswoll
  • 1,355
  • 3
  • 15
  • 35

2 Answers2

3

If you're using an InternalResourceViewResolver you should be able to set a property called exposeContextBeansAsAttributes that will expose your beans as attributes that JSPs can access directly:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="exposeContextBeansAsAttributes" value="true"/>
</bean>

So if your LanguageBean is specified as id="languagesSupportedBean" in your XML, you can reference it directly in your JSP using its id:

<c:forEach items="${languagesSupportedBean.languagesSupported}" var="language">
    ${language}
</c:forEach>

So no controller or model modifications needed.

If you are declaring your LanguageBean in XML, then you won't need to annotate it @Component

Alternatively, if you're not using InternalResourceViewResolver you could inject your LanguageBean into your controller and then expose it using a method annotated @ModelAttribute:

@ModelAttribute("languagesSupportedBean")
public LanguageBean getLanguageBean() {
    return languageBean;
}

That would then be accessible in your JSP using the name languagesSupportedBean and would alleviate the need to set the bean on every model in every controller method.

Will Keeling
  • 22,055
  • 4
  • 51
  • 61
  • I'm using `TilesViewResolver` so I don't think I can set that property. – Mekswoll May 21 '13 at 03:25
  • I'm going to accept this as answer because I didn't specify in the question that I was using Tiles3 and this answer would work if I used the `InternalViewResolver`. The answer with Tiles3 can be found here http://stackoverflow.com/questions/2848415/accessing-spring-beans-from-a-tiles-view-jsp – Mekswoll May 21 '13 at 07:59
  • @Mekswoll - glad you found the answer. I updated the question with another possible alternative too. – Will Keeling May 21 '13 at 08:08
0

Store the bean in the session. You can access it the same way from the JSP. Otherwise you can extend Model and @Autowire your been there(not sure that it will work).

Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145