Announcement Announcement Module
Collapse
No announcement yet.
Transition to set locale Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • breaux
    started a topic Transition to set locale

    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.

  • breaux
    replied
    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, 09:22 AM. Reason: formatting

    Leave a comment:


  • boudewijn
    replied
    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

    Leave a comment:


  • breaux
    replied
    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.

    Leave a comment:


  • boudewijn
    replied
    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.

    Leave a comment:

Working...
X