013 JstlView

一:InternalResourceViewResolverhtml

1.InternalResourceViewResolverjava

  JSP是常見的視圖技術,可使用InternalResourceViewResolver做爲視圖解析器。web

 

二:JstlViewspring

1.JstlView說明mvc

  若是項目中使用了JSTL。app

  InternalResourceViewResolver會將InternalResourceView轉爲JstlView。jsp

 

2.fmt標籤說明ui

  若是使用fmt標籤。spa

  須要在springmvc配置文件中配置國際化資源文件。code

 

三:程序

1.添加jstl須要的lib包

  

 

2.添加配置文件

  

 

3.success

  須要導入fmt標籤庫。

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11     success page
12     <br><br>
13     <fmt:message key="i18n.username"></fmt:message><br>
14     <br>
15     <fmt:message key="i18n.password"></fmt:message><br>
16 </body>
17 </html>

 

4.配置國際化資源文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans     
 6                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 7                        http://www.springframework.org/schema/context 
 8                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 9     <!-- 配置自定義掃描的包 -->               
10     <context:component-scan base-package="com.spring.it" ></context:component-scan>
11     
12     <!-- 配置視圖解析器 -->
13     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
14         <property name="prefix" value="/WEB-INF/views/" />
15           <property name="suffix" value=".jsp" />
16     </bean>
17     
18     <!-- 配置國際化資源文件 -->
19     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
20         <property name="basename" value="i18n"></property>
21     </bean>
22 </beans>

 

5.controller

 1 package com.spring.it;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 @Controller
 7 public class HelloWorldControl {
 8     @RequestMapping("/helloworld")
 9     public String hello() {
10         System.out.println("hello world*");
11         return "success";
12     }
13 }

 

6.index

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <a href="helloworld">Test Jstl</a>
11 </body>
12 </html>

 

7.效果

  

相關文章
相關標籤/搜索