首页 > 编程语言 > 详细

springmvc之使用JstlView

时间:2020-01-11 14:48:06      阅读:83      评论:0      收藏:0      [点我收藏+]

1.导入包

  • jstl.jar
  • standard.jar

2.在springmvc.xml中配置

    <!-- 配置国际化资源文件 -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>    
    </bean>

3.配置以下文件

技术分享图片

i18n_en_US.properties

i18n.username=Username
i18n.password=Password

i18n_zh_CN.properties(实际上是用户名,密码)

i18n.username=\u7528\u6237\u540D
i18n.password=\u5BC6\u7801

i18n.properties

i18n.username=Username
i18n.password=Password

springmvcTest.java

@RequestMapping("/springmvc")
@Controller
public class SpringmvcTest {
private static final String SUCCESS = "success"; @RequestMapping(
"/test") public String test() { System.out.println("RequestmMapping"); return SUCCESS; } }

index.jsp

<a href="springmvc/test">test</a>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <p>Success</p>
    <fmt:message key="i18n.username"></fmt:message>
    <br><br>
    <fmt:message key="i18n.password"></fmt:message>
    <br><br>    
</body>
</html>

需要引入<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>。

启动服务器:

在IE浏览器中输入:http://localhost:8080/springmvc1/

技术分享图片

点击:转到success.jsp

技术分享图片

选择Inteernet选项--》语言--》添加english(美国) --》将该语言上移至第一位,刷新浏览器之后:

技术分享图片

完成了国际化的操作。

说明:

(1) 若项目中使用了JSTL,则spingmvc会自动把视图由InternalResourceView转换为JstlView。

(2)使用Jstl的fmt标签则需要在springmvc中配置国际化资源文件。

(3)若希望直接响应通过springmvc渲染的页面,可以使用mvc:view-controller实现,下节在写。

springmvc之使用JstlView

原文:https://www.cnblogs.com/xiximayou/p/12179778.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!