当前位置: 首页 > news >正文

SpringMVC格式总结

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

  1. SpringMVC几种数据接收方式

    1.参数绑定:

       a. 提交的数据中有一个对像 的情况   

  2. @RequestMapping(value = "/postSave")
        public String postSave(HttpServletRequest request,Post t) {

       b . 提交的数据只有一个参数的情况

  3. @RequestMapping(value = "/postPraise/{postId}")
    	public void postPraise(HttpServletRequest request, @PathVariable Long postId)

     c . 最后一种就是直接从request对象中获取

  4. String newPassword = request.getParameter("password");

二. 返回格式类型(理论上)

    1. 路径跳转

      a . 转发

return "home/post";
return "/home/post";
//备注;这个是没有区别的

      b.  重定向

return "redirect:/home/post";//项目路径后
return "redirect:home/post";//当前路径后
return "redirect:../home/post";//等同于当前路径
//备注:'/' 代表项目路径

   2 . 返回数据格式

      a.  普通的可以将数据放到request中

      b.  较为特殊的可以用json类型    

//返回一个字符串
@RequestMapping(value = "/uploadImage")
	@ResponseBody
	public Map<String, String> uploadImage(HttpServletRequest request) throws Exception {
		String imageSrc = "/picture/" + dateFormat.format(new Date()) + "/" + fileName;
		Map<String, String> modelMap=new HashMap<String,String>();
		modelMap.put("imageSrc", imageSrc);
		return modelMap;
	}
//返回一个对象
$.ajax({
	type:'post',
	url:'<%=dynamicDomain%>/advert/selectAdvertPosition/'+positionCode,
	dataType:'json',
	success:function(msg){
	 if(msg.result){
	    alert(msg.advertPosition.width);
	    alert(msg.advertPosition.height);
	    alert("ture");	
	   }
	}
 });
	
 @RequestMapping("/selectAdvertPosition/{positionCode}")
    public String selectAdvertPosition(HttpServletRequest request,@PathVariable String positionCode,
                       ModelMap modelMap) throws IOException{
                int width=22;
                int height=22;
		AdvertPosition ad= new AdvertPosition (width,height);
		modelMap.addAttribute("result", true);
		modelMap.addAttribute("advertPosition", ad);//返回一个对象
		return "jsonView";
    }

  ajax文件上传

 function ajaxFileUpload11() {  
	        if($("#uploadFile1").val()==''){
	            alert('请选择上传文件');
	            return false;
	        }
	        $.ajaxFileUpload({  
	            url: '${dynamicDomain}/screenshot/uploadImage?ajax=1',  
	            secureuri: false,  
	            fileElementId: 'uploadFile1',  //对应文件表单的id
	            dataType: 'json',  //服务器返回类型
	            success: function(json, status) {
	                if(json.result=='true'){
	                    var filePath = json.filePath;
	                }
	            },error: function (data, status, e)//服务器响应失败处理函数
	            {
	                alert(e);
	            }
	        }  
	    );
	        return false;  
	    } ;
	    
@RequestMapping("/uploadImage")
public String uploadImage(HttpServletRequest request, HttpServletResponse response,
                          ModelMap map) throws Exception {
	String result = "false";
	//从request流中获取文件
	UploadFile uploadFile = FileUpDownUtils.getUploadFile(request, "uploadFile");
	String fileName = uploadFile.getFileName();
	if (StringUtils.isNotBlank(fileName)){
	  if (fileName.endsWith(".jpg")
	  ||fileName.endsWith(".bmp")
	  ||fileName.endsWith(".gif")
	  ||fileName.endsWith(".png")
	  ||fileName.endsWith(".JPG")
	  ||fileName.endsWith(".BMP")
	  ||fileName.endsWith(".GIF")
	  ||fileName.endsWith(".PNG")) {
		byte[] fileData = FileUpDownUtils.getFileContent(uploadFile.getFile());
		String filePath = fileManager.saveImageFile(fileData, uploadFile.getFileName());
		BufferedImage image = ImageUtils.readImage(uploadFile.getFile().getAbsolutePath());
		result = "true";
		map.addAttribute("filePath", filePath.trim());
		if (image != null) {
			map.put("width", image.getWidth() + "");
			map.put("height", image.getHeight() + "");
		}
		} else {
			map.put("message", "图片格式必须为jpg/bmp/gif/png");
		}
		}
		map.put("result", result);
		return "jsonView";
	}   
	    
	    


转载于:https://my.oschina.net/craftsdream/blog/549098

相关文章:

  • QT内使用OpenCV
  • FRC与tableview的联合使用
  • Sublime Text 3安装与使用
  • 解放全天下开发者
  • Cesium 获取当前视图范围
  • Gradle介绍
  • 扩展SpringMVC以支持绑定JSON格式的请求参数
  • 0x05 Python数据分析,Anaconda八斩刀
  • dos保存adb logcat读取的Android信息
  • Rotate Image LeetCode
  • MFC消息映射与命令传递
  • CentOS6.6下解压安装mysql-5.7.10-linux-glibc2.5-i686.tar.gz
  • myeclipse安装插件phpeclipse后进行PHP代码编写
  • 怎样选择前端框架
  • 【遇到的问题】父div不能被撑开
  • 【技术性】Search知识
  • android 一些 utils
  • angular学习第一篇-----环境搭建
  • JavaScript DOM 10 - 滚动
  • Java反射-动态类加载和重新加载
  • Next.js之基础概念(二)
  • nginx 配置多 域名 + 多 https
  • Object.assign方法不能实现深复制
  • v-if和v-for连用出现的问题
  • WebSocket使用
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 高程读书笔记 第六章 面向对象程序设计
  • 互联网大裁员:Java程序员失工作,焉知不能进ali?
  • 技术发展面试
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 每天10道Java面试题,跟我走,offer有!
  • 前端面试之CSS3新特性
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 入手阿里云新服务器的部署NODE
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • 硬币翻转问题,区间操作
  • 最简单的无缝轮播
  • No resource identifier found for attribute,RxJava之zip操作符
  • ###C语言程序设计-----C语言学习(3)#
  • (6)STL算法之转换
  • (Matalb时序预测)WOA-BP鲸鱼算法优化BP神经网络的多维时序回归预测
  • (pojstep1.3.1)1017(构造法模拟)
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (二)正点原子I.MX6ULL u-boot移植
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (附源码)ssm学生管理系统 毕业设计 141543
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (四) Graphivz 颜色选择
  • (循环依赖问题)学习spring的第九天
  • (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
  • .net 简单实现MD5
  • .net 托管代码与非托管代码
  • .Net的C#语言取月份数值对应的MonthName值