Home  >  Article  >  Database  >  ExtJS3.2+SpringMVC4.0 Form提交后返回json始终是failure,而且

ExtJS3.2+SpringMVC4.0 Form提交后返回json始终是failure,而且

WBOY
WBOYOriginal
2016-06-07 15:37:571232browse

经过一段折腾,总算把问题解决了,下面将解决的过程记录下来,希望能给其他人提供些帮助: 返回JSON式的数据,需要在ApplicationContext.xml中配置,当然网上有两种方法,我用了第一种,即阿里巴巴的fastjson,之前下载的是低版本的,运行时发现有错,因为缺

经过一段折腾,总算把问题解决了,下面将解决的过程记录下来,希望能给其他人提供些帮助:

返回JSON格式的数据,需要在ApplicationContext.xml中配置,当然网上有两种方法,我用了第一种,即阿里巴巴的fastjson,之前下载的是低版本的,运行时发现有错,因为缺少对spring的支持,最终下载了1.1.40版本(当然版本越高肯定也支持),将下载的fastjson-1.1.40.jar包加入到buildpath中。下面就要配置applicationcontext.xml了,配置代码如下:


       
           
           
               
                   
                       
                        text/html;charset=UTF-8
                        application/json;charset=UTF-8
                   

               

           

       

   

在SpringMVC的控制层的代码如下:

@RequestMapping("/upload")  //这是定义了路由,和本文讲的没什么关系
 @ResponseBody //因为我不想返回视图,而是返回一个json数据,那么这边必须要用RespnseBody来注解

Object类型,并且通过Map的方式将success和message添加进去,这样到前台就会根据success的值来判断调用是否成功执行了


 public Object doUpload(@RequestParam(value = "F_FileType", required = false) String sType,HttpServletRequest request,HttpServletResponse response) throws IOException{

/*下面这一段是文件上传的代码*/
  MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request; 
  MultipartFile file = fileRequest.getFile("F_PicPath");  
  String path = request.getSession().getServletContext().getRealPath("upload")+"\\"+sType+"\\";
  String fileName = file.getOriginalFilename();
  //response.setContentType("application/json; charset=UTF-8");
  File targetFile = new File(path, fileName);
  if (!targetFile.exists()){
   targetFile.mkdirs();
  }
  try{
   file.transferTo(targetFile);
  }catch(Exception e){
   e.printStackTrace();
  }
  Map map = new HashMap();
  map.put("success", false);
  map.put("msg", "失败");
  return map;
 }





Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn