SpringMVC study five (resultful style / exception handling / comment)

  • resultful style
  • Exception Handling

1.Restfule style

  Restfule style is a style of software architecture, rather than the standard, but provides a design principles and constraints. Mainly applied to the software client and server interaction. It is based on the http protocol. It aims to improve the scalability of the system, to reduce the coupling between the application framework to facilitate distributed processing program. Based on this style of software can be more simple, more structured and easier to implement caching mechanism.
  In resultful style, a user requests a URL url using the same manner and with the request: get / post / delete / put on the processing method, etc. to distinguish the request. This allows front-end developers backstage separate development in the former no confusion on the resources requested address to form a unified interface. 

Use difference: in the http protocol, four verb indicates the operation mode: GET / Post / put / Delete , they correspond to the four basic operations. Get used to obtain resources. post for the newly created resource can be updated resources. put to update the resource. Delete to delete the resource

The general format is as follows:

@RequestMapping(value="{id}",method=RequestMethod.GET)
@RequestMapping(value="{id}",method=RequestMethod.POST)
@RequestMapping(value="{id}",method=RequestMethod.DELETE)
@RequestMapping(value="{id}",method=RequestMethod.PUT)

Controller now using the following classes:

 1 @Controller
 2 @RequestMapping("users")
 3 public class UsersController {
 4     
 5     @RequestMapping(value="{uid}",method=RequestMethod.GET)//查询
 6     public String fingById(@PathVariable("uid") int id) {
 7         System.out.println("===findbyid===");
 8         int a=10/0;
 9         return "index"; 
10     }
11     
12     @RequestMapping(method=RequestMethod.POST)//添加
13     public String addUsers(Users users) {
14         System.out.println("===addUsers===");
15         System.out.println(users);
16         return "index"; 
17     }
18      
19     @RequestMapping(method=RequestMethod.PUT)//修改
20     @ResponseBody
21     public String updateUsers(Users users) { 
22         System.out.println("===updateUsers===");
23         System.out.println(users);
24         return "index"; 
25     }
26     
27     @RequestMapping(value="{id}", method=RequestMethod.DELETE)//删除
28     @ResponseBody 
29     public String delete(@PathVariable int id) {
30         System.out.println(id+"===delete===");
31         return "index"; 
32     }
33 }

After the completion of the preparation for testing, where we tested with a google plug-in, you can choose a very convenient way of submission, etc.

By testing both GET and POST, the display 200 status code (pass)

 

 

 For PUT and DELETE which we found in the test and can not be correct

Because the browser only the GET and POST, so we need to add attributes when testing _method = PUT / _method = DELETE, the corresponding need to add a filter in your profile [ HiddenHttpMethodFilter ] The role of the filter is to POST submit the _method = PUT / _method = DELETE by conversion to PUT / DELETE

1   <filter>
2       <filter-name>hiddenHttpMethodFilter</filter-name>
3       <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
4   </filter>
5   <filter-mapping>
6       <filter-name>hiddenHttpMethodFilter</filter-name>
7       <url-pattern>/*</url-pattern>
8   </filter-mapping>

至此测试通过如下

 

 2.异常处理

 在上面的controller类中大家可能发现有一个地方会出错,抛出异常即int a=10/0,我们现在进行处理;

1 @RequestMapping(value="{uid}",method=RequestMethod.GET)//查询
2 public String fingById(@PathVariable("uid") int id) {
3     System.out.println("===findbyid===");
4     int a=10/0;//抛出异常
5     return "index"; 
6 }

对于此异常有两种处理方式,实际算是一种吧,看个人理解

第一种:在本类中加入以下代码

//该类中发生异常时由该方法来处理
    @ExceptionHandler
    public ModelAndView error(Exception exception) {
        ModelAndView mv=new ModelAndView();
        mv.addObject("error", exception.getMessage());
        mv.setViewName("error");
        return mv;
    }

当该类中发生异常时由该方法来处理,对应的可以在前端页面进行接收这个错误信

第二种:新建一个类,用来接收获得的所有的异常,以此避免在每一个类中进行异常处理

 

 

 1 @ControllerAdvice
 2 public class ExceptionController {
 3     
 4     //该类中发生异常时由该类来处理
 5         @ExceptionHandler
 6         public ModelAndView error(Exception exception) {
 7             ModelAndView mv=new ModelAndView();
 8             mv.addObject("error", exception.getMessage());
 9             mv.setViewName("error");
10             return mv;
11         }
12 }

 前端页面代码,其中需加入isErrorPage="true"属性,意思是此页面为错误信息显示页面,我们再次接收错误信息,当处理了上面的异常后跳出到这里显示

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8" isErrorPage="true"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     ${error }
11 </body>
12 </html>

测试结果,成功显示异常信息:

 

 Tips(SpringMVC部分注解)

SpringMVC注解:

1@Controller:标注该类为控制层类

2.@RequestMapping:标注请求的地址

3.@ResponseBody:把java对象转化为json对象

4.@Valid:标注校验该数据

5.@PathVariable:接收uri地址的值赋值给参数

6.@SessionAttributes:保存到Session中 

7.@RequestParam:接收参数若参数名不同可用,当没传参数值时可设置默认值

8.@ExceptionAdivice:标注一个类为异常处理类

9.@EcceptionHandler:标注一个方法为异常处理方法

10.@InitBinder:时间参数处理格式

 

Guess you like

Origin www.cnblogs.com/murmansk/p/11469752.html