Home > Java > Java Tutorial > body text

SpringMVC Learning Series (8) Detailed introduction to international code

黄舟
Release: 2017-03-03 11:04:46
Original
1548 people have browsed it

In series (7) we talked about the formatted display of data. Spring has already done internationalization processing when formatting the display. So how to internationalize other content of our website (such as menus, titles, etc.) What about chemical treatment? This is the content of this article -> internationalization.

SpringMVC Learning Series (8) Detailed introduction to international code. International implementation based on browser request:

First configure the springservlet-config.xml file of our project and add the following content:


                         
       
                   
               
Copy after login


Add GlobalController.java in the com.demo.web.controllers package as follows:

package com.demo.web.controllers;import java.util.Date;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.support.RequestContext;import com.demo.web.models.FormatModel;

@Controller
@RequestMapping(value = "/global")public class GlobalController {
    
    @RequestMapping(value="/test", method = {RequestMethod.GET})    public String test(HttpServletRequest request,Model model){        if(!model.containsAttribute("contentModel")){            
            //从后台代码获取国际化信息
            RequestContext requestContext = new RequestContext(request);
            model.addAttribute("money", requestContext.getMessage("money"));
            model.addAttribute("date", requestContext.getMessage("date"));

            
            FormatModel formatModel=new FormatModel();

            formatModel.setMoney(SpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international code.SpringMVC Learning Series (8) Detailed introduction to international code78);
            formatModel.setDate(new Date());
            
            model.addAttribute("contentModel", formatModel);
        }        return "globaltest";
    }
    
}
Copy after login


The model shown here also uses the functions in series (7) Demo.

Add three files: messages.properties, messages_zh_CN.properties, and messages_en_US.properties to the source folder resources in the project. Among them, "money" and "date" in messages.properties and messages_zh_CN.properties are Chinese, messages_en_US.properties are in English.

Add the globaltest.jsp view in the views folder with the following content:

nbsp;html PUBLIC "-//WSpringMVC Learning Series (8) Detailed introduction to international codeC//DTD HTML SpringMVC Learning Series (8) Detailed introduction to international code.0SpringMVC Learning Series (8) Detailed introduction to international code Transitional//EN" "http://www.wSpringMVC Learning Series (8) Detailed introduction to international code.org/TR/htmlSpringMVC Learning Series (8) Detailed introduction to international code/loose.dtd">Insert title here

    下面展示的是后台获取的国际化信息:
    ${money}
    ${date}
    下面展示的是视图中直接绑定的国际化信息:
    :
    
    :
    
    
Copy after login


Run the test:

SpringMVC Learning Series (8) Detailed introduction to international code

Change the browser language order and refresh the page:

SpringMVC Learning Series (8) Detailed introduction to international code

SpringMVC Learning Series (8) Detailed introduction to international code. Session-based internationalization implementation:

In The content added to the project's springservlet-config.xml file is as follows (the content added in the first method should be retained):

  
     
        
Copy after login


Change the globaltest.jsp view to the following content:

nbsp;html PUBLIC "-//WSpringMVC Learning Series (8) Detailed introduction to international codeC//DTD HTML SpringMVC Learning Series (8) Detailed introduction to international code.0SpringMVC Learning Series (8) Detailed introduction to international code Transitional//EN" "http://www.wSpringMVC Learning Series (8) Detailed introduction to international code.org/TR/htmlSpringMVC Learning Series (8) Detailed introduction to international code/loose.dtd">Insert title here
    中文 | 英文
    下面展示的是后台获取的国际化信息:
    ${money}
    ${date}
    下面展示的是视图中直接绑定的国际化信息:
    :
    
    :
    
    
Copy after login


Change GlobalController.java to the following content:

package com.demo.web.controllers;import java.util.Date;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import org.springframework.context.iSpringMVC Learning Series (8) Detailed introduction to international code8n.LocaleContextHolder;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.servlet.iSpringMVC Learning Series (8) Detailed introduction to international code8n.SessionLocaleResolver;import org.springframework.web.servlet.support.RequestContext;import com.demo.web.models.FormatModel;

@Controller
@RequestMapping(value = "/global")public class GlobalController {
    
    @RequestMapping(value="/test", method = {RequestMethod.GET})    public String test(HttpServletRequest request,Model model, @RequestParam(value="langType", defaultValue="zh") String langType){        if(!model.containsAttribute("contentModel")){            
            if(langType.equals("zh")){
                Locale locale = new Locale("zh", "CN"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 
            }            else if(langType.equals("en")){
                Locale locale = new Locale("en", "US"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
            }            else 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());            
            //从后台代码获取国际化信息
            RequestContext requestContext = new RequestContext(request);
            model.addAttribute("money", requestContext.getMessage("money"));
            model.addAttribute("date", requestContext.getMessage("date"));

            
            FormatModel formatModel=new FormatModel();

            formatModel.setMoney(SpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international code.SpringMVC Learning Series (8) Detailed introduction to international code78);
            formatModel.setDate(new Date());
            
            model.addAttribute("contentModel", formatModel);
        }        return "globaltest";
    }
    
}
Copy after login


Run the test:

SpringMVC Learning Series (8) Detailed introduction to international code

SpringMVC Learning Series (8) Detailed introduction to international code

SpringMVC Learning Series (8) Detailed introduction to international code. Cookie-based internationalization implementation:

Implement the second method in the project's springservlet-config Comment out the

Copy after login
Copy after login


added in the .xml file and add the following:

Copy after login

##Change GlobalController.java For the following content:

package com.demo.web.controllers;import java.util.Date;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.context.iSpringMVC Learning Series (8) Detailed introduction to international code8n.LocaleContextHolder;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.servlet.iSpringMVC Learning Series (8) Detailed introduction to international code8n.CookieLocaleResolver;//import org.springframework.web.servlet.iSpringMVC Learning Series (8) Detailed introduction to international code8n.SessionLocaleResolver;import org.springframework.web.servlet.support.RequestContext;import com.demo.web.models.FormatModel;

@Controller
@RequestMapping(value = "/global")public class GlobalController {
    
    @RequestMapping(value="/test", method = {RequestMethod.GET})    public String test(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam(value="langType", defaultValue="zh") String langType){        if(!model.containsAttribute("contentModel")){            
            /*if(langType.equals("zh")){
                Locale locale = new Locale("zh", "CN"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 
            }
            else if(langType.equals("en")){
                Locale locale = new Locale("en", "US"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
            }
            else 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());*/
            
            if(langType.equals("zh")){
                Locale locale = new Locale("zh", "CN"); 
                //request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
                (new CookieLocaleResolver()).setLocale (request, response, locale);
            }            else if(langType.equals("en")){
                Locale locale = new Locale("en", "US"); 
                //request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
                (new CookieLocaleResolver()).setLocale (request, response, locale);
            }            else 
                //request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
                (new CookieLocaleResolver()).setLocale (request, response, LocaleContextHolder.getLocale());            
            //从后台代码获取国际化信息
            RequestContext requestContext = new RequestContext(request);
            model.addAttribute("money", requestContext.getMessage("money"));
            model.addAttribute("date", requestContext.getMessage("date"));

            
            FormatModel formatModel=new FormatModel();

            formatModel.setMoney(SpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international codeSpringMVC Learning Series (8) Detailed introduction to international code.SpringMVC Learning Series (8) Detailed introduction to international code78);
            formatModel.setDate(new Date());
            
            model.addAttribute("contentModel", formatModel);
        }        return "globaltest";
    }
    
}
Copy after login

运行测试:

SpringMVC Learning Series (8) Detailed introduction to international code

SpringMVC Learning Series (8) Detailed introduction to international code

关于bean id="localeResolver" class="org.springframework.web.servlet.iSpringMVC Learning Series (8) Detailed introduction to international code8n.CookieLocaleResolver" />SpringMVC Learning Series (8) Detailed introduction to international code个属性的说明(可以都不设置而用其默认值):


    
    
    
    
    
    
Copy after login


四.基于URL请求的国际化的实现:

首先添加一个类,内容如下:

import java.util.Locale;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.DispatcherServlet;import org.springframework.web.servlet.LocaleResolver;public class MyAcceptHeaderLocaleResolver extends AcceptHeaderLocaleResolver {    private Locale myLocal;    public Locale resolveLocale(HttpServletRequest request) {        return myLocal;
    } 

    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
        myLocal = locale;
    }
  
}
Copy after login


然后把实现第二种方法时在项目的springservlet-config.xml文件中添加的

Copy after login
Copy after login


注释掉,并添加以下内容:

Copy after login


“xx.xxx.xxx”是刚才添加的MyAcceptHeaderLocaleResolver 类所在的包名。

保存之后就可以在请求的URL后附上 locale=zh_CN 或 locale=en_US 如 http://www.php.cn/ 来改变语言了,具体这里不再做演示了。


国际化部分的内容到此结束。

以上就是SpringMVC学习系列(8) 之 国际化代码详细介绍的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!