SringMVC 国际化
SringMVC 国际化
0.依赖(不只是本案例所需)
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gxeom</groupId>
<artifactId>bookshop</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--1.servlet-api jsp-api 2个jar包-->
<!--作用:
1. Springmvc 当中要用servlet对象 request session
2. Springmvc web.xml配置 dispatcherServlet 继承httpServlet
3.Springmvc 的注解 -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!--2.数据库 3个jar包 -->
<!-- mysql的连接驱动 版本要和自己的数据库版本相匹配 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- 连接池的依赖-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 我为了使用JdbcTemplate 所以我们要导入spring-jdbc-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--3.spring核心 3个jar包(SpEL)不必须导入 -->
<!-- spring 核心依赖 beans core context SpEL(不是必须导入)-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--4. Spring-web+ Spring-mvc+json 3个jar包-->
<!-- 要和web相结合 spring-web-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- Spring mvc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- springmvc配套的第三方json依赖-->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<!--5.aop+aspectj 3个jar包-->
<!-- AOP ASPECTJ-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--aop切点-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<!--动态代理-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1</version>
</dependency>
<!-- 6.jstl标签 2个jar包
作用: 页面上去掉 scriptlet(脚本) 页面上获值并输出-->
<!--jstl+standard-->
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--7.图片上传 2个jar包-->
<!--字节流+图片-->
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>
1.xml配置(WEB-INF下)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--转发控制器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置classpath contextConfigLocation 默认classpath WEB-INF--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Spring-MVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- <error-page>--> <!-- <error-code>404</error-code>--> <!-- <location>/fristex.do</location>--> <!-- </error-page>--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- <servlet-mapping>--> <!-- <servlet-name>default</servlet-name>--> <!-- <url-pattern>*.js</url-pattern>--> <!-- <url-pattern>*.css</url-pattern>--> <!-- <url-pattern>*.map</url-pattern>--> <!-- <url-pattern>*.jpg</url-pattern>--> <!-- <url-pattern>*.png</url-pattern>--> <!-- </servlet-mapping>--> </web-app>
2.Spring-MVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- <mvc:resources mapping="/js/**" location="js/"></mvc:resources>-->
<!-- mvc驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.tjetc.*"></context:component-scan>
</bean>
<!-- 国际化
1. 位置对象 id="localeResolver" 不能改,内部属性 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:language"/>
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"/>
</bean>
</mvc:interceptors>
</beans>
3.resources 下两个资源文件
(1) language_en_US.properties
language.cn =\u4e2d\u6587 language.en =English internationalisation = \u0020Internationalisation welcome = This is the English environment introduce= This is I18N Demo
(2)language_en_US.properties
language.cn =\u4e2d\u6587 language.en =English internationalisation = \u56fd\u6945\u5316 welcome = \u8fd9\u662f\u4e2d\u6587\u73af\u5883 introduce= \u8fd9\u662f\u56fd\u9645\u5316\u7684\u4e8b\u4f8b
4.GuoJiHua (Controller)
package com.tjetc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
@Controller
public class GuoJiHua {
// @RequestMapping("/guo.do")
// public String index() {
//
// return "/WEB-INF/success.jsp";
// }
//只是为了回顾 1.MultipartFile(图片上传)
// 2.ModelAndView (可接受值,可返回值)
@RequestMapping("/mv.do")
public ModelAndView mv(ModelAndView mv, MultipartFile file){
mv.setViewName("/WEB-INF/success.jsp");
return mv;
}
// @RequestMapping("v.do")
// public View v(View view){
//
// view
// }
}
5.success.jsp (WEB-INF下)
<%--
Created by IntelliJ IDEA.
User: mazhic
Date: 2020/6/8
Time: 14:21
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<title>SpringMVC<spring:message code="internationalisation"/></title>
</head>
<body>
Language:
<a href="?lang=zh_CN"><spring:message code="language.cn"/></a>
<a href="?lang=en_US"><spring:message code="language.en"/></a>
<h1>
<spring:message code="welcome"/>
</h1>
当前语言: ${pageContext.response.locale }
</body>
</html>

浙公网安备 33010602011771号