- 1.Spring配置的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:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
- <context:component-scan
- base-package="com.spring.action"/>
- <!--
- org.springframework.web.servlet.view.ResourceBundleViewResolver
- 用于多个视图集成时,ResourceBundleViewResolver是通过解析资源文件来解析请求输出文件的。
- <property name="basename" value="views"></property>,即表示在/WEB-INF/classes路径下有一个
- views.properties文件,本例中views.properties的内容为
- welcome.(class)=org.springframework.web.servlet.view.velocity.VelocityView
- welcome.url=welcome.vm
- freemarker.(class)=org.springframework.web.servlet.view.freemarker.FreeMarkerView
- freemarker.url=freemarker.ftl
- -->
- <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
- <property name="basename" value="views">
- </property>
- <!--
- <property name="order" value="0"></property>
- -->
- </bean>
- <!-- FreeMarker环境配置 -->
- <bean id="freemarkerConfig"
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
- <!-- freemarker模板位置 -->
- <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
- </bean>
- <!-- velocity环境配置 -->
- <bean id="velocityConfig"
- class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
- <!-- velocity配置文件路径 -->
- <property name="configLocation" value="/WEB-INF/velocity.properties"/>
- <!-- velocity模板路径 -->
- <property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
- </bean>
- <bean id="viewResolver"
- class="com.spring.action.MultiViewResover">
- <property name="resolvers">
- <map>
- <entry key="jsp">
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView"/>
- <property name="prefix" value="/WEB-INF/jsp/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- </entry>
- <entry key="ftl">
- <bean
- class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
- <property name="cache" value="true"/>
- <property name="prefix" value="/"/>
- <property name="suffix" value=".ftl"/>
- </bean>
- </entry>
- <entry key="vm">
- <bean
- class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
- <property name="cache" value="true"/>
- <property name="prefix" value="/"/>
- <property name="suffix" value=".vm"/>
- </bean>
- </entry>
- </map>
- </property>
- </bean>
- </beans>
2 解析多视图请求的类
- package com.spring.action;
- import java.util.Locale;
- import java.util.Map;
- import org.springframework.web.servlet.View;
- import org.springframework.web.servlet.ViewResolver;
- /**
- * @author Jeson
- * @blog:http://www.gbsou.com
- * @date:Oct 20, 2009 8:39:27 AM
- * @version :1.0
- *
- */
- public class MultiViewResover implements ViewResolver {
- private Map<String, ViewResolver> resolvers;
- @Override
- public View resolveViewName(String viewName, Locale locale)
- throws Exception {
- int n = viewName.lastIndexOf("_"); // 获取
- // viewName(modelAndView中的名字)看其有没有下划线
- if (n == (-1))
- return null; // 没有则直接返回空
- // 有的话截取下划线后面的字符串 这里一般是jsp,ftl,vm与配置文件中的<entry key="ftl">的key匹配
- String suffix = viewName.substring(n + 1);
- // 根据下划线后面的字符串去获取托管的视图解析类对象
- ViewResolver resolver = resolvers.get(suffix);
- // 取下划线前面的部分 那时真正的资源名.比如我们要使用hello.jsp 那viewName就应该是hello_jsp
- viewName = viewName.substring(0, n);
- if (resolver != null)
- return resolver.resolveViewName(viewName, locale);
- return null;
- }
- public Map<String, ViewResolver> getResolvers() {
- return resolvers;
- }
- public void setResolvers(Map<String, ViewResolver> resolvers) {
- this.resolvers = resolvers;
- }
- }
3. action
- package com.spring.action;
- import java.util.HashMap;
- import java.util.Map;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
- @Controller
- public class HelloWorldAction {
- @RequestMapping(value = "/helloworld.do")
- public ModelAndView hello() {
- ModelAndView modelAndView = new ModelAndView();
- modelAndView.setViewName("hello_jsp");
- modelAndView.addObject("message", "Hello,SpringMvc");
- return modelAndView;
- }
- @RequestMapping(value = "/welcome.do")
- public ModelAndView helloVm() {
- // ModelAndView modelAndView=new ModelAndView();
- // modelAndView.setViewName("freemarker_ftl");
- // modelAndView.addObject("message", "Hello,Velocity");
- // return modelAndView;
- Map map = new HashMap();
- return new ModelAndView("freemarker_ftl", map); // 根据之前的解释这里就是去寻找
- // user.ftl资源
- }
- @RequestMapping(value = "/free.do")
- public ModelAndView helloFreeMarker() {
- ModelAndView modelAndView = new ModelAndView();
- modelAndView.setViewName("welcome");
- modelAndView.addObject("message", "Hello,Freemarker");
- return modelAndView;
- }
- }
这样 在页面键入对应action就可以访问对应的视图实现了