zoukankan      html  css  js  c++  java
  • Springmvc-配置多视图解析器 ContentNegotiatingViewResolver

    需求说明:查看用户明细,要求返回的数据以json纯数据的格式进行输出

     修改springmvc-servlet.xml配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:mvc="http://www.springframework.org/schema/mvc"
     5     xmlns:p="http://www.springframework.org/schema/p"
     6     xmlns:context="http://www.springframework.org/schema/context"
     7     xsi:schemaLocation="
     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         http://www.springframework.org/schema/mvc
    13         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    14         
    15     <context:component-scan base-package="cn.smbms.controller"/>    
    16     
    17     <mvc:annotation-driven>
    18         <mvc:message-converters>
    19             <bean class="org.springframework.http.converter.StringHttpMessageConverter">
    20                 <property name="supportedMediaTypes">
    21                     <list>
    22                         <value>application/json;charset=UTF-8</value>
    23                     </list>
    24                 </property>
    25             </bean> 
    26             <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
    27                 <property name="supportedMediaTypes">
    28                     <list>
    29                         <value>text/html;charset=UTF-8</value>
    30                         <value>application/json</value>
    31                     </list>
    32                 </property>
    33                 <property name="features">
    34                     <list>
    35                       <!--  Date的日期转换器 -->
    36                       <value>WriteDateUseDateFormat</value>
    37                     </list>
    38                 </property>
    39             </bean>
    40         </mvc:message-converters>
    41     </mvc:annotation-driven>
    42     
    43     <mvc:resources mapping="/statics/**" location="/statics/" />
    44     <!-- 完成视图的对应 -->
    45     <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
    46     <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    47         <property name="prefix" value="/WEB-INF/jsp/"/>
    48         <property name="suffix" value=".jsp"/>
    49     </bean> -->
    50     
    51     <!-- 配置多视图解析器:允许同样的内容数据呈现不同的view -->
    52     <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    53         <property name="favorParameter" value="true"/>
    54         <property name="defaultContentType" value="text/html"/>
    55         <property name="mediaTypes">
    56             <map>
    57                 <entry key="html" value="text/html;charset=UTF-8"/>
    58                 <entry key="json" value="application/json;charset=UTF-8"/>
    59                 <entry key="xml" value="application/xml;charset=UTF-8"/>
    60             </map>
    61         </property>
    62         <!--配置网页的视图解析器  -->
    63         <property name="viewResolvers">
    64             <list>
    65                 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    66                     <property name="prefix" value="/WEB-INF/jsp/"/>
    67                     <property name="suffix" value=".jsp"/>
    68                 </bean>        
    69             </list>
    70         </property>
    71     </bean>
    72     
    73     <!--  全局异常处理 -->
    74     <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    75         <property name="exceptionMappings">
    76             <props>
    77                 <prop key="java.lang.RuntimeException">error</prop>
    78             </props>
    79         </property>
    80     </bean>
    81     
    82     <!-- 配置MultipartResolver,用于上传文件,使用spring的CommonsMultipartResolver -->  
    83     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    84             <property name="maxUploadSize" value="5000000"/>
    85             <property name="defaultEncoding" value="UTF-8"/>
    86     </bean>
    87     
    88 </beans>

    修改userlist.js

     UserController.java保持不动

     启动项目,在浏览器上面直接输入地址:(能够看到我们想要的结果)

     还可以换一种实现方式:

    修改userlist.js

     重新启动项目:在浏览器中数据地址信息,得到如下结果:

  • 相关阅读:
    Eclipse JSP/Servlet 环境搭建
    1,有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
    Electron-vue实战(二)— 请求Mock数据渲染页面
    Electron-vue实战(一)—搭建项目与安装Element UI
    Electron-vue实战(三)— 如何在Vuex中管理Mock数据
    vue学习笔记(六)— 关于Vuex可以这样简单理解
    vue学习笔记(五)— 组件通信
    OpenLayers学习笔记(十二)— 飞机速度矢量线预测(二)
    QML学习笔记(八)— QML实现列表侧滑覆盖按钮
    重学JavaScript
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/12262720.html
Copyright © 2011-2022 走看看