提交 611e176a 编写于 作者: 武汉红喜's avatar 武汉红喜

whatsmars-boot-sample-thymeleaf

上级 24602e21
......@@ -28,6 +28,7 @@
<module>whatsmars-boot-sample-web</module>
<module>whatsmars-boot-sample-async</module>
<module>whatsmars-boot-sample-datasource</module>
<module>whatsmars-boot-sample-thymeleaf</module>
</modules>
<dependencies>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>whatsmars-spring-boot-samples</artifactId>
<groupId>org.hongxi</groupId>
<version>Rocket.S8</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>whatsmars-boot-sample-thymeleaf</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.hongxi.whatsmars.boot.sample.thymeleaf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by shenhongxi on 2020/8/16.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package org.hongxi.whatsmars.boot.sample.thymeleaf.common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.Locale;
/**
* Created by shenhongxi on 2017/6/28.
*/
@Component
public class LocaleService {
public static final Locale DEFAULT_LOCALE = Locale.SIMPLIFIED_CHINESE;
@Autowired
private MessageSource messageSource;
public String getMessage(String key) {
Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(key, null, locale);
}
public static boolean isUSLocale() {
return LocaleContextHolder.getLocale().equals(Locale.US);
}
public static Locale resetLocale(Locale locale) {
Locale origin = LocaleContextHolder.getLocale();
LocaleContextHolder.setLocale(locale);
return origin;
}
}
\ No newline at end of file
package org.hongxi.whatsmars.boot.sample.thymeleaf.controller;
import org.hongxi.whatsmars.boot.sample.thymeleaf.common.LocaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
import java.util.Map;
/**
* Created by shenhongxi on 2020/8/16.
*/
@Controller
public class HomeController {
@Autowired
private LocaleService localeService;
/**
* 设置区域解析器 (default is AcceptHeaderLocaleResolver)
*/
@Bean
public LocaleResolver localeResolver() {
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setCookieName("language");
localeResolver.setCookieMaxAge(Integer.MAX_VALUE);
localeResolver.setDefaultLocale(LocaleService.DEFAULT_LOCALE); // Locale.getDefault()
return localeResolver;
}
@PostMapping(value = "/changeLang")
@ResponseBody
public String changeLang(HttpServletRequest request, HttpServletResponse response, @RequestParam String lang){
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if ("zh".equals(lang)) {
localeResolver.setLocale(request, response, Locale.SIMPLIFIED_CHINESE);
} else if("en".equals(lang)){
localeResolver.setLocale(request, response, Locale.US);
}
return "lang:" + LocaleContextHolder.getLocale().getLanguage();
}
@GetMapping("/")
public String home(Map<String,Object> map) {
System.out.println(LocaleService.isUSLocale());
map.put("hello", "Hi, boy!");
map.put("country", localeService.getMessage("country"));
return "index";
}
}
spring:
profiles:
active: dev
thymeleaf:
cache: false
messages:
basename: i18n/messages
\ No newline at end of file
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>thymeleaf</title>
</head>
<body>
<h1 th:inline="text">thymeleaf</h1>
<p th:text="${hello}"></p>
<p th:text="${country}"></p>
<p th:text="#{country}"></p>
</body>
</html>
\ No newline at end of file
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册