1、在springmvc.xml中加上

 <!-- 多视图的支持 -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager">
<bean class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"/>
</property>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.xiaostudy.domain.User</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>

2、在支持多视图的类中加上注释@XmlRootElement

 package com.xiaostudy.domain;

 import javax.xml.bind.annotation.XmlRootElement;

 @XmlRootElement
public class User { private int id;
private String username;
private String password;
private int age; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + "]";
} }

3、使用

 package com.xiaostudy.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.xiaostudy.domain.User; @Controller//<bean class="com.xiaostudy.controller.MyController"/>
@RequestMapping(value="/myController")//访问该类的方法时,前面多这样一个路径
public class MyController { @RequestMapping("multiView")
public User multiView() {
User user = new User();
user.setId(2018);
user.setUsername("xiaostudy");
user.setPassword("123456");
user.setAge(23);
return user;
} }

4、访问


springMVC多视图的支持的更多相关文章

  1. SpringMVC 多视图解析器配置以及问题

    在SpringMVC模式当中可以通过如下配置来支持多视图解析 <!-- jsp jstl --> <bean id="JSPViewResolver" class ...

  2. springmvc上传图片并显示--支持多图片上传

    实现上传图片功能在Springmvc中很好实现.现在我将会展现完整例子. 开始需要在pom.xml加入几个jar,分别是: <dependency> <groupId>comm ...

  3. Springmvc多视图

    Springmvc多视图 多视图是一个方法可以返回json/xml等格式的数据 第一步:导入xml格式支持的jar包 spring-oxm-3.2.0.RC2.jar 第二步:配置支持多视图 < ...

  4. SpringMVC重定向视图RedirectView小分析

    目录 前言 RedirectView介绍 实例讲解 总结 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnbl ...

  5. SpringMvc:视图和视图解析器

    请求处理方法执行完成后,最终返回一个ModelAndView对象,对于返回String,View或ModelMap等类型的处理方法,SpringMvc也会在内部将它们装配成一个ModelAndView ...

  6. SpringMVC自定义视图Excel视图和PDF视图

    SpringMVC自定义视图 Excel视图和PDF视图 SpringMVC杂记(十一) 使用Excel视图 Spring MVC 视图解析器(ViewResolver ) java实现导出excel ...

  7. SpringMVC——说说视图解析器

    学习SpringMVC——说说视图解析器   各位前排的,后排的,都不要走,咱趁热打铁,就这一股劲我们今天来说说spring mvc的视图解析器(不要抢,都有位子~~~) 相信大家在昨天那篇如何获取请 ...

  8. SpringMVC源码分析6:SpringMVC的视图解析原理

    title: SpringMVC源码分析6:SpringMVC的视图解析原理 date: 2018-06-07 11:03:19 tags: - SpringMVC categories: - 后端 ...

  9. SpringMVC核心——视图渲染(包含视图解析)问题

    一.本来想说的是返回值处理问题,但在 SpringMVC 中,返回值处理问题的核心就是视图渲染.所以这里标题叫视图渲染问题. 本来想在上一篇文章中对视图解析进行说明的,但是通过源码发现,它应该算到视图 ...

随机推荐

  1. shell (check return of each line)(PIPESTATUS[@])and sudoer

    shell result from cmdline echo $? if 0 then success ;else failure (shell 执行每部返回值,rm -rf 错误,打包不能覆盖) 我 ...

  2. Spring Data 查询方法的规则定义(五)

    有句话这样说  欲练神功  挥刀自宫  请亲们先回到第一个  从Spring data 介绍 开始看  搭好环境 跟着步伐一块走 Spring Data 的方法必须严格按照它的规范进行编写,如果写错了 ...

  3. Codeforces441B_Valera and Fruits(暴力)

    Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. Install Haskell on Ubuntu and CentOS

    For Ubuntu: Step one: Install GHC If you don't want to install curl you can skip step 1 and just dir ...

  5. Hbase架构和读写流程

    转载自:http://www.cnblogs.com/muzili-ykt/p/muzili_ykt.html 在HBase读写时,相同Cell(RowKey/ColumnFamily/Column相 ...

  6. 理解tomcat之搭建简易http服务器

    做过java web的同学都对tomcat非常熟悉.我们在使用tomcat带来的便利的同时,是否想过tomcat是如何工作的呢?tomcat本质是一个http服务器,本篇文章将搭建一个简单的http服 ...

  7. 项目实战:Mahout构建图书推荐系统

    前言 本文是Mahout实现推荐系统的又一案例,用Mahout构建图书推荐系统.与之前的两篇文章,思路上面类似,侧重点在于图书的属性如何利用.本文的数据在自于Amazon网站,由爬虫抓取获得. 目录 ...

  8. 如何修改opencart的模版适合为mycncart系统使用

    如何修改opencart的模版适合为mycncart系统使用 mycncart跟随opencart的最新代码不断进行升级,并改造和不断加入中国特色的功能,因此opencart的模版均不能够拿来直接套用 ...

  9. Laravel 文档中的 Service Providers

    $this->app->singleton('ReportServices', function () { return new \App\Services\ReportServices( ...

  10. Django的orm练习---多表查询

    Django的orm练习---多表查询 表关系如下 表结构 : from django.db import models # Create your models here. # 多对多-----&g ...