Learn from. NET to Java eighth--springboot realize session sharing and internationalization

Source: Internet
Author: User
Tags i18n locale uuid

Springboot Session Sharing

Modify Pom.xml Add Dependency

        <!--Spring Session -        <Dependency>            <groupId>Org.springframework.session</groupId>            <Artifactid>Spring-session-data-redis</Artifactid>        </Dependency>

Add configuration class Redissessionconfig

= $)// default is 1800 seconds expired, here test modified to 60 second publicclass  redissessionconfig {} 

Add a controller class Sessioncontroller to test

@RestController  Public class Sessioncontroller {    @RequestMapping ("/uid")    String UID (HttpSession session) {        = (UUID ) Session.getattribute ("UID");         if NULL ) {            = uuid.randomuuid ();        }        Session.setattribute ("UID", UID);         return Session.getid ();    }}

Visit Http://localhost:8083/boot/uid First

Then modify the configuration file Application.yml

Spring:  Profiles:    test

Re-run the Idea,test profile configuration port is 8085, so the browser input http://localhost:8085/boot/uid

We see that the two UID is the same.

Here I use spring boot Redis for session sharing, and you can load balance with Nginx and share session.

About Nginx can refer to my other article: Nginx Detailed-server cluster

Spring Boot Internationalization

Internationalization is a simple thing to do in spring boot.

(1) Under the Resources directory, we create a few new resource files, messages.properties equivalent to the default configuration, when the other configuration can not find the record, the final will be in this configuration file to find.

Messages.propertiesmessages_en_us. propertiesmessages_zh_cn. Properties

Add the following configuration values in each of the three configuration files in turn:

Msg= , I'm Chinese .
msg=I ' m Chinese
Msg= , I'm Chinese .

Once added, these files are automatically wrapped in a

It should be noted that this naming is fastidious,messages.properties parts are fixed, in different languages, we can distinguish between them by _. Why is a fixed name, because the source code is hard-coded so named.

(2) Create a new configuration file localeconfig

@Configuration @enableautoconfiguration@componentscan Public classLocaleconfigextendsWebmvcconfigureradapter {@Bean Publiclocaleresolver Localeresolver () {sessionlocaleresolver SLR=NewSessionlocaleresolver (); //Default LanguageSlr.setdefaultlocale (Locale.china); returnSLR; } @Bean Publiclocalechangeinterceptor Localechangeinterceptor () {Localechangeinterceptor LCI=NewLocalechangeinterceptor (); //Name of parameterLci.setparamname ("Lang"); returnLCI; } @Override Public voidaddinterceptors (Interceptorregistry registry) {Registry.addinterceptor (Localechangeinterceptor ()); }}

(3) In the controller, we add the method of testing

    //i18n@RequestMapping ("/")     PublicString i18n () {return"I18n"; } @RequestMapping ("/changesessionlanauage")     Publicstring Changesessionlanauage (httpservletrequest request, httpservletresponse Response, String lang) {System.ou        T.println (lang); Localeresolver Localeresolver=Requestcontextutils.getlocaleresolver (Request); if("zh". Equals (lang)) {Localeresolver.setlocale (Request, Response,NewLocale ("zh", "CN")); }Else if("en". Equals (lang)) {Localeresolver.setlocale (Request, Response,NewLocale ("en", "US")); }        return"redirect:/"; }

(4) Add view to show, create new file under Templates i18n.html, through # can get the value of configuration item in internationalization configuration file directly.

<!DOCTYPE HTML><HTMLxmlns:th= "http://www.thymeleaf.org"><Head>    <MetaCharSet= "UTF-8"/>    <title>$Title $</title></Head><Body><ahref= "/changesessionlanauage?lang=en">中文版 (US)</a><ahref= "/changesessionlanauage?lang=zh">Chinese Simplified</a><BR/><H3Th:text= "#{msg}"></H3><h4Th:text= "${message}"></h4></Body></HTML>

(5) Run view effect

Learn from. NET to Java eighth--springboot realize session sharing and internationalization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.