Spring MVC

您所在的位置:网站首页 springmvc接收json Spring MVC

Spring MVC

#Spring MVC| 来源: 网络整理| 查看: 265

1.上传图片

       springmvc中对多部件(multipart)类型解析,在页面from中提交enctype="multipart/form-data" 的数据时,需要springmvc对multipart类型的数据进行解析;      1.1 在springmvc.xml中配置解析器    org.springframework.web.multipart.commons.CommonsMultipartResolver       1.2 模拟:在tomcat上配置虚拟目录来进行上传图片     注意:在图片虚拟目录中,一定将图片目录分级创建(提高i/o性能),一般按照日期创建;   上传思路:                      1)存储的物理路径                      2)拿到原始名称 getOriginalFileName()                      3)新的图片名称 UUID.randomUUID() + 原始                      4)新图片 new File(物理+新名称)                      5)tranTofer(file)

     1.3 tomcat建立虚拟文件目录

           (1)方法1:图形化界面操作

  

Spring MVC - 上傳圖片, JSON数据交互 ,RESTful 支持_mybatis

  

Spring MVC - 上傳圖片, JSON数据交互 ,RESTful 支持_上传图片_02

             图片访问路径就为 示例:

http://localhost:8989/images/3e896d12-ff02-4777-aa6e-90b2b31d3359.png

  

            (2)方法2 :Tomact配置文件配置

                          tomcat / conf /server.xml 中 的 host标签中添加 :

                       

      即 :docBase为物理路径 , path为虚拟路径 ;

    1.3 SpringMvc.xml 中 配置解析器 

         解析器: org.springframework.web.multipart.commons.CommonsMultipartResolver

   1.4 controller实现示例 

@RequestMapping("/uploadImage") public String uploadImage(FClient fc,MultipartFile mFile) throws Exception{ if(mFile!=null){ //物理路径 String lujin="D:\\yuan\\image\\"; //文件名称 String oldImagename=mFile.getOriginalFilename(); //新的文件名称 String newImagename=UUID.randomUUID()+oldImagename.substring(oldImagename.lastIndexOf('.')); File file = new File(lujin+newImagename); //保存文件 mFile.transferTo(file); //图片名称 System.out.println(newImagename); } System.out.println(fc.getUsername()); return "/success.jsp"; }

    1.5 JSp页面实现

 

客户姓名: 上传图片:

2.Json数据交互

   2.1  为什么进行JSON数据交互json数据在js中,手机客户端中,webservice中使用流行且简单;

   2.2 所需jar 

   2.3 两个注解

       @RequestBody:将json串转成java对象 ;

       @ResponseBody:将java对象转成json串;

   2.4 Json配置实现

       (1) 注解驱动的方式不需要配置,即

       (2)  注解适配器的方式,需要配置json配置

              

//使用注解器的配置json使用

  2.5 请求的是json,输出的是json,要求请求的是json串,所以在前端页面中需要将请求的内容转为json 实现 

       (1)controller实现

@RequestMapping("/requestJson") public @ResponseBody FClient requestJson(@RequestBody FClient client){ client.setUsername("我去"); return client; }

      (2)ajax进行请求

function requestJson(){ $.ajax({ type: 'post', url:'/SpringMvcMybatis1Demo/requestJson.action', contentType:'application/json;', data:'{"id":"102","username":"yuan"}', success:function(data){ console.log(data); } }); }

   2.6 请求key-value ,输出的json, 比较常用 ;

       (1)controller实现

@RequestMapping("/requestJsonKV") public @ResponseBody FClient requestJsonKV( FClient client){ client.setUsername("我去"); return client; }

      (2)ajax实现

function requestJson(){ $.ajax({ type: 'post', url:'/SpringMvcMybatis1Demo/requestJsonKV.action', data:'id=102&username=yuan', success:function(data){ console.log(data); } }); }

  2.7 整个json Controller实现

   

@Controller public class JsonController { @Autowired private FClientService fs; @RequestMapping("/requestJson") public @ResponseBody FClient requestJson(@RequestBody FClient client){ client.setUsername("我去"); return client; } @RequestMapping("/requestJsonKV") public @ResponseBody FClient requestJsonKV( FClient client){ client.setUsername("我去"); return client; } @RequestMapping("/requestListJsons") public @ResponseBody List getClients() throws Exception{ return fs.findClientList(); } @RequestMapping("/findClientsbyid/{id}") public @ResponseBody FClient findClientsbyid(@PathVariable("id") Integer id) throws Exception{ return fs.findClientById(id); } }

    html实现 :

json function requestJson(){ $.ajax({ type: 'post', url:'/SpringMvcMybatis1Demo/requestJson.action', contentType:'application/json;charset=utf-8', data:'{"id":"102","username":"yuan"}', success:function(data){ console.log(data); } }); } function requestJson1(){ $.ajax({ type: 'post', url:'/SpringMvcMybatis1Demo/requestJsonKV.action', data:'id=102&username=yuan', success:function(data){ console.log(data); } }); } $(document).ready(function(){ console.log("我家在了"); });

3.RESTful实现

    3.1 什么是restful?

        restful 是一种开发的理念,是对http的很好的诠释,全称Representational State Transfer :表现层的状态转换;

     3.2 作用1 :对Url进行规范,写成RESTful格式的url

      即:参数后以斜杠传入参数

http://localhost:8989/SpringMvcMybatis1Demo/findClientsbyid/1

     3.3 实现

          (1)配置RESTful的前端控制器 

                  可以和 .action 共存,但servlet-name不可以一样;

springmvc_rest org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring/springmvc.xml springmvc_rest /

        (2) URL模板映射 :将URL中的模板变量映射到指定名称中;使用 @PathVariable()

 

@RequestMapping("/findClientsbyid/{id}") public @ResponseBody FClient findClientsbyid(@PathVariable("id") Integer id) throws Exception{ return fs.findClientById(id); }

      (3)一个问题:RESTful模式需要对静态资源的加载,配置前端控制器的url-partten中指定的 / ,对静态资源文件的访问有问题;

  解决: 静态资源的解析,静态文件有 :js, img,html 等; 

     例如:对html的指定访问



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3