SpringMvc(4)

1.Springmvc RESTful风格

2.Springmvc 异常处理

(2)Springmvc RESTful风格

1.什么是REST架构?

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

2.RESTful主要对以下两个方面进行了规范

  1.定位资源的URL风格,列如 http://localhost:8080/springmvc-05/uesr/1

  2.对资源的操作:采用HTTP协议规定的GET,POST,PUT,DELETE,提交方式来处理对资源的增删改查操作

3.什么是 RESTful 风格?

  就是符合REST约束风格和原则的应用程序或设计就是RESTful风格

  用于控制层根据请求的方式调用不同的方法

Demo: 模拟增删改查操作

步骤一.创建一个实体类

步骤二.在操作层的@RuquestMapping注解中加上value属性(也就是主键id)和method属性(请求方式)

package com.zhiyou100.ydb.controller;

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.ydb.bean.User;

@Controller
@RequestMapping("user")
public class UserController {//查询操作
    @RequestMapping(value="{uid}",method=RequestMethod.GET)//method表示该方法处理get请求
    public String FindById(@PathVariable("uid") int id) {//@PathVariable把uid的值赋给形参
        System.out.println(id);
        return "index";
    }
    
    @RequestMapping(method=RequestMethod.POST)
    public String addUser (User user) {//添加操作
        System.out.println(user);
        return "index";
    }
}

步骤三.修改和删除页面使用的是PUT 和DELETE 的提交方式,而浏览器目前不支持。可以使用Ajax 以及加入转化提交方式的过滤器解决,步骤如下

1.在web.xml 文件中配置转化提交方式的过滤器

<!-- method 过滤器 -->
      <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>

2.使用AJax了 操作层需要加入@ResponseBody //用于把Java对象转化为json对象

@RequestMapping(method=RequestMethod.PUT)
    @ResponseBody
    public String updateUser (User user) {//修改操作
        System.out.println(user);
        return "update";
    }

3.在需求页面编写Ajax(jar包不要忘记引入)以修改为例 :需要使用 _method 来指定真实的提交方式

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/springmvc-05/js/jquery-3.2.1.min.js"></script>
</head>
<body>

</body>
<script type="text/javascript">
$.ajax({
    url:"user",
    type:"post",
    data:{
        "_method":"put",
        "name":"zs",
        "age":"1",
        "password":"1234354435"
    },
    success:function(cs){
        alert(cs)
    }
})
</script>
</html>

4.如果使用前端模拟器:

5.删除案例

操作层

@RequestMapping(value="{id}",method=RequestMethod.DELETE)
    @ResponseBody
    public String Delete(@PathVariable int id) {//删除操作
        System.out.println(id+"delete");
        return "delete";
    }

网页层

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/springmvc-05/js/jquery-3.2.1.min.js"></script>
</head>
<body>

</body>
<script type="text/javascript">
$.ajax({
    url:"user/1",
    type:"post",
    data:{
        "_method":"delete",
    },
    success:function(cs){
        alert(cs)
    }
})
</script>
</html>

(2)Springmvc 异常处理

Springmvc 的异常处理有两种方式:局部异常处理  全局异常处理

局部异常处理 在处理异常的方法上加入@ExceptionHandler 表示当该类中发生异常时会由改方法来处理

package com.zhiyou100.ydb.controller;

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.ydb.bean.User;

@Controller
@RequestMapping("user")
public class UserController {//查询操作    
//    局部异常
    @RequestMapping(method=RequestMethod.GET)
    public String list() {
        if(true) {
        throw new RuntimeException("页面发生错误");
        }
        return "index";
    }
    @ExceptionHandler
    public ModelAndView error(Exception excption) {
        ModelAndView mv =new ModelAndView();
        mv.addObject("error", excption.getMessage());
        mv.setViewName("error");
        return mv;
    }
}

创建一个专门用于显示异常的页面 需要加入 isErrorPage="true"

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${error }
</body>
</html>

显示结果

 全局异常处理

1.创建用于手机异常的Controller

 在里面写好处理异常的方法

package com.zhiyou100.ydb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class ErrorController {
//全局处理异常    
    @ExceptionHandler
    public ModelAndView error(Exception excption) {
        ModelAndView mv =new ModelAndView();
        mv.addObject("error", excption.getMessage());
        mv.setViewName("error");
        return mv;
    }
}

2.为了验证是全局有效的,我选择在创建一个操作类 StudentController 在里面写好会发生异常的方法

package com.zhiyou100.ydb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class StudentController {
    @RequestMapping(method=RequestMethod.GET)
    public String list() {
        int i = 10/0;//相当于抛出异常,然后就会进入到全局异常,最后进入到异常专属界面
        return "index";
    }
}

3.显示结果

原文地址:https://www.cnblogs.com/Kuriyama-Mirai/p/11469628.html