Results 1 to 5 of 5

Thread: Transition to set locale

  1. #1

    Question Transition to set locale

    What's the simplest way to define a transition that switches the locale and returns to the current view-state? We do have the LocaleChangeInterceptor configured if that helps.

  2. #2

    Default

    You call the flow you want to go to (or want to stay), and add &locale=<locale> to the url (if you've configured LocaleChangeInterceptor to read "locale"); the interceptor takes care of the rest.

  3. #3

    Default Understood, but...

    What's the simplest way to do that from a transition? I'm sorry if I'm being thick here, but it's been a while since I last coded SWF, so it's possible something obvious is escaping me. All I can think of right now is to transition to another view-state that does this, but I was hoping instead for something that could be done right in the transition itself.

  4. #4

    Default

    You can create a loginBean, and call it in your transition like this:
    Code:
    <transition on="login">
    			<evaluate expression="loginBean.doLogin()"></evaluate>
    		</transition>
    The bean would have the following method:
    Code:
    public String doLogin() throws IOException, ServletException {
    		ExternalContext context = FacesContext.getCurrentInstance()
    				.getExternalContext();
    
    		RequestDispatcher dispatcher = ((ServletRequest) context.getRequest())
    				.getRequestDispatcher("/web/loginProcess");
    
    		dispatcher.forward((ServletRequest) context.getRequest(),
    				(ServletResponse) context.getResponse());
    
    		FacesContext.getCurrentInstance().responseComplete();
    		// It's OK to return null here because Faces is just going to exit.
    		return null;
    	}
    (configure the spring login process as /web/loginProcess, as in the method)
    The username should be called j_username and the password j_password.
    Don't forget to add FORWARD in the Spring security filter like this:
    Code:
    	<filter-mapping>
    		<filter-name>springSecurityFilterChain</filter-name>
    		<url-pattern>/*</url-pattern>
    		<dispatcher>FORWARD</dispatcher>
    		<dispatcher>REQUEST</dispatcher>
    	</filter-mapping>
    This should do the trick to make use of the default configuration.

    cheers

  5. #5

    Default

    What I posted to StackOverflow:

    This is not what I was seeking, but it's the only thing I found to do. That is, I created an action method to call from the transition:

    <transition on="switchLanguage" validate="false">
    <evaluate expression="myAction.switchLanguage"/>
    </transition>


    And

    public Event switchLanguage(RequestContext context)
    {
    // get the "other" locale string itself from the current locale's resource bundle
    Locale locale = context.getExternalContext().getLocale();
    MessageSource ms = context.getActiveFlow().getApplicationContext();
    String newLocaleString = ms.getMessage("lang.other", null, locale);

    HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getNativeRequest();
    HttpServletResponse res = (HttpServletResponse) context.getExternalContext().getNativeResponse();

    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(req);
    localeResolver.setLocale(req, res, StringUtils.parseLocaleString(newLocaleString));
    return success();
    }
    Last edited by breaux; Mar 28th, 2012 at 09:22 AM. Reason: formatting

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •