一、Spring支持的View Resolver
二、InternalResourceViewResolver
Spring supports JSP views in two ways:
? InternalResourceViewResolver
? Spring provides two JSP tag libraries, one for form-to-model binding and one providing general utility features.
1.在java中定义
1 package spittr.web; 2 3 import org.springframework.context.MessageSource; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.ComponentScan; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.support.ReloadableResourceBundleMessageSource; 8 import org.springframework.web.servlet.ViewResolver; 9 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; 10 import org.springframework.web.servlet.config.annotation.EnableWebMvc; 11 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 12 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 13 import org.springframework.web.servlet.view.InternalResourceViewResolver; 14 15 @Configuration 16 @EnableWebMvc 17 @ComponentScan("spittr.web") 18 public class WebConfig extends WebMvcConfigurerAdapter { 19 20 @Bean 21 public ViewResolver viewResolver() { 22 InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 23 resolver.setPrefix("/WEB-INF/views/"); 24 resolver.setSuffix(".jsp"); 25 return resolver; 26 }
2.在xml中定义
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans:beans xmlns="http://www.springframework.org/schema/mvc" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:beans="http://www.springframework.org/schema/beans" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc 7 http://www.springframework.org/schema/mvc/spring-mvc.xsd 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd"> 12 13 <annotation-driven /> 14 15 <context:component-scan base-package="spitter" /> 16 17 <beans:bean 18 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 19 <beans:property name="prefix" value="/WEB-INF/views/" /> 20 <beans:property name="suffix" value=".jsp" /> 21 </beans:bean> 22 23 <resources mapping="/resources/**" location="/resources/" /> 24 25 <!-- 26 <view-controller path="/" view-name="home" /> 27 --> 28 29 </beans:beans>
By resolving JstlView , the JSTL tags will be given the Locale and any message source configured in Spring.
3.
1 @Bean 2 public ViewResolver viewResolver() { 3 InternalResourceViewResolver resolver = 4 new InternalResourceViewResolver(); 5 resolver.setPrefix("/WEB-INF/views/"); 6 resolver.setSuffix(".jsp"); 7 resolver.setViewClass( 8 org.springframework.web.servlet.view.JstlView.class); 9 return resolver; 10 }
4.
1 <bean 2 id="viewResolver" 3 class="org.springframework.web.servlet.view. 4 InternalResourceViewResolver" 5 p:prefix="/WEB-INF/views/" 6 p:suffix=".jsp" 7 p:viewClass="org.springframework.web.servlet.view.JstlView" />
Whether you use Java configuration or XML , this will ensure that JSTL ’s formatting and message tags will get the Locale and message sources configured in Spring.
时间: 2024-12-21 02:51:27