Spring Boot 系列教程16-数据国际化
internationalization(i18n)
- 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式。
- 它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素。
- 换言之,应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产。
- 开发这样的程序的过程,就称为国际化。
数据国际化
- * 关键的思路是从请求作用域获取locale,然后查询对应的数据*
中文语言数据页面:只有中文数据
英文语言数据页面:只有英文数据
浏览器切换中文,英文
User
@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
private String locale;//通过此字段查询对应的数据
InitApplicationListener
package com.jege.spring.boot.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import com.jege.spring.boot.data.jpa.entity.User;
import com.jege.spring.boot.data.jpa.repository.UserRepository;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:spring的事件监听器的处理机制:在启动服务器的时候,插入默认数据
*/
@Component
public class InitApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext context = event.getApplicationContext();
UserRepository userRepository = context.getBean("userRepository", UserRepository.class);
User user;
for (int i = 1; i < 21; i++) {
if (i % 2 == 0) {
user = new User("je哥" + i, 25 + i);
user.setLocale("zh");
} else {
user = new User("je-ge" + i, 25 + i);
user.setLocale("en");
}
userRepository.save(user);
}
}
}
UserController
// 从user.jsp列表页面由easyui-datagrid发出ajax请求获取json数据
@RequestMapping("/json")
@ResponseBody
public Map<String, Object> json(@RequestParam(name = "page", defaultValue = "1") int page,
@RequestParam(name = "rows", defaultValue = "10") int rows, final String q, HttpServletRequest request) {
// 按照id降序
Sort sort = new Sort(Sort.Direction.DESC, "id");
// 封装分页查询条件
Pageable pageable = new PageRequest(page - 1, rows, sort);
// 拼接查询条件
Specification<User> specification = new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<Predicate>();
if (!StringUtils.isEmpty(q)) {
list.add(cb.like(root.get("name").as(String.class), "%" + q + "%"));
}
if (request.getLocale().toString().contains("en")) {
list.add(cb.like(root.get("locale").as(String.class), "%en%"));
} else {
list.add(cb.like(root.get("locale").as(String.class), "%zh%"));
}
Predicate[] p = new Predicate[list.size()];
return cb.and(list.toArray(p));
}
};
return findEasyUIData(userRepository.findAll(specification, pageable));
}
其他关联项目
- Spring Boot 系列教程15-页面国际化
http://blog.csdn.net/je_ge/article/details/53434761 - Spring Boot 系列教程7-EasyUI-datagrid
http://blog.csdn.net/je_ge/article/details/53365189
源码地址
https://github.com/je-ge/spring-boot
如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!
Spring Boot 系列教程16-数据国际化的更多相关文章
- Spring Boot 系列教程15-页面国际化
internationalization(i18n) 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式. 它要求从产品中抽离所有地域语言,国家/地区和 ...
- Spring Boot 系列教程17-Cache-缓存
缓存 缓存就是数据交换的缓冲区(称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,如果找到了则直接执行,找不到的话则从内存中找.由于缓存的运行速度比内存快得多,故缓存的作用就是帮 ...
- Spring Boot 系列教程14-动态修改定时任务cron参数
动态修改定时任务cron参数 不需要重启应用就可以动态的改变Cron表达式的值 不能使用@Scheduled(cron = "${jobs.cron}")实现 DynamicSch ...
- Spring Boot 系列教程11-html页面解析-jsoup
需求 需要对一个页面进行数据抓取,并导出doc文档 html解析器 jsoup 可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操 ...
- Spring Boot 系列教程10-freemarker导出word下载
freemarker FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户的,而是一个 ...
- Spring Boot 系列教程9-swagger-前后端分离后的标准
前后端分离的必要 现在的趋势发展,需要把前后端开发和部署做到真正的分离 做前端的谁也不想用Maven或者Gradle作为构建工具 做后端的谁也不想要用Grunt或者Gulp作为构建工具 前后端需要通过 ...
- Spring Boot 系列教程8-EasyUI-edatagrid扩展
edatagrid扩展组件 edatagrid组件是datagrid的扩展组件,增加了统一处理CRUD的功能,可以用在数据比较简单的页面. 使用的时候需要额外引入jquery.edatagrid.js ...
- Spring Boot 系列教程7-EasyUI-datagrid
jQueryEasyUI jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要 ...
- Spring Boot 系列教程19-后台验证-Hibernate Validation
后台验证 开发项目过程中,后台在很多地方需要进行校验操作,比如:前台表单提交,调用系统接口,数据传输等.而现在多数项目都采用MVC分层式设计,每层都需要进行相应地校验. 针对这个问题, JCP 出台一 ...
随机推荐
- nginx读取图片没有权限
场景是这样的,我们项目中上传图片到linux服务器后,保存图片存储路径和网络访问路径.app中用数据库返回的 图片访问路径来访问图片(nginx通过nfs方式读取图片).但是访问不到.要手动 chmo ...
- 读书笔记之ado.net entity framework
提供了对数据访问的一种抽象层,是更加易于以编程的方式来操作及管理数据 有以下几种模式:Model First, Database First, and Code First 现在主要讨论code Fi ...
- React 相关资料
learncodeacademy/react-js-tutorials MobX
- jQuery实例1
1.选择器: <body> <script src="jquery-2.2.4.js"></script> <div id="n ...
- 转 gl_VertexID的含义
#version //layout(location = 0) in vec4 VERTEX; uniform mat4 MODEL_MATRIX; uniform mat4 VIEW_MATRIX; ...
- 理解php的opcode
Opcode是一种PHP脚本编译后的中间语言,就像Java的ByteCode,或者.NET的MSL,举个例子,比如你写下了如下的PHP代码: <?php echo "Hello Wor ...
- 为什么 dll 改名字之后无法使用
有人直接把dll名字改了,我的程序运行出错,说这是我程序的问题,难道真是这样吗? 总感觉直接改dll名字不对,但哪儿不对呢,带着这样的疑惑研究了一下,重新做了一下试验,结果程序抛出了错误: Could ...
- 关于socket客户端接收不定长数据的解决方案
#!/usr/bin/env python3.5 # -*-coding:utf8-*- """ 本实例客户端用于不断接收不定长数据,存储到变量res "&qu ...
- keepalived问题
Sep 30 11:41:34 XSXSH-LB2 Keepalived[2735]: Starting Keepalived v1.2.12 (04/08,2014) Sep 30 11:41:34 ...
- Linux RCU机制详解
关于rcu的几点声明: 1:RCU使用在读者多而写者少的情况.RCU和读写锁相似.但RCU的读者占锁没有任何的系统开销.写者与写写者之间必须要保持同步,且写者必须要等它之前的读者全部都退出之后才能释放 ...