In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the springboot page internationalization how to configure the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will gain after reading this springboot page internationalization how to configure the article, let's take a look at it.
The method is as follows
1. Introduce dependency pom.xml
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf
two。 Import web resources, here to recommend a page resource that I am using, SB ADMIN-2
The html page is placed in the templates directory, which is the default parsing directory for thymeleaf, and other style files are placed in the static directory.
3. Take over spring Mvc and customize the url access path, with or without doing it
Create a config directory and a myWebMvcConfig here
Import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.LocaleResolver;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class myWebMvcConfig implements WebMvcConfigurer {@ Override public void addViewControllers (ViewControllerRegistry registry) {registry.addViewController ("/ wq") .setViewName ("register"); / / localhost:8080/wq registry.addViewController ("/") .setViewName ("register") / / localhpst:8080/ registry.addViewController ("/ register.html") .setViewName ("register"); / / localhost:8080/register.html}}
Multiple paths can be set so that all three url,spring will access register.html.
There's another way to do it.
Import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class demoController {@ RequestMapping ({"/", "/ wq"}) public String test () {return "register";}}
4. Internationalization profile: en_US English, zh_CN Chinese
Click the plus sign in the upper left corner to add the configured attributes, as long as you fill in the corresponding Chinese and English on the right
5. The configuration file has been written, how can it be used in our page? The role of thyme leaf is here again.
First add a header like this to your web page
Adding * * th:** to all html attributes is taken over by thymeleaf. According to thymeleaf syntax, the international value is obtained using * * {} * *, the local value is * * ${} * *, and url is used * * @ {} * *
Chinese English
6. The page and configuration file are ready, how to achieve the jump?
In WebMvcAutoConfiguration.class
@ Bean @ ConditionalOnMissingBean (name = {"localeResolver"}) public LocaleResolver localeResolver () {if (this.webProperties.getLocaleResolver () = = org.springframework.boot.autoconfigure.web.WebProperties.LocaleResolver.FIXED) {return new FixedLocaleResolver (this.webProperties.getLocale ()) } else {AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver (); localeResolver.setDefaultLocale (this.webProperties.getLocale ()); return localeResolver;}}
We found AcceptHeaderLocaleResolver.class again and found that it implemented LocaleResolver.
Public class AcceptHeaderLocaleResolver implements LocaleResolver {private final List supportedLocales = new ArrayList (4); @ Nullable private Locale defaultLocale
Then we'll write our own LocaleResolver.
Public class myLocaleResolver implements LocaleResolver {@ Override public Locale resolveLocale (HttpServletRequest request) {String mylocale=request.getParameter ("l"); Locale locale=Locale.getDefault (); if (! StringUtils.isEmpty (mylocale)) {String [] split=mylocale.split ("_"); locale=new Locale (split [0], split [1]) } System.out.println ("debug==== >" + mylocale); return locale;} @ Override public void setLocale (HttpServletRequest request, HttpServletResponse response, Locale locale) {}}
Then inject myLocaleResolver into the spring configuration
@ Bean public LocaleResolver localeResolver () {return new myLocaleResolver ();}
* * Note: the method name must be localeResolver**,** because the bean** named localeResolver in the source code
7. Finally, let's test it.
And the console output is fine.
This is the end of the article on "how to configure the internationalization of springboot pages". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to configure the internationalization of springboot pages". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.