Spring Boot + Bootstrap + jQuery + Freemarker

原文地址:http://qilu.me/post/tech/2018-03-18

最近在写一些Web的东西,技术上采用了Spring Boot + Bootstrap + jQuery + Freemarker。过程中查了大量的资料,也感受到了前端技术的分裂,每种东西都有N种实现,组合起来,每种解决方案的资料却很有限。

这篇文章记录下多语言国际化的实现,以支持中英文为例。

首先是页面内容的国际化

1.定义页面文本配置文件的路径,在application.properties里添加spring.messages.basename=i18n/messages

2.在resources/目录下创建上述目录,添加3个配置文件messages.properties、messages_zh.properties、messages_en.properties,分别对应默认,中文和英文配置,完整路径为resources/i18n/messages.properties

3.在配置文件里定义每条需要国际化的文本,比如中文 index.title=麦希工具 - 您身边的助手,英文Meta Tool - Your Best Assistant

4.在Freemarker文件里使用<@spring.message ""/>来输出文本,比如<@spring.message "index.title"/>

再说下验证内容的国际化

所谓验证内容,就是比如在填Form表单的时候,有些字段有格式或数值等的要求,表单提交时,应该验证数据是否符合要求。验证又分前端验证和后端验证,一般结合使用。

前端用来验证格式(必须是数字/英文/邮件)等,如:

这里pattern使用正则表达式,定义了输入框输入字符的范围和数量。

后端用来验证数值(必须大于18岁)等,可以在Spring Boot的VO对象里,以添加注解的形式实现,如:

@Min(value = 100, message = "不能低于100cm")
@Max(value = 250, message = "不能高于250cm")
private String height;

这里就出现了问题,即注解也应该采用配置文件的形式,以支持多语言切换,这就是验证内容的国际化。

1.定义验证文本配置文件的路径,在application.properties里添加spring.messages.basename=i18n/messages,i18n/ValidationMessages,前面的是第一节的页面文本,后面的是验证文本

2.在resources/目录下创建上述目录,添加3个配置文件ValidationMessages.properties、ValidationMessages_zh.properties、ValidationMessages_en.properties,分别对应默认,中文和英文配置,完整路径为resources/i18n/ValidationMessages.properties

3.在配置文件里定义每条需要国际化的文本,比如中文 vm.bmi.height.lower=不能低于100cm,英文vm.bmi.height.lower=Can Not Lower Than 100cm

4.与页面文本相比,这里要多做一步,即在代码里覆盖默认验证器的配置,在代码根目录添加以下文件:

@Configuration

public class CustomConfiguration implements WebMvcConfigurer {

@Autowired
private MessageSource messageSource; @Override
public Validator getValidator() {
return localValidatorFactoryBean();
} @Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource(messageSource);
return validator;
}

}

5.在接收表达的VO对象的属性上,使用验证文本作为提示

public class BmiVo {

@Min(value = 100, message = "{vm.bmi.height.lower}")
@Max(value = 250, message = "{vm.bmi.height.upper}")
private String height;

6.在表单上添加验证失败后的提示,使用<@spring.bind "" />绑定VO对象的属性,使用<@spring.showErrors ""/>显示属性验证失败的提示

cm

这样就做到了页面内容和验证内容的多语言国际化支持,具体示例参看Meta Tool

Spring Boot + Bootstrap + jQuery + Freemarker的更多相关文章

  1. spring boot ----> 常用模板freemarker和thymeleaf

    ===========================freemarker=================================== freemarker 官网:https://freem ...

  2. 玩转spring boot——结合jQuery和AngularJs

    在上篇的基础上 准备工作: 修改pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  3. Spring Boot+BootStrap fileInput 多图片上传

    一.依赖文件 <link rel="stylesheet" type="text/css" th:href="@{/js/bootstrap/c ...

  4. Spring Boot使用模板freemarker【从零开始学Spring Boot(转)

    视频&交流平台: à SpringBoot网易云课堂视频 http://study.163.com/course/introduction.htm?courseId=1004329008 à  ...

  5. spring boot中使用freemarker

    在做Java web开发领域,web模板引擎主要有jsp.freemarker.velocity,其中freemarker是我们常用的一种,在spring boot中使用的freemarker的方法可 ...

  6. Spring boot之使用freemarker

    大纲 (1)在pom.xml中引入freemarker; (2)如何关闭freemarker缓存 (3)编写模板文件.ftl (4)编写访问文件的controller 在pom.xml中引入freem ...

  7. Spring Boot + Bootstrap 出现"Failed to decode downloaded font"和"OTS parsing error: Failed to convert WOFF 2.0 font to SFNT"

    准确来讲,应该是maven项目使用Bootstrap时,出现 "Failed to decode downloaded font"和"OTS parsing error: ...

  8. Spring Boot +Bootstrap 图片上传与下载,以及在bootstrap-table中的显示

    1.前台上传: <input type="file" name="file" id="file"> 2.后台的接收与处理: St ...

  9. Spring Boot + Freemarker多语言国际化的实现

    最近在写一些Web的东西,技术上采用了Spring Boot + Bootstrap + jQuery + Freemarker.过程中查了大量的资料,也感受到了前端技术的分裂,每种东西都有N种实现, ...

随机推荐

  1. 洛谷P2391 白雪皑皑(并查集)

    题目背景 “柴门闻犬吠,风雪夜归人”,冬天,不期而至.千里冰封,万里雪飘.空中刮起了鸭毛大雪.雪花纷纷,降落人间. 美能量星球(pty 在 spore 上的一个殖民地)上的人们被这美景所震撼.但是 p ...

  2. css 中font属性知识点总结

    一. font属性值可以继承.例如子元素可以继承父元素的行高,字体大小等等. 二.font属性可以进行连写:font: font-sytle  font-weight  font-size/line- ...

  3. equal height

    https://css-tricks.com/the-perfect-fluid-width-layout/ http://nicolasgallagher.com/multiple-backgrou ...

  4. Walking on the path of Redis --- Redis configuration

    废话开篇 Redis的安装是非常简单易操作的,但是配置就有点复杂了,要想得到高性能的Redis数据服务,深入了解下如何配置是很重要的. 配置详解 下面是主要的参数及说明,至于如何配置才能最优,目前还不 ...

  5. in 与 exist , not in 与 not exist

    in和exists in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. 如果查询的两个 ...

  6. vj线段树专题

    vj线段树专题题解 单点更新模板 void build(int x,int l,int r){//sum[x]控制l-r区域 if(l==r){Sum[x]=num[l];return ;} int ...

  7. 【JavaScript框架封装】JavaScript中的文本字符串的转义和反转义的实现

    如果是想把本地的一个字符串串存到服务器,再次取出来的还是文本的话,就需要这个文本字符串的转义: /** * 对一个字符串的转义 * @param str * @return {*} */ functi ...

  8. httpClient 几种超时问题

    HttpClient的有3种超时时间,分别是: 1. [java] view plaincopyprint? ConnManagerParams.setTimeout(params, 1000); C ...

  9. Charles抓包工具抓取HTTS请求

    1. 移动端(手机)配置证书 1.1 进入Charles,点击Help,选择SSL Proxying --> 选择安装手机证书 1.2 在Charles弹框提示中,按照提示内容,进入手机进入下图 ...

  10. C#基础--索引器

    classProgram { staticvoidMain(string[] args) { man mm =new man(); mm[0]="jingya"; mm[1]=&q ...