SpringMVC中如何实现返回JSON、XML、PDF、图片等多种格式数据的多种返回方式?
- 内容介绍
- 文章标签
- 相关推荐
本文共计508个文字,预计阅读时间需要3分钟。
Spring MVC 返回方式 - 摘录自CSDN+sunhuwh的博客javapackage com.boventech.learning.controller;
import java.util.HashMap;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;
spring mvc返回方式-摘抄于csdn sunhuwh的博客package com.boventech.learning.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.boventech.learning.entity.User;
/**
* MVCReturn
* @author peng.xia
*
*/
@Controller
@RequestMapping("/MVCReturn")
public class SpringMVCReturnController {
@RequestMapping(value="/index1",method=RequestMethod.GET)
public ModelAndView index(){
ModelAndView modelAndView = new ModelAndView("/user/index");
modelAndView.addObject("name", "xxx");
return modelAndView;
}
//对于ModelAndView构造函数可以指定返回页面的名称,也可以通过setViewName方法来设置所需要跳转的页面;
@RequestMapping(value="/index2",method=RequestMethod.GET)
public ModelAndView index2(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name", "xxx");
modelAndView.setViewName("/user/index");
return modelAndView;
}
//返回的是一个包含模型和视图的ModelAndView对象;
/**
* Model一个模型对象,
* 主要包含spring封装好的model和modelMap,以及java.util.Map,
* 当没有视图返回的时候视图名称将由requestToViewNameTranslator决定;
* @return
*/
@RequestMapping(value="/index3",method=RequestMethod.GET)
public Map
本文共计508个文字,预计阅读时间需要3分钟。
Spring MVC 返回方式 - 摘录自CSDN+sunhuwh的博客javapackage com.boventech.learning.controller;
import java.util.HashMap;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;
spring mvc返回方式-摘抄于csdn sunhuwh的博客package com.boventech.learning.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.boventech.learning.entity.User;
/**
* MVCReturn
* @author peng.xia
*
*/
@Controller
@RequestMapping("/MVCReturn")
public class SpringMVCReturnController {
@RequestMapping(value="/index1",method=RequestMethod.GET)
public ModelAndView index(){
ModelAndView modelAndView = new ModelAndView("/user/index");
modelAndView.addObject("name", "xxx");
return modelAndView;
}
//对于ModelAndView构造函数可以指定返回页面的名称,也可以通过setViewName方法来设置所需要跳转的页面;
@RequestMapping(value="/index2",method=RequestMethod.GET)
public ModelAndView index2(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name", "xxx");
modelAndView.setViewName("/user/index");
return modelAndView;
}
//返回的是一个包含模型和视图的ModelAndView对象;
/**
* Model一个模型对象,
* 主要包含spring封装好的model和modelMap,以及java.util.Map,
* 当没有视图返回的时候视图名称将由requestToViewNameTranslator决定;
* @return
*/
@RequestMapping(value="/index3",method=RequestMethod.GET)
public Map

