一、控制器


 


1、控制器:负责提供访问应用程序的行为,通常通过接口定义或注解定义两种方式实现,负责解析用户的请求并将其转换为一个模型


 


2、实现方式


//实现该接口的类获得控制器功能
public interface Controller {
   //处理请求且返回一个模型与视图对象
   ModelAndView handleRequest(HttpServletRequest var1, HttpServletResponsevar2) throws Exception;
}

//定义控制器

//注意点:不要导错包,实现Controller接口,重写方法;
public class ControllerTest1 implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws Exception {
       //返回一个模型视图对象
       ModelAndView mv = new ModelAndView();
       mv.addObject("msg","Test1Controller");
       mv.setViewName("test");
       return mv;
  }
}
<bean name="/t1" class="com.jason.controller.ControllerTest1"/>

(2)使用注解@Controller


<!-- 自动扫描指定的包,下面所有注解类交给IOC容器管理 -->
<context:component-scan base-package="com.jason.controller"/>
//@Controller注解的类会自动添加到Spring上下文中

@Controller
public class ControllerTest2{
    //映射访问路径
    @RequestMapping("/t2")
   public String index(Model model){
       //Spring MVC会自动实例化一个Model对象用于向视图中传值
        model.addAttribute("msg", "ControllerTest2");
       //返回视图位置
        return "test";
  }
}

3、注解@RequestMapping


@Controller
public class TestController {
   @RequestMapping("/h1")
   public String test(){
       return "test";
  }
}

@Controller
@RequestMapping("/admin")
public class TestController {
   @RequestMapping("/h1")
   public String test(){
       return "test";
  }
}

二、RestFul风格


 


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


 


2、功能对比


(1)传统方式操作资源  :通过不同的参数来实现不同的效果!方法单一,post 和 get


http://127.0.0.1/item/queryItem.action?id=1 查询,GET

http://127.0.0.1/item/saveItem.action 新增,POST

http://127.0.0.1/item/updateItem.action 更新,POST

http://127.0.0.1/item/deleteItem.action?id=1 删除,GET或POST


 


(2)使用RESTful操作资源 :可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能可以不同!


http://127.0.0.1/item/1 查询,GET

http://127.0.0.1/item 新增,POST

http://127.0.0.1/item 更新,PUT

http://127.0.0.1/item/1 删除,DELETE


 


3、具体实例


@Controller
public class RestFulController {
  @RequestMapping("/add/{a}/{b}")
  public String restFul(@PathVariable int a,@PathVariable int b, Model model){
    int res = a + b;
    model.addAttribute("name", "结果为" + res);
    return "hello";
  }
}

 




RestController 起名_RestFul风格


 


4、思考:使用路径变量的好处是什么?

  • 使路径变得更加简洁;
  • 获得参数更加方便,框架会自动进行类型转换。
  • 通过路径变量的类型可以约束访问参数,如果类型不一样,则访问不到对应的请求方法,如这里访问是的路径是/add/1/a,则路径与方法不匹配,而不会是参数转换失败


RestController 起名_RestFul风格_02


 


5、修改参数类型会如何呢?


@Controller
public class RestFulController {
  @RequestMapping("/add/{a}/{b}")
  public String restFul(@PathVariable int a,@PathVariable String b, Model model){
    String res = a + b;
    model.addAttribute("name", "结果为" + res);
    return "hello";
  }
}

 



RestController 起名_springmvc控制器_03


 


6、使用method属性指定请求类型,用于约束请求的类型,可以收窄请求范围。指定请求谓词的类型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等


@Controller
public class MethodController {
  @RequestMapping(value = "/test",method = {RequestMethod.POST})
  public String test(Model model){
    model.addAttribute("msg", "Jason");
    return "test";
  }
}

 



RestController 起名_控制器_04


 


@Controller
public class MethodController {
  @RequestMapping(value = "/test",method = {RequestMethod.GET})
  public String test(Model model){
    model.addAttribute("msg", "Jason");
    return "test";
  }
}

 



RestController 起名_RestController 起名_05