Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7065945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:01:13+00:00 2026-05-28T05:01:13+00:00

Story I have a select control that represents user access level. I’m looking for

  • 0

Story

I have a select control that represents user access level. I’m looking for a way to internationalize it. The label should be loaded from a message resource and the value should be used as is. I prepare all my drop down lists in controllers using a simple SelectOption class that has a label and a value properties. This way, my select’s look consistent accross all jsp’s.

Problem

I’ve found some examples but they are based on logic within jsp. Developer loops through his labels and manually constructs the option tag using a message resource. While this works, there just has to be a better way. I’ve also found some comments that Spring 3 will have support for internationalizing option labels but I can’t find anything concrete on that.

Controller logic

Collection<SelectOption> optionList = new ArrayList<SelectOption>();
optionList.add(new SelectOption("-SELECT-", "-"));  
optionList.add(new SelectOption("Administrator", "ADMIN"));
optionList.add(new SelectOption("Editor", "EDIT"));
bean.setFilterUserAccessLevelOptionList(optionList);

JSP logic

<form:select path="filterUserAccessLevel"  items="${bean.filterUserAccessLevelOptionList}" itemLabel="label" itemValue="value"/>

Questions

  1. I would like to add options in my controller in this way: optionList.add(new SelectOption(“userAccessLevelAdministratorLabel”, “ADMIN”)); and have Spring convert userAccessLevelAdministratorLabel to a value from a message resource. Is this possible?
  2. If Spring 3 cannot do this for me, how else can this be achieved without manually constructing the option tag within jsp?

=== 2012-01-15 ==============================================================

Still trying to work out a solution using aweigold’s idea.

Controller

@Controller
public class UserController {
@Autowired
private UserService userService;

@Autowired
SelectOptionListBuilder listBuilder;

    @RequestMapping("/userIndex/{pageNumber}")
public ModelAndView getUserList(@PathVariable Integer pageNumber, @ModelAttribute("userIndexBean") UserIndexBean phantomBean, Locale locale, Model model) {

    UserIndexBean bean = new UserIndexBean();

    // prepare filter form
    Collection<SelectOption> optionList = listBuilder.getUserAccessLevelOptionList(true, SortOrder.NONE, locale);
    bean.setFilterUserAccessLevelOptionList(optionList);

SelectOptionListBuilderImpl

@Component
public class SelectOptionListBuilderImpl implements SelectOptionListBuilder, MessageSourceAware {

private MessageSource messageSource;

@Override     
public void setMessageSource(MessageSource messageSource) {
    this.messageSource = messageSource;
} 


@Override
public List<SelectOption> getUserAccessLevelOptionList(boolean addSelectPrompt, SortOrder sortOrder, Locale locale) {
    List<SelectOption> optionList = new ArrayList<SelectOption>();

    if(addSelectPrompt) {
        optionList.add(new SelectOption(messageSource.getMessage("common.selectPromptLabel", null, locale), "-"));
    }

messageSource mapping

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">    
<property name="basename" value="/WEB-INF/i18n/messages" />
<property name="defaultEncoding" value="UTF-8"/>
<property name="UseCodeAsDefaultMessage" value="true"/>
</bean>

Exception

org.springframework.context.NoSuchMessageException: No message found under code 'common.selectPromptLabel' for locale 'en_CA'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:01:17+00:00Added an answer on May 28, 2026 at 5:01 am

    When I need to do operations like this in a Controller outside of a jsp, I’ve been making my Controllers MessageSourceAware. Spring will then inject a new MessageSource when they are swapped, and you can interrogate it much like Spring does. In your example, you would do something like this:

    @Controller
    public class someController implements MessageSourceAware {
        private MessageSource messageSource;
    
        @Override
        public void setMessageSource(MessageSource messageSource) {
            this.messageSource = messageSource;
        }
    
        @RequestMapping
        // Pass in the locale from the LocaleResolver
        public void someMapping(Locale locale){
            optionList.add(new SelectOption(
               messageSource.getMessage("userAccessLevelAdministratorLabel", null, locale),
               "ADMIN"))
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an asp.net calendar control that is used to select a date and
I currently have a user control that is used on both an /Address/Edit and
I have one doubt that how to get other control's value when we select
Story: One of the app that i have works on python 2.4 and other
I have string looking like this: 'Toy Story..(II) (1995)' I want to split the
I needed to create a custom select list for the user registration page that
I have a system that allows a person to select a form type that
List -> l_user I have a List node that has a user-reference field (
My story is: I have a couple of forms in separately views that post
Hi i have a store procedure, where i do a select query. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.