SpringMVC视图解析器以及国际化操作

视图的顶级接口:View
视图解析器:ViewResolver

常见的视图和解析器:
InternalResourceView、InternalResourceViewResolver

springMVC解析jsp时 会默认使用InternalResourceView,
如果发现Jsp中包含了jstl语言相关的内容,则自动转为JstlView。

JstlView 可以解析jstl\实现国际化操作

国际化: 针对不同地区、不同国家 ,进行不同的显示

想要实现国际化操作,就得依赖于jstl,因此需要导入jstl的两个jar:jstl.jar , standar.jar
一、创建资源文件
格式为:
基名_语言_地区.properties
基名_语言.properties
在这里插入图片描述
本文示例:
在这里插入图片描述
然后文件里面的书写 k=v 键值对,注意值必须转为ASCAII码。
i18n.properties是补漏文件,发现写的值没有在对应地区找到,就再i18n.properties中寻找。

怎么将普通文字转ASCAII码:
在JDK的bin目录下运行native2ascii.exe
在这里插入图片描述
.properties文件的内容
在这里插入图片描述
2、配置springmvc.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--扫描有注解的包-->
    <context:component-scan base-package="handler"></context:component-scan>

    <!--配置视图解析器() 该解析器的作用是将东西渲染成success.jsp页面-->
    <!--如果发现jsp中包含了jstl语言,则自动转为JstlView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

<!--加载国际化资源文件
1.将ResourceBundleMessageSouce在程序加载时  加入springMvc:会自动查找一个MessageSource的bean
2.如果配置了ResourceBundleMessageSource,则该类会在程序响应时介入
-->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>
    </bean>
</beans>

国际化响应的页面:注意国际jstl导入的的url不是下面的:
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
而是:<%@taglib uri=“http://java.sun.com/jsp/jstl/fmt” prefix=“fmt” %>

<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<!--下面就是jstl提供的国际化操作-->
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
    <title>Title</title>
</head>

<body>
	<br/>
		<fmt:message key="resource.welcome"></fmt:message>
		<fmt:message key="resource.exits"></fmt:message>
	<br/>
</body>
</html>

测试:
注意不能直接访问上面的页面,国际化操作只会在程序响应时介入

<a href="handler/testI18n">dadasdasdas</a>
    @RequestMapping("testI18n")
    public String testI18n(){
        return "success";
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

键盘歌唱家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值