- 浏览: 61824 次
- 性别:
- 来自: 南宁
最新评论
-
diwuci:
用response.setContentType(" ...
spring mvc 系列3 返回JSON以及文件上传 -
dickluo:
得花时间好好看下代码了 多谢
spring mvc 系列1 环境配置 -
muqingren:
这个学习了,歇息
spring mvc 系列3 返回JSON以及文件上传 -
JetMah:
Jophy 写道
@RequestMapping(pa ...
spring mvc 系列3 返回JSON以及文件上传 -
LifeFree:
LZ 1.的实现,貌似有笔误。
应该类似:
param = ...
一个小细节现实后的蛋疼结果。
先来看下spring mvc提供返回为json数据的注解
返回头部content-type为:application/json
文件上传:
EXT from 如果提交时添加:fileUpload: true 也就是文件上传时,要求返回的响应头部content-type为:text/html,而@ResponseBody返回的响应头部为:application/html 这时浏览器会要求选择打开XX文件的选项,如要防止此种情况产生,必须修改返回类型为:text/html 用@ResponseBody 本人还没找到解决方法,如有高人,请亮下方法,呵呵。
富文本编辑器本人用的是FCK。配置见 web.xml
根目录:WebRoot/fckeditor,
在EXT中,用的是html方式引入:
var htmleditorstr = '<div>' +
'<input type="hidden" id="giDescription___Config" value="" style="display:none" />' +
'<iframe name="giDescription___Frame" id="giDescription___Frame" ' +
'src="../fckeditor/editor/fckeditor.html?InstanceName=giDescription" ' +
'width="100%" height="400px″ frameborder="0″ scrolling="no"></iframe></div>';
没必要那么麻烦,直接用
那个示例就是解决方法啊。
非常感谢 Jophy 童鞋, 问题解决了。
据说在 json 转换配置文件里面添加一些接受的返回类型就可以了,具体没有深究,改天有空先,呵呵。
/** * * @功能模块: add * @方法说明: 添加一个对象 * @version: 1.0 * @param goodsType * GoodsTypeModel * @return Object spring 自动转换成 json 数据 * @throws */ @RequestMapping(params = "method=add") @ResponseBody public Object add(@ModelAttribute("goodsType") GoodsTypeModel goodsType) { return this.goodsTypeService.add(goodsType);// 返回的对象为:GoodsTypeModel }
返回头部content-type为:application/json
文件上传:
/** * * @功能模块: add * @方法说明: 处理 method=add 请求, 添加商品信息 * @version: 1.0 * @param goodsInfo * 商品信息对象,由spring自动获取 * @param model * @return ModelMap 由spring自动生成json数据 * @throws IOException * @throws */ @RequestMapping(params = "method=add") //@ResponseBody public void add(@ModelAttribute("goodsInfo") GoodsInfoModel goodsInfo, HttpServletResponse response) throws IOException { if (!goodsInfo.getMpFile().isEmpty()) { goodsInfo.setGiImg(FileUploadUtil.saveFileUpload(goodsInfo.getMpFile(), GoodsInfoModel.DEFAULT_FILE_UPLOAD_DIR)); } this.goodsInfoService.add(goodsInfo); //return this.goodsInfoService.add(goodsInfo); // EXT 文件上传时,要返回的头部信息类型为 text/html, // 而用@ResponseBody返回的头部信息为application/json所以自动转换类型 // 由于本人能力有限,不知道如果要用@ResponseBody的时候怎么转换,呵呵 JsonUtil.printJSON(response, new Result()); } /* * 多文件上传时,请使用:DefaultMultipartHttpServletRequest public String add(@ModelAttribute("goodsInfo") GoodsInfoModel goodsInfo, DefaultMultipartHttpServletRequest multipartRequest){ List<MultipartFile> mpFiles = multipartRequest.getFiles("mpFile"); String fileNames = null; for(MultipartFile mpFile : mpFiles){ if( ! mpFile.isEmpty()){ if(fileNames!=null){ fileNames += "," + FileUploadUtil.saveFileUpload(mpFile, GoodsInfoModel.DEFAULT_FILE_UPLOAD_DIR); }else{ fileNames = FileUploadUtil.saveFileUpload(mpFile, GoodsInfoModel.DEFAULT_FILE_UPLOAD_DIR); } } } goodsInfo.setGiImg(fileNames); this.goodsInfoService.add(goodsInfo); return "redirect:/Product.do?method=list"; }*/
EXT from 如果提交时添加:fileUpload: true 也就是文件上传时,要求返回的响应头部content-type为:text/html,而@ResponseBody返回的响应头部为:application/html 这时浏览器会要求选择打开XX文件的选项,如要防止此种情况产生,必须修改返回类型为:text/html 用@ResponseBody 本人还没找到解决方法,如有高人,请亮下方法,呵呵。
富文本编辑器本人用的是FCK。配置见 web.xml
根目录:WebRoot/fckeditor,
在EXT中,用的是html方式引入:
var htmleditorstr = '<div>' +
'<input type="hidden" id="giDescription___Config" value="" style="display:none" />' +
'<iframe name="giDescription___Frame" id="giDescription___Frame" ' +
'src="../fckeditor/editor/fckeditor.html?InstanceName=giDescription" ' +
'width="100%" height="400px″ frameborder="0″ scrolling="no"></iframe></div>';
- TestSpringMVC.part1.rar (8 MB)
- 下载次数: 1614
- TestSpringMVC.part2.rar (5.4 MB)
- 下载次数: 729
评论
13 楼
diwuci
2017-03-14
用response.setContentType("text/html");是可以解决的
另外一种方式继续使用@ResponseBody 也可以,在@RequestMapping后面加上produces = "text/html;charset=utf-8"解决
@RequestMapping(value="add",method=RequestMethod.POST, produces = "text/html;charset=utf-8")
@ResponseBody
public String add()
另外一种方式继续使用@ResponseBody 也可以,在@RequestMapping后面加上produces = "text/html;charset=utf-8"解决
@RequestMapping(value="add",method=RequestMethod.POST, produces = "text/html;charset=utf-8")
@ResponseBody
public String add()
12 楼
muqingren
2011-03-16
这个学习了,歇息
11 楼
JetMah
2011-03-07
Jophy 写道
@RequestMapping(params = "method=login") public ResponseEntity<String> doFirst(@RequestParam("u") String u,@RequestParam("p") String p,HttpSession session,ModelMap model){ HttpHeaders headers = new HttpHeaders(); MediaType mt=new MediaType("text","html",Charset.forName("gbk")); headers.setContentType(mt); ResponseEntity<String> re=null; String return = new String("test"); re=new ResponseEntity<String>(return,headers, HttpStatus.OK); return re; }这样可以自定义 返回的响应头部content-type
没必要那么麻烦,直接用
@RequestMapping(params = "method=add") @ResponseBody public String add(@ModelAttribute("goodsInfo") GoodsInfoModel goodsInfo, HttpServletResponse response) throws IOException { // ... return "{success:true}"; }
10 楼
joknm
2010-12-30
看回复呀,回复里面不是有解决方案吗?
9 楼
jiajiafucs
2010-12-29
哈哈,解决了,
public void importData(@RequestParam("file") MultipartFile file,HttpServletResponse response,ModelMap modelMap){
int num=0;
try{
if(!file.isEmpty()){
// 获得inputstream
InputStream inputstream = file.getInputStream() ;
num = getXlsData(inputstream);
inputstream.close();
file=null;
}
String msg = "没有数据";
if(num>0){
msg = "导入成功";
}
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("{success:true,msg:'"+msg+"'}");
}catch(Exception e){
e.printStackTrace();
}
}
public void importData(@RequestParam("file") MultipartFile file,HttpServletResponse response,ModelMap modelMap){
int num=0;
try{
if(!file.isEmpty()){
// 获得inputstream
InputStream inputstream = file.getInputStream() ;
num = getXlsData(inputstream);
inputstream.close();
file=null;
}
String msg = "没有数据";
if(num>0){
msg = "导入成功";
}
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("{success:true,msg:'"+msg+"'}");
}catch(Exception e){
e.printStackTrace();
}
}
8 楼
joknm
2010-10-22
lshoo 写道
楼上怎么解决的,在大家分享一下!我也要弄这个。
那个示例就是解决方法啊。
7 楼
lshoo
2010-10-20
楼上怎么解决的,在大家分享一下!我也要弄这个。
6 楼
joknm
2010-10-13
Jophy 写道
@RequestMapping(params = "method=login") public ResponseEntity<String> doFirst(@RequestParam("u") String u,@RequestParam("p") String p,HttpSession session,ModelMap model){ HttpHeaders headers = new HttpHeaders(); MediaType mt=new MediaType("text","html",Charset.forName("gbk")); headers.setContentType(mt); ResponseEntity<String> re=null; String returnString = new String("test"); re=new ResponseEntity<String>(returnString, headers, HttpStatus.OK); return re; }这样可以自定义 返回的响应头部content-type
非常感谢 Jophy 童鞋, 问题解决了。
5 楼
Jophy
2010-10-13
@RequestMapping(params = "method=login") public ResponseEntity<String> doFirst(@RequestParam("u") String u,@RequestParam("p") String p,HttpSession session,ModelMap model){ HttpHeaders headers = new HttpHeaders(); MediaType mt=new MediaType("text","html",Charset.forName("gbk")); headers.setContentType(mt); ResponseEntity<String> re=null; String return = new String("test"); re=new ResponseEntity<String>(return,headers, HttpStatus.OK); return re; }这样可以自定义 返回的响应头部content-type
4 楼
niyong
2010-10-13
中文容易出现乱码
3 楼
eyeieye
2010-10-12
看 AnnotationMethodHandlerAdapter messageConverters
2 楼
joknm
2010-10-12
oakeye 写道
这问题我也遇到 不知道怎么解决
他会在生成的json成功信息的前后加上<pre></pre>
他会在生成的json成功信息的前后加上<pre></pre>
据说在 json 转换配置文件里面添加一些接受的返回类型就可以了,具体没有深究,改天有空先,呵呵。
1 楼
oakeye
2010-10-12
这问题我也遇到 不知道怎么解决
他会在生成的json成功信息的前后加上<pre></pre>
他会在生成的json成功信息的前后加上<pre></pre>
发表评论
-
一个小细节现实后的蛋疼结果。
2011-03-07 08:37 1146情况是这样子的,比如你用单击按钮在单数次单击里面要求某个数为0 ... -
eclipse3.6 集成 myeclipse 插件
2010-11-16 14:06 4667最近很是邪恶,不想费钱,但又不想邪恶的去找 myeclipse ... -
spring mvc 系列2 泛型DAO(基于存储过程)
2010-10-06 14:05 5440spring mvc 系列1 中: 感谢 ricoyu 提示 ... -
spring mvc 系列1 环境配置
2010-10-02 18:59 1948废话: 热门?=spring + hibernate + st ... -
finally 浅谈
2010-07-29 10:01 2347近日做程序时突发奇想:try ... finally ... ... -
大量数据访问时,用JPA 对象映射关系会出错?
2009-12-30 09:23 2758昨天在做一JPA调试时出了些莫名其妙的错。 public ... -
单线程处理多任务处理方式
2009-12-27 16:14 4231缘由: 在实际的应用中可能会出现这样一种尴尬:只有一个 ... -
命令模式,解释命令
2009-12-26 21:52 1049用命令模式解释命令 ... -
Hibernate+jpa+spring
2009-12-23 17:34 1316今天搞了一个JPA+HIBERNATE,以下是资料,全英文 h ... -
需要但没有session等会话信息下的程序开发
2009-10-19 09:52 1161本人最近在做个小实现: 需求:类似于聊天室, ... -
jpa 多个对象映射同时加载
2009-10-13 23:18 1342jpa 多个对象映射同时加载 FetchType可设置为EA ... -
利用RMI实现JAVA分布式应用
2009-10-10 15:53 987Java RMI (Remote Method Invocat ... -
一道SQL面试题
2009-09-22 17:28 1078有两个表, table1, table2, Table tab ... -
java 取随机字符串
2009-09-16 13:51 4625package org.jinsheng.util; ... -
junit4单元测试的注释使用说明
2009-08-24 23:16 4759junit4单元测试的使用说明 右键项目->prope ... -
spring2.5注释型 + jpa + 数据库池的配置(4)
2009-08-10 00:57 1293PersonDao.java package org.ji ... -
spring2.5注释型 + jpa + 数据库池的配置(3)
2009-08-10 00:45 1071Person.java(实体文件) package org ... -
spring2.5注释型 + jpa + 数据库池的配置(2)
2009-08-10 00:41 1233文件配置: applicationContext.xml ... -
spring2.5注释型 + jpa + 数据库池的配置(1)
2009-08-10 00:28 1791本人用的环境是:eclipse 3.0 + jdk 6.0_1 ...
相关推荐
spring mvc生成xml或json
NULL 博文链接:https://coocaobo.iteye.com/blog/2410629
工程文件 博文链接:https://starscream.iteye.com/blog/1067606
该项目在以前的基础上整合了json的使用
spring mvc拦截器,过滤json数据中的null值,将null变成空字符串,内含截图,及jar包
Ajax With Spring 3. Eclipse 工程,包含Web所需要的 所有jar包。 1> ajax 请求。 2> spring 3注解使用 3> mvc:annotation
总有4个包 jackson-core-asl-1.9.13.jar jackson-core-lgpl-1.9.13 jackson-mapper-asl-1.9.13 jackson-mapper-lgpl-1.9.13
在本示例中,我们将向您展示如何将对象转换成json格式并通过spring mvc框架返回给用户。 使用技术及环境: Spring 3.2.2.RELEASE Jackson 1.9.10 JDK 1.6 Eclipse 3.6 Maven 3 PS:在spring 3 中,要输出json...
最近在整spring mvc 返回json的配置,网上搜集整理了一下,觉得蛮有用的就共享出来,三个必备json包并附spring mvc 返回json的配置,此包有两个版本json包,一个2.6一个2.2
Spring MVC返回JSON数据 用到的jar包,主要是jackson-all-1.9.0.jar,jackson-all-1.9.9.jar,jackson-all-1.9.11.jar三个包
spring mvc 常用开发框架 jar包 提高开发效率 降低开发难度
这是结合博客的源码,链接是:http://blog.csdn.net/u012660464/article/details/53463708 。名为:使用Spring MVC生成Json数据,我们后台开发生成了Json数据后可以供前端进行异步调用。
1.创建第一个 Spring MVC 程序案例 2.Spring MVC @RequestMapping 注解案例 ...12.Spring MVC 实现 JSON 数据返回案例 13.Spring MVC 文件的上传与下载案例 14.Spring MVC 拦截器案例 15.Spring MVC 异常处理案例
Spring MVC--8.JSON、国际化、文件上传
最新spring3 mvc restful urlrewrite jquery json freemarker整合架构jar图片,欢迎下载: 说明比较少,群号:24172014,不懂来问,已懂讨论
spring mvc 返回json 接口 完整demo
本篇文章主要介绍了Spring MVC3返回JSON数据中文乱码问题解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring4 mvc 配置json 所需要的依赖jar包很全
NULL 博文链接:https://gaojiewyh.iteye.com/blog/1754619