29、(知識篇)SpringMVC06 Spring jstlview國際化/訪問/web-inf/下的jsp/訪問靜態資源

/**

* Spring jstlview國際化/訪問/web-inf/下的jsp/訪問靜態資源

* 1、國際化 在bean中配置 org.springframework.context.support.ResourceBundleMessageSource

* ***注意,這個類很奇怪一定要寫id="messageSource",不寫的話國際化無效,可能springmvc源碼有限制,有時間再研究

* 指定引入國際化的文文件

* <!-- 國際化資源文件 -->

* <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="i18in"></property></bean>

* 2、直接訪問jsp

* <!-- 在這裏配置可以直接訪問web-inf下的資源

path:訪問路徑

view-name:jsp文件名(因爲上面配置了org.springframework.web.servlet.view.InternalResourceViewResolver所以不用寫全稱)

-->

<mvc:view-controller path="/error" view-name="error"/>

* 3、訪問靜態資源

* <!-- 

配置這個可以訪問外部資源 例子爲WebContent下js/jquery-3.1.1.js

不配置會報404

-->

<mvc:default-servlet-handler/>

是因爲tomcat指定了org.apache.catalina.servlets.DefaultServlet

所以會判斷到其他文件爲靜態文件

* @return

*/

測試類:

package com.spring.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
	
	/**
	 * Spring jstlview國際化/訪問/web-inf/下的jsp/訪問靜態資源
	 * 
	 * 1、國際化 在bean中配置 org.springframework.context.support.ResourceBundleMessageSource
	 * ***注意,這個類很奇怪一定要寫id="messageSource",不寫的話國際化無效,可能springmvc源碼有限制,有時間再研究
	 * 指定引入國際化的文文件
	 * <!-- 國際化資源文件 -->
	 * <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="i18in"></property> </bean>
	 * 
	 * 2、直接訪問jsp
	 * <!-- 在這裏配置可以直接訪問web-inf下的資源
		path:訪問路徑
		view-name:jsp文件名(因爲上面配置了org.springframework.web.servlet.view.InternalResourceViewResolver所以不用寫全稱)
	 	-->
		<mvc:view-controller path="/error" view-name="error"/>
	 * 
	 * 
	 * 3、訪問靜態資源
	 * <!-- 
			配置這個可以訪問外部資源 例子爲WebContent下js/jquery-3.1.1.js
			不配置會報404
		 -->
		<mvc:default-servlet-handler/>
		是因爲tomcat指定了org.apache.catalina.servlets.DefaultServlet
		所以會判斷到其他文件爲靜態文件
	 * 
	 * 
	 * @return
	 */
	
	
	@RequestMapping("testJSTLView")
	public String testJSTLView(){
		return "index";
	}
	
	
	
}

i18in_zh_CN.properties

i18n.username=測試
i18n.age=20

i18in.properties

i18n.username=username
i18n.age=20

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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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-4.3.xsd">
	<context:component-scan base-package="com.spring"></context:component-scan>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="suffix" value=".jsp"></property>
		<property name="prefix" value="/WEB-INF/views/"></property>
	</bean>
	
	<!-- 國際化資源文件 一定要寫 id="messageSource" -->
	
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="i18in"></property>
	</bean>
	
	<!-- 在這裏配置可以直接訪問web-inf下的資源
		path:訪問路徑
		view-name:jsp文件名(因爲上面配置了org.springframework.web.servlet.view.InternalResourceViewResolver所以不用寫全稱)
	 -->
	<mvc:view-controller path="/error" view-name="error"/>
	
	<!-- 
		配置這個可以訪問外部資源 例子爲WebContent下js/jquery-3.1.1.js
		不配置會報404
	 -->
	<mvc:default-servlet-handler/>
	
	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>27SpringMVC04</display-name>
	<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

web-inf/views/index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<fmt:message key="i18n.username"></fmt:message>
</body>
</html>

web-inf/views/error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	直接訪問
</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章