SPRING IN ACTION 第4版笔记-第六章Rendering web views-001- Spring支持的View Resolver、InternalResourceViewResolver、JstlView

一、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

SPRING IN ACTION 第4版笔记-第六章Rendering web views-001- Spring支持的View Resolver、InternalResourceViewResolver、JstlView的相关文章

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-006- 使用thymeleaf(TemplateResolver、SpringTemplateEngine、ThymeleafViewResolver、th:include、th:object、th:field=&quot;*{firstName}&quot;)

一.在Spring中使用thymeleaf的步骤 1.配置 In order to use Thymeleaf with Spring, you’ll need to configure three beans that enable Thymeleaf-Spring integration:? A ThymeleafViewResolver that resolves Thymeleaf template views from logical view names? A SpringTempl

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-003- SPRING的GENERAL TAG LIBRARY简介及用&lt;s:message&gt;和ReloadableResourceBundleMessageSource实现国际化

一. SPRING支持的GENERAL TAG LIBRARY 1. 二.用<s:message>和ReloadableResourceBundleMessageSource实现国际化 1.配置ReloadableResourceBundleMessageSource,它能it has the ability to reload message properties without recompiling or restarting the application. 1 package spi

SPRING IN ACTION 第4版笔记-第六章RENDERING WEB VIEWS-005- 使用ApacheTiles(TilesConfigurer、TilesViewResolver、&lt;put-attribute&gt;、&lt;t:insertAttribute&gt;)

一. 1.定义TilesConfigurer.TilesViewResolver的bean 注意有tiles2和tiles3,这里使用tiles3 (1)java形式 1 package spittr.web; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.ComponentScan; 5 import org.springframew

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode

一. Spring的bean默认是单例的 But sometimes you may find yourself working with a mutable class that does main-tain some state and therefore isn’t safe for reuse. In that case, declaring the class as asingleton bean probably isn’t a good idea because that obje

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法

一. 1.Introduction的作用是给类动态的增加方法 When Spring discovers a bean annotated with @Aspect , it will automatically create a proxy that delegates calls to either the proxied bean or to the introduction implementation, depending on whether the method called be

SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-006Spring-Data的运行规则(@EnableJpaRepositories、&lt;jpa:repositories&gt;)

一.JpaRepository 1.要使Spring自动生成实现类的步骤 (1)配置文件xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xm

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)

一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's Lonely Hearts Club Band", "The Beatles"); } < bean id = "sgtPeppers" class = "soundsystem.BlankDisc" c: _title = &quo

SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-002-Controller的requestMapping、model

一.RequestMapping 1.可以写在方法上或类上,且值可以是数组 1 package spittr.web; 2 3 import static org.springframework.web.bind.annotation.RequestMethod.*; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.ui.Model; 7 import org.springfra

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍

一. 1.SpEL expressions are framed with  #{ ... } 2.SpEl的作用 Sp EL has a lot of tricks up its sleeves, including the following:? The ability to reference beans by their ID s? Invoking methods and accessing properties on objects? Mathematical, relational