• springMVC(九)——restful风格和异常处理


    restful风格

    REST架构是一个抽象的概念,目前主要是基于HTTP协议实现,其目的是为了提高系统的可伸缩性,降低应用之间的耦合度,便于框架分布式处理程序。

    Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

    它改变了URL的地址风格

    举个例子:以前的URL为:http://localhost:8080/Demo/list/login?id=1;

    而使用restful风格后的地址变为:http://localhost:8080/Demo/list/1;

    它使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。分别对应 添加、 删除、修改、查询。

    package com.zhiyou100.zjc.controller;
    
    import javax.validation.Valid;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.zhiyou100.zjc.bean.User;
    
    @Controller
    @RequestMapping("user")
    public class UserController {
        //method:表示该方法处理GET请求
        //uid:接受参数
        @RequestMapping(value="{uid}",method = RequestMethod.GET)
        public String findById(@PathVariable("uid") int id) {//把uid赋值给id
            System.out.println(id);
            return "index";
        }
        @RequestMapping(value = "{uid}",method = RequestMethod.POST)
        public String insertUser(@PathVariable("uid" ) int id,User user) {
            System.out.println(user);
            return "index";
        }
        //springmvc提供了一个人过滤,该过滤器可以把post请求转化为put和delete请求
        @RequestMapping(method = RequestMethod.PUT)
        @ResponseBody
        public String update(User user) {
            System.out.println(user+"--------update");
            return "index";
        }
        @RequestMapping(value="{id}",method = RequestMethod.DELETE)
        @ResponseBody
        public String delete(@PathVariable int id) {
            
            System.out.println(id+"---delete");
            return "index";
        }    
    }

    前端代码示例:

     注意:黄色部分,要设置 "_method=DELETE"(这只是删除,修改的话吧delete改成put就行)

    $.messager.confirm('确认','确定删除ID为 '+ids+' 的会员吗?',function(r){
                if (r){
                    $.ajax({
                        type:"POST",
                        url:"/user",
                        data:{'ids':ids,'_method':'DELETE'},
                        statusCode: {
                            200:function(){
                                $.messager.alert('提示','删除成功!');
                                $("#userList").datagrid("reload");
                                clearForm();
                            },
                            500:function(){
                                $.messager.alert('提示','删除失败!');
                            }
                        }
                        
                    })
                }
            });

    在使用put和delete方法时候,要在web.xml中配置过滤器

        <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

    Springmvc如何进行异常处理

    异常处理分为局部异常处理和全局异常处理

    局部异常处理:

    @ExceptionHandler//局限于当前的类中
        public ModelAndView error(Exception exception) {
            ModelAndView mav = new ModelAndView();
            mav.setViewName("error");
            mav.addObject("error", exception.getMessage());
            return mav;
        }

     全局异常处理,是定义一个全局异常类

    package com.zhiyou100.zjc.controller;
    
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.servlet.ModelAndView;
    
    @ControllerAdvice
    public class ExceptionController {
    
        @ExceptionHandler
        public ModelAndView error(Exception exception) {
            ModelAndView mav = new ModelAndView();
            mav.setViewName("error");
            mav.addObject("error", exception.getMessage());
            return mav;
        }
    }
  • 相关阅读:
    nhibernate记事
    关于mysql数据库中,连续签到等连续日期的处理
    记IIS部署https arr urlrewrite
    mybatis Generator
    CGI
    FreedonStudio freertos 内存分配方式选择
    NB模块BC26调试记录
    调试升级程序记录
    最近做的一个嵌入式项目
    4*4键盘扫描程序--去抖加长按
  • 原文地址:https://www.cnblogs.com/zjc364259451/p/11469630.html
Copyright © 2020-2023  润新知