/**
* 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>