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

联合建设官方网站品牌代理加盟网

联合建设官方网站,品牌代理加盟网,百度收录提交网站后多久收录,做网站被骗了怎么办八、HttpMessageConverter HttpMessageConverter#xff0c;报文信息转换器#xff0c;将请求报文转换为Java对象#xff0c;或将Java对象转换为响应报文 HttpMessageConverter提供了两个注解和两个类型#xff1a;RequestBody#xff0c;ResponseBody#xff0c;Reque…八、HttpMessageConverter HttpMessageConverter报文信息转换器将请求报文转换为Java对象或将Java对象转换为响应报文 HttpMessageConverter提供了两个注解和两个类型RequestBodyResponseBodyRequestEntity ResponseEntity 1、RequestBody RequestBody可以获取请求体需要在控制器方法设置一个形参使用RequestBody进行标识当前请求的请求体就会为当前注解所标识的形参赋值 form th:action{/testRequestBody} methodpost用户名input typetext nameusernamebr密码input typepassword namepasswordbrinput typesubmit /formRequestMapping(/testRequestBody) public String testRequestBody(RequestBody String requestBody){System.out.println(requestBody:requestBody);return success; }输出结果 requestBody:usernameadminpassword123456 2、RequestEntity RequestEntity封装请求报文的一种类型需要在控制器方法的形参中设置该类型的形参当前请求的请求报文就会赋值给该形参可以通过getHeaders()获取请求头信息通过getBody()获取请求体信息 RequestMapping(/testRequestEntity) public String testRequestEntity(RequestEntityString requestEntity){System.out.println(requestHeader:requestEntity.getHeaders());System.out.println(requestBody:requestEntity.getBody());return success; }输出结果 requestHeader:[host:“localhost:8080”, connection:“keep-alive”, content-length:“27”, cache-control:“max-age0”, sec-ch-ua:“” Not A;Brand;v“99”, “Chromium”;v“90”, “Google Chrome”;v“90”, sec-ch-ua-mobile:“?0”, upgrade-insecure-requests:“1”, origin:“http://localhost:8080”, user-agent:“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36”] requestBody:usernameadminpassword123 3、ResponseBody ResponseBody用于标识一个控制器方法可以将该方法的返回值直接作为响应报文的响应体响应到浏览器 RequestMapping(/testResponseBody) ResponseBody public String testResponseBody(){return success; }结果浏览器页面显示success 4、SpringMVC处理json ResponseBody处理json的步骤 a导入jackson的依赖 dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.12.1/version /dependencyb在SpringMVC的核心配置文件中开启mvc的注解驱动此时在HandlerAdaptor中会自动装配一个消息转换器MappingJackson2HttpMessageConverter可以将响应到浏览器的Java对象转换为Json格式的字符串 mvc:annotation-driven /c在处理器方法上使用ResponseBody注解进行标识 d将Java对象直接作为控制器方法的返回值返回就会自动转换为Json格式的字符串 RequestMapping(/testResponseUser) ResponseBody public User testResponseUser(){return new User(1001,admin,123456,23,男); }浏览器的页面中展示的结果 {“id”:1001,“username”:“admin”,“password”:“123456”,“age”:23,“sex”:“男”} 5、SpringMVC处理ajax a请求超链接 div idappa th:href{/testAjax} clicktestAjaxtestAjax/abr /divb通过vue和axios处理点击事件 script typetext/javascript th:src{/static/js/vue.js}/script script typetext/javascript th:src{/static/js/axios.min.js}/script script typetext/javascriptvar vue new Vue({el:#app,methods:{testAjax:function (event) {axios({method:post,url:event.target.href,params:{username:admin,password:123456}}).then(function (response) {alert(response.data);});event.preventDefault();}}}); /scriptc控制器方法 RequestMapping(/testAjax) ResponseBody public String testAjax(String username, String password){System.out.println(username:username,password:password);return hello,ajax; }6、RestController注解 RestController注解是springMVC提供的一个复合注解标识在控制器的类上就相当于为类添加了Controller注解并且为其中的每个方法添加了ResponseBody注解 7、ResponseEntity ResponseEntity用于控制器方法的返回值类型该控制器方法的返回值就是响应到浏览器的响应报文 九、文件上传和下载 1、文件下载 使用ResponseEntity实现下载文件的功能 RequestMapping(/testDown) public ResponseEntitybyte[] testResponseEntity(HttpSession session) throws IOException {//获取ServletContext对象ServletContext servletContext session.getServletContext();//获取服务器中文件的真实路径String realPath servletContext.getRealPath(/static/img/1.jpg);//创建输入流InputStream is new FileInputStream(realPath);//创建字节数组byte[] bytes new byte[is.available()];//将流读到字节数组中is.read(bytes);//创建HttpHeaders对象设置响应头信息MultiValueMapString, String headers new HttpHeaders();//设置要下载方式以及下载文件的名字headers.add(Content-Disposition, attachment;filename1.jpg);//设置响应状态码HttpStatus statusCode HttpStatus.OK;//创建ResponseEntity对象ResponseEntitybyte[] responseEntity new ResponseEntity(bytes, headers, statusCode);//关闭输入流is.close();return responseEntity; }2、文件上传 文件上传要求form表单的请求方式必须为post并且添加属性enctype“multipart/form-data” SpringMVC中将上传的文件封装到MultipartFile对象中通过此对象可以获取文件相关信息 上传步骤 a添加依赖 !-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -- dependencygroupIdcommons-fileupload/groupIdartifactIdcommons-fileupload/artifactIdversion1.3.1/version /dependencyb在SpringMVC的配置文件中添加配置 !--必须通过文件解析器的解析才能将文件转换为MultipartFile对象-- bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver/beanc控制器方法 RequestMapping(/testUp) public String testUp(MultipartFile photo, HttpSession session) throws IOException {//获取上传的文件的文件名String fileName photo.getOriginalFilename();//处理文件重名问题String hzName fileName.substring(fileName.lastIndexOf(.));fileName UUID.randomUUID().toString() hzName;//获取服务器中photo目录的路径ServletContext servletContext session.getServletContext();String photoPath servletContext.getRealPath(photo);File file new File(photoPath);if(!file.exists()){file.mkdir();}String finalPath photoPath File.separator fileName;//实现上传功能photo.transferTo(new File(finalPath));return success; }
http://wiki.neutronadmin.com/news/149304/

相关文章:

  • 网站开发及维护是什么网站logo设计免费版在线
  • 如何获取网站根目录链接济南网签查询系统
  • 秦皇岛做网站哪家好济南住宅与房地产信息网官方网站
  • 余姚做企业网站郑州东区做网站的公司
  • 网站备案 空间文案策划公司
  • 哈尔滨大型网站制作开发个人网站空间大小
  • 如何在vs做网站施工企业三金压降指的是哪三金
  • 舟山网站建设优化网站建设 九艾
  • 网站 域名安阳区号座机22开头哪的电话
  • 长春网站设计团队百度云群组
  • 做网站用什么开发好支付宝开放平台
  • 做网站要固定ipwordpress代码缓存
  • 网站开发 问题 关键技术wordpress模板不显示
  • 做网站百度新闻源游戏推广代理app
  • 北京中御建设公司网站网页装修设计
  • 深圳网站建设网站推广的方法专业seo网络营销公司
  • 秦皇岛建设局局官方网站西安软件开发培训机构
  • 如何建立一个手机网站网站备案要网站做才可以使用吗
  • 重庆市公司网站备案在哪了苏州seo排名公司
  • 建设通属于什么网站app制作商
  • 营销网站报备中英文微信网站建设
  • 网站建设专员工作职责宁波做网站建设推广
  • 长沙建一个网站多少钱上虞网站建设哪家好
  • 动态背景网站宁波超值关键词优化
  • 网站建设实训实训心得忻州 建网站
  • 应付网站软件服务怎么做分录成都网络推广公司
  • 做pc端网站资讯全屋整装家装
  • 门户网站的营销方式wordpress 重新生成缩略图
  • 自己的网站做防伪码做企业商城网站
  • 长沙麓谷网站建设苏州最好的网站建设