spring mvc 多视图 freemarker json xml 以及自定义dataset

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder  ignore-resource-not-found="true"
                                   location="classpath:resource/*.properties,
                                  file:/root/web_home/manager/resource/*.properties" />
    <!-- 自动扫描且只扫描@Controller -->
    <context:component-scan base-package="com.tori.*.controller" />

    <bean id="contentNegotiationManager"
          class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <!-- 设置默认的MIME类型,如果没有指定拓展名或请求参数,则使用此默认MIME类型解析视图 -->
        <property name="defaultContentType" value="application/json" />
        <!-- 是否不适用请求头确定MIME类型 -->
        <property name="ignoreAcceptHeader" value="true" />
        <!-- 是否根据路径拓展名确定MIME类型 -->
        <property name="favorPathExtension" value="false" />
        <!-- 是否使用参数来确定MIME类型 -->
        <property name="favorParameter" value="true" />
        <!-- 上一个属性配置为true,我们指定type请求参数判断MIME类型 -->
        <property name="parameterName" value="format" />
        <!-- 根据请求参数或拓展名映射到相应的MIME类型 -->
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html" />
                <entry key="xml" value="application/xml" />
                <entry key="json" value="application/json" />
                <entry key="dataset" value="application/dataset" />
            </map>
        </property>
    </bean>

    <mvc:annotation-driven
            content-negotiation-manager="contentNegotiationManager">
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                        <property name="dateFormat">
                            <bean class="java.text.SimpleDateFormat">
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
                            </bean>
                        </property>
                    </bean>
                </property>
            </bean>
            <bean   class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                <constructor-arg ref="jaxbMarshaller" />
                <property name="supportedMediaTypes" value="application/xml"></property>
            </bean>

            <bean class="com.tori.common.context.DataSetHttpMessageConverter">
                <property name="supportedMediaTypes" value="application/dataset"></property>
            </bean>
        </mvc:message-converters>

        <mvc:argument-resolvers>
            <bean class="com.tori.common.context.DataSetHandlerMethodArgumentResolver" />
        </mvc:argument-resolvers>
    </mvc:annotation-driven>

    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.tori.storage.pojo.TestStorage</value>
            </list>
        </property>
    </bean>


    <bean id="freemarkerConfiguration"
          class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location" value="classpath:resource/freemarker.properties" />

    </bean>

    <!-- freemaker  Directive-->
    <bean id="blockDirective" class="cn.org.rapid_framework.freemarker.directive.BlockDirective"/>
    <bean id="extendsDirective" class="cn.org.rapid_framework.freemarker.directive.ExtendsDirective"/>
    <bean id="overrideDirective" class="cn.org.rapid_framework.freemarker.directive.OverrideDirective"/>
    <bean id="superDirective" class="cn.org.rapid_framework.freemarker.directive.SuperDirective"/>

    <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
          p:prefix="${TPL}" p:suffix=".ftl">

        <property name="cache" value="false" />
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
        <property name="contentType" value="text/html;charset=UTF-8"></property>
        <property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
        <property name="exposeSpringMacroHelpers" value="true" />
        <property name="requestContextAttribute" value="request" />
        <property name="order" value="0"></property>
    </bean>

    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
    <bean id="freemarkerConfig"
              class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="freemarkerVariables">
            <map>
                <entry key="xml_escape" value-ref="fmXmlEscape" />
                <entry key="s_multiple_url" value="${s_multiple_url}"></entry>
                <entry key="s_storage_url" value="${s_storage_url}"></entry>
                <entry key="s_cost_url" value="${s_cost_url}"></entry>
                <entry key="s_accounting_url" value="${s_accounting_url}"></entry>
                <entry key="s_quality_url" value="${s_quality_url}"></entry>
                <entry key="s_produce_url" value="${s_produce_url}"></entry>
                <entry key="s_purchase_url" value="${s_purchase_url}"></entry>
                <entry key="s_sales_url" value="${s_sales_url}"></entry>
                <entry key="s_base_url" value="${s_base_url}"></entry>
                <!--下面四个是在下面定义的模板继承-->
                <entry key="extends" value-ref="extendsDirective"/>
                <entry key="override" value-ref="overrideDirective"/>
                <entry key="block" value-ref="blockDirective"/>
                <entry key="super" value-ref="superDirective"/>
            </map>
        </property>
        <property name="freemarkerSettings" ref="freemarkerConfiguration" />
    </bean>

    <!-- 定义文件上传解析器 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设定默认编码 -->
        <property name="defaultEncoding" value="UTF-8"></property>
        <!-- 设定文件上传的最大值5MB,5*1024*1024 -->
        <property name="maxUploadSize" value="5242880"></property>
    </bean>

</beans>

自定义dataset

package com.tori.common.context;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.going.dataset.Dataset;
import com.going.enums.ArgumentType;
import com.going.exception.PlatformException;
import com.going.resolver.GoingArgument;
import com.going.utils.CollectionUtils;
import com.going.utils.LogUtils;
import com.going.utils.StringUtils;
import com.going.validation.resources.GoPropertyResourcesBundle;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.validation.*;
import java.lang.annotation.Annotation;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by Administrator on 2016/8/11.
 */
public class DataSetHandlerMethodArgumentResolver  implements HandlerMethodArgumentResolver {



    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        if (parameter.hasParameterAnnotation(DataSet.class)) {
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        //此次返回自定义格式
    }


}

 

转载于:https://my.oschina.net/gulaotou/blog/731157

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值