Results 1 to 6 of 6

Thread: Multi-language pages

  1. #1
    Join Date
    Jan 2011
    Posts
    18

    Default Multi-language pages

    Hi,

    I have a site which supports two languages German and Japanese. The controller for the home page is like this:
    Code:
    @Controller
    @RequestMapping({"/home-de.html", "/home-jp.html"})
    public class HomeController {
    	@RequestMapping(method=RequestMethod.GET)
    	public String home(Model model) {
    		...
    	}	
    }
    The locale is controlled by request urls by checking whether they end with "-de.html" or "-jp.html". Everything works fine.

    If the site is expanded to support more languages, the value of the RequestMapping would increase (i.e. the length of the array). My question is how to dispatch all incoming urls home-de.html, home-jp.html, home-nl.html, home-... to the same controller HomeController, where HomeController would be
    Code:
    @Controller
    @RequestMapping("/home.html")
    public class HomeController {
    	@RequestMapping(method=RequestMethod.GET)
    	public String home(Model model) {
    		...
    	}	
    }
    I tried to use a custom HandlerMapping but failed.

    Thank you!
    Last edited by ee722; Jun 8th, 2012 at 01:11 PM.

  2. #2
    Join Date
    Jan 2011
    Posts
    18

    Default

    Take an incoming request url .../home-jp.html for example, I want to change it to .../home.html before Spring DispatcherServlet servlet redirects it. In other words, when .../home-jp.html comes in, I'd like DispatcherServlet servlet redirects .../home.html instead of .../home-jp.html. Does anyone have an idea where I can perform that modification?

    Thank you!
    Last edited by ee722; Jun 9th, 2012 at 01:28 PM.

  3. #3
    Join Date
    Jan 2011
    Posts
    18

    Default

    After doing more research on the topic, I think I need to create a custom HandlerMapping class for it. Does anyone know which method I should override?

    By the way, I think the topic is very common in multiple-language web sites. How do you handle the situation without using locale/language parameter in the urls?

    Thank you!
    Last edited by ee722; Jun 12th, 2012 at 08:59 AM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Why not simply use URL Rewriting?! There are good filters out there (or if you use apache httpd is supported in there also) so that you can simply change the internal url (and transform home-jp.html into home.html?locale=jp for instance). You could always create your own filter which wraps the request and does the munging for you but I propably would use URL Rewriting.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jan 2011
    Posts
    18

    Default

    Quote Originally Posted by Marten Deinum View Post
    Why not simply use URL Rewriting?! There are good filters out there (or if you use apache httpd is supported in there also) so that you can simply change the internal url (and transform home-jp.html into home.html?locale=jp for instance). You could always create your own filter which wraps the request and does the munging for you but I propably would use URL Rewriting.
    Thank you for the suggestion.

    When I write my own filter, I don't know which object I should use to re-set incoming URLs? I can retrieve them via HttpServletRequest but the ServletRequest doesn't have a seter for the request URLs.

    Thank you!

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You don't reset incoming urls you wrap the request... But why reinvent the wheel? There is already a pretty good URL Rewriting filter out there, why not use that?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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