`
awdxzc
  • 浏览: 332851 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

spring MVC 配置多视图模块,多输出格式

阅读更多
spring mvc 可能要配置多个视图模式,比方说需求要求有jsp和freemarker 2种输出文件格式。freemarker可以自己定制

xml|json|html等。
面对这样的需求,显然单纯的mvc的jsp视图模板是不够的,所以需要spring对freemarker进行支持。
对于这样的问题,有多种解决方案,就我用过的2中说说吧:
1:使用org.springframework.web.servlet.view.ResourceBundleViewResolver
直接上代码:
<!--配置一个ResourceBundleViewResolver 配置一个spring-views.properites 文件(必须在根目录) 加载的顺序是最优先 

"order = 0" -->
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
	<property name="basename" value="spring-views" />
	<property name="order" value="0" />
</bean>

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">           

 <property name="templateLoaderPath">
	      <value>/WEB-INF/views/modules</value>
	</property>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/views/"/>
	<property name="suffix" value=".jsp"/>
</bean>


spring-views.properties:
articles.(class)=org.springframework.web.servlet.view.JstlView
articles.url=articles.jsp

module-content-articles.(class)=org.springframework.web.servlet.view.freemarker.FreeMarkerView
module-content-articles.url=modules/content/html/articles.ftl

这里的class指定的是解析的视图类,url指定的是视图页面。

Controller类中请求方式:
return "module-content-articles";






我用的是另一种配置多视图的方式:

<!--
	<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
		<property name="basename" value="spring-views" />
		<property name="order" value="0" />
	</bean>-->

	<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
	    <property name="templateLoaderPath">
	        <value>/WEB-INF/views/modules</value>
	    </property>
	</bean>
	
	<bean id="xmlviewResolver"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="suffix" value=".xml.ftl" />
        <property name="contentType" value="text/xml;charset=UTF-8"></property>
    </bean>
    
    <bean id="htmlviewResolver"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="suffix" value=".html.ftl" />
        <property name="contentType" value="text/html;charset=UTF-8"></property>
    </bean>
    
    <bean id="jsonviewResolver"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="suffix" value=".json.ftl" />
        <property name="contentType" value="application/text;charset=UTF-8"></property>
    </bean>

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>

这样就没有使用org.springframework.web.servlet.view.ResourceBundleViewResolver了,这样的话spring-views.properties文

件就没有存在的意义了。


Controller类:
return "content/"+format+"/articles";


目录结构
分享到:
评论
5 楼 kingzhe2011 2014-07-29  
4 楼 tianyifeng1989 2013-12-13  
<property name="suffix" value=".xml.ftl" />  是不是可以是xml文件和ftl文件?可是我的出错了说找不到


<bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
        <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> 
        <property name="contentType" value="text/html; charset=utf-8"/> 
        <property name="cache" value="true" /> 
        <property name="suffix" value=".html.ftl" />
        <property name="order" value="0"/> 
        <property name="exposeRequestAttributes" value="false"/>
<property name="exposeSessionAttributes" value="false"/>
<property name="exposeSpringMacroHelpers" value="true"/>
    </bean>
     
    <!-- 配置freeMarker的模板路径 --> 
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
        <property name="templateLoaderPath"> 
            <value>/WEB-INF/view/</value> 
        </property>   
        <property name="defaultEncoding"> 
            <value>utf-8</value> 
        </property> 
        <property name="freemarkerSettings"> 
            <props> 
                <prop key="template_update_delay">0</prop> 
            </props> 
        </property> 
    </bean>
3 楼 qq630703545 2013-09-14  
第2种方式如果 一个jsp文件叫做 test ftl也叫test 会有问题啊
2 楼 awdxzc 2011-01-24  
sunnylocus 写道
能解释下么?
引用
这样就没有使用org.springframework.web.servlet.view.ResourceBundleViewResolver了,这样的话spring-views.properties文

件就没有存在的意义了。
但我看在文件还是配置了ResourceBundleViewResolver,不自相矛盾么?


请看清楚,org.springframework.web.servlet.view.ResourceBundleViewResolver 在第2种方法里面已经被注释掉了。
1 楼 sunnylocus 2011-01-12  
能解释下么?
引用
这样就没有使用org.springframework.web.servlet.view.ResourceBundleViewResolver了,这样的话spring-views.properties文

件就没有存在的意义了。
但我看在文件还是配置了ResourceBundleViewResolver,不自相矛盾么?

相关推荐

    Spring MVC+MyBatis开发从入门到项目实战

    第3篇是Spring MVC技术入门,包括Spring MVC的背景介绍、架构整体剖析、环境搭建、处理器与映射器的讲解、前端控制器的源码分析、多种视图解析器的介绍、请求映射与参数绑定的介绍、Validation校验与异常处理和拦截...

    spring框架的MVC

    Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是 Struts 这样的 Web 框架。通过策略接口,Spring 框架是高度可配置的,而且...

    spring mvc 框架示例

    Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是 Struts 这样的 Web 框架。通过策略接口,Spring 框架是高度可配置的,而且...

    全面掌握Spring MVC:从基础到高级的实践指南

    Spring MVC是Spring框架的一个模块,专注于构建Web应用程序。作为架构师和Java开发者,深入理解Spring MVC的原理和实践应用是非常重要的。本文通过分析Spring MVC的核心组件和执行流程,提供了一个全面的学习指南。 ...

    spring mvc 0604

    Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是 Struts 这样的 Web 框架。通过策略接口,Spring 框架是高度可配置的,而且...

    Spring+MVC+Mybatis 书城项目

    Spring MVC: Spring MVC是Spring框架的一个模块,它实现了MVC(模型-视图-控制器)设计模式,用于构建Web应用。Spring MVC允许你将应用的逻辑、数据和用户界面分离,使得代码更加清晰和易于维护。 MyBatis: MyBatis...

    使用Spring MVC和JSP构建的全栈RSS阅读器Web应用程序(95分以上课程大作业).zip

    Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...

    spring mvc中文教程

    本文将指导大家如何使用Spring MVC3去构建一个Web应用。在Spring MVC中,所有的用户请求都会被派发到控制器模块,控制器然后再根据业务逻辑去调用数据访问层获得数据,最后通过JSP视图返回。

    Spring MVC简单使用

    本文将指导大家如何使用Spring MVC3去构建一个Web应用。在Spring MVC中,所有的用户请求都会被派发到控制器模块,控制器然后再根据业务逻辑去调用数据访问层获得数据,最后通过JSP视图返回。

    用于将所有Spring xml配置转换为基于Spring java的配置的工具(高分毕设).zip

    Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...

    springweb3.0MVC注解(附实例)

    web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示: ...

    使用Spring MVC3构建Web应用详细教程

    本文将指导大家如何使用Spring MVC3去构建一个Web应用。在Spring MVC中,所有的用户请求都会被派发到控制器模块,控制器然后再根据业务逻辑去调用数据访问层获得数据,最后通过JSP视图返回。

    spring技术

    Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是 Struts 这样的 Web 框架。通过策略接口,Spring 框架是高度可配置的,而且...

    Spring MVC4.3.5+MyBatis3.4.2+Apache Shiro1.3.2整合开发高仿小米商城的后台用户管理系统

    视图框架:Spring MVC 4.3.5 任务调度:Spring + Quartz 2.2.3 持久层框架:MyBatis 3.4.2 + Mybatis-plus 2.0.1 数据库连接池:Alibaba Druid 1.0 缓存框架:Ehcache 2.6 + Redis 2.9.0 日志管理:SLF4J 1.7 + Log4...

    高级Spring Boot和Spring Cloud微服务示例 Zuul上的API Swagger2

    Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...

    Spring Boot ( Jetty )、Spring Cloud库、Netflix OSS工具、Docker

    Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...

    使用Spring in Guice和Guice in Spring的工具(高分项目).zip

    Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...

    java、spring-boot、spring-tool-suite、maven、restful-api

    Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...

    Spring Boot Migrator (SBM)是一种用于自动代码迁移以升级或迁移到Spring Boot的工具

    Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...

    JAVAspring学习资料

    Spring MVC:用于构建Web应用程序的模块,提供了基于MVC(模型-视图-控制器)的开发方式,简化了Web应用程序的开发和测试2. Spring Data:用于简化与数据库的交互,提供了对多种数据库的支持,并提供了一套统一的API...

Global site tag (gtag.js) - Google Analytics