spring mvc 系列3 返回JSON以及文件上传

先来看下spring mvc提供返回为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>';
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值