search
HomeWeb Front-endHTML TutorialSpring MVC and easyui internationalization_html/css_WEB-ITnose

1. Create resource files

Create the folder language under the webapp, add file in it, and name it language.properties, language_en.properties, language_zh_CN.properties. Among them, language.properties is the default resource file.

Add content in it in the following format:

language.properties

welcome=Welcome

language_en.properties

welcome=Welcome

Language_zh_cn.properties

Welcome = U6b22u8fce

Among them, Welcome is key, and this is used in JSP. Properties file Entering Chinese into unicode will be automatically converted into unicode.

Among them, & lt; proprity name = "basenename" value = "/language/language"/& gt; value is the resource file, no suffix name.properties.

3. Call internationalization

3.1 Call internationalization in web

Add

<mvc:interceptors>        <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /></mvc:interceptors><!-- Saves a locale change using a session--><bean id="localeResolver"    class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /><!-- 国际化文件 --><bean id="messageSource"class="org.springframework.context.support.ReloadableResourceBundleMessageSource">    <property name="basename" value="/language/language" />    <property name="defaultEncoding" value="UTF-8"/></bean>

in the jsp file header Change the content at the jsp HTML display content, such as

to

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>

or

<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true">    Welcome</a>

changed to

<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true">    <spring:message code="welcome"/></a>

3.2 Call internationalization in javascript

<input type="text" value="welcome" />
Add

Change

<input type="text" value='<spring:message code="welcome"/>' />

to

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>

Note: 1." as this will cause an error.

2. The key value in the _en file cannot use the or ' symbol.

$.messager.alert('info');

3.3 Calling internationalization in java

Use

$.messager.alert('<spring:message code="message.info"/>');

in the controller where request is HttpServletRequest and model is Model.

4 Solving Garbled Codes

4.1 Chinese Input Garbled Codes

After completing the above steps, you will find that you enter Chinese in the XCenter interface, click OK, and write Chinese into the database , what is written is garbled code.

if(!model.containsAttribute("contentModel")){    RequestContext requestContext = new RequestContext(request);    String welcome = requestContext.getMessage("welcome");}
The solution is as follows:

In applicationContext.xml, change

to

Note: Do not add ?useUnicode=true&characterEncoding=UTF-8 after jdbc.url=jdbc:mysql://localhost:3306/demo in jdbc.properties. This is incorrect.

4.2 Some interfaces display Chinese garbled characters

<property name="url" value="${jdbc.url}"/>

Then I found that most interfaces have added this sentence

<property name="url" value="${jdbc.url}?useUnicode=true&characterEncoding=UTF-8"/>
can display Chinese normally, but part of the XCenter interface displays Chinese as???. After inspection, it was found that it is because in the controller The return value is String and marked as @ResponseBody. After checking the information, I found that the default encoding of @ResponseBody in Spring MVC is ISO-8859-1 (others are UTF-8. Does anyone know why another one is used here in the same framework? coding).

Solution:

Add produces ="application/json; charset=utf-8"

🎜> becomes

Then you will find that the Chinese characters in some trees are still displayed as garbled characters. It is found that the return value is converted into JSON format.

@RequestMapping(value = "welcome", method = RequestMethod.POST)
Solution:

Format the return value of tree to

@RequestMapping(value = "welcome", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
to

5 中英文切换

5.1 资源文件的中英文切换

         这个比较简单,使用如下代码就行

<a href="?locale=en" onclick="changeLanguage('en')">English</a><a href="?locale=zh" onclick="changeLanguage('zh_CN')">简体中文</a>

5.2 easyui框架的中英文切换

         easyui中有些框架(如datagrid)有些内容是框架自带的,需要在

中加入

<script type="text/javascript" src="js/locale/easyui-lang-zh_CN.js"></script>

那如何切换中英文呢

首先不要在

中添加上面这句,然后在主界面中加入如下js语句

<script type="text/javascript">    var language=window.navigator.language;    var userLanguage="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE']}";    if(null != userLanguage&&userLanguage!=""){//not login        language = userLanguage;    }    $(function(){        var src = 'js/locale' + '/easyui-lang-'+language.replace("-","_")+'.js';// when login in China the language=zh-CN             $.getScript(src);    });</script>

这是使用jquery加载js文件。

 

接下来将项目中需要替换的内容全部用步骤3中的方法替换就行了,这样国际化就完成了。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Java Spring怎么实现定时任务Java Spring怎么实现定时任务May 24, 2023 pm 01:28 PM

java实现定时任务Jdk自带的库中,有两种方式可以实现定时任务,一种是Timer,另一种是ScheduledThreadPoolExecutor。Timer+TimerTask创建一个Timer就创建了一个线程,可以用来调度TimerTask任务Timer有四个构造方法,可以指定Timer线程的名字以及是否设置为为守护线程。默认名字Timer-编号,默认不是守护线程。主要有三个比较重要的方法:cancel():终止任务调度,取消当前调度的所有任务,正在运行的任务不受影响purge():从任务队

Java axios与spring前后端分离传参规范是什么Java axios与spring前后端分离传参规范是什么May 03, 2023 pm 09:55 PM

一、@RequestParam注解对应的axios传参方法以下面的这段Springjava代码为例,接口使用POST协议,需要接受的参数分别是tsCode、indexCols、table。针对这个Spring的HTTP接口,axios该如何传参?有几种方法?我们来一一介绍。@PostMapping("/line")publicList

Spring Boot与Spring Cloud的区别与联系Spring Boot与Spring Cloud的区别与联系Jun 22, 2023 pm 06:25 PM

SpringBoot和SpringCloud都是SpringFramework的扩展,它们可以帮助开发人员更快地构建和部署微服务应用程序,但它们各自有不同的用途和功能。SpringBoot是一个快速构建Java应用的框架,使得开发人员可以更快地创建和部署基于Spring的应用程序。它提供了一个简单、易于理解的方式来构建独立的、可执行的Spring应用

Spring 最常用的 7 大类注解,史上最强整理!Spring 最常用的 7 大类注解,史上最强整理!Jul 26, 2023 pm 04:38 PM

随着技术的更新迭代,Java5.0开始支持注解。而作为java中的领军框架spring,自从更新了2.5版本之后也开始慢慢舍弃xml配置,更多使用注解来控制spring框架。

Java Spring框架创建项目与Bean的存储与读取实例分析Java Spring框架创建项目与Bean的存储与读取实例分析May 12, 2023 am 08:40 AM

1.Spring项目的创建1.1创建Maven项目第一步,创建Maven项目,Spring也是基于Maven的。1.2添加spring依赖第二步,在Maven项目中添加Spring的支持(spring-context,spring-beans)在pom.xml文件添加依赖项。org.springframeworkspring-context5.2.3.RELEASEorg.springframeworkspring-beans5.2.3.RELEASE刷新等待加载完成。1.3创建启动类第三步,创

从零开始学Spring Cloud从零开始学Spring CloudJun 22, 2023 am 08:11 AM

作为一名Java开发者,学习和使用Spring框架已经是一项必不可少的技能。而随着云计算和微服务的盛行,学习和使用SpringCloud成为了另一个必须要掌握的技能。SpringCloud是一个基于SpringBoot的用于快速构建分布式系统的开发工具集。它为开发者提供了一系列的组件,包括服务注册与发现、配置中心、负载均衡和断路器等,使得开发者在构建微

Java Spring Bean生命周期管理的示例分析Java Spring Bean生命周期管理的示例分析Apr 18, 2023 am 09:13 AM

SpringBean的生命周期管理一、SpringBean的生命周期通过以下方式来指定Bean的初始化和销毁方法,当Bean为单例时,Bean归Spring容器管理,Spring容器关闭,就会调用Bean的销毁方法当Bean为多例时,Bean不归Spring容器管理,Spring容器关闭,不会调用Bean的销毁方法二、通过@Bean的参数(initMethod,destroyMethod)指定Bean的初始化和销毁方法1、项目结构2、PersonpublicclassPerson{publicP

spring设计模式有哪些spring设计模式有哪些Dec 29, 2023 pm 03:42 PM

spring设计模式有:1、依赖注入和控制反转;2、工厂模式;3、模板模式;4、观察者模式;5、装饰者模式;6、单例模式;7、策略模式和适配器模式等。详细介绍:1、依赖注入和控制反转: 这两个设计模式是Spring框架的核心。通过依赖注入,Spring负责管理和注入组件之间的依赖关系,降低了组件之间的耦合度。控制反转则是指将对象的创建和依赖关系的管理交给Spring容器等等。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools