**
-
1.Spring:大杂烩
我们可以将SpringMVC中所有要用到的bean,注册到Spring中**
-
@Component 组件
-
@Service Service
-
@COntroller controller
-
@Repository dao
使用Model传送数据比使用ModelAndView更方便一点
Model:
//代表这个类会被Spring接管,被这个注解的类中的所有方法,如果返回值是String,并且有具体页面可以跳转,那么就会被视图解析器解析
@Controller
public class ControllerTest2 {
@RequestMapping("/t2")
public String test1(Model model){
//model就是传数据专用的
model.addAttribute("msg","ControllerTest2");
return "hello"; //会返回给WEB-INF/jsp/hello.jsp
}
}
ModelAndView:
public class ControllerTets1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//返回一个模型视图对象
ModelAndView mv = new ModelAndView();
//添加数据
mv.addObject("msg","Hello Controller");
//设置视图名字(跳转到哪里)
mv.setViewName("hello");
return mv;
}
}
- 2.RestFul风格讲解
REST(英文:Representational State Transfer,简称REST,意思:表述性状态转换,描述了一个架构样式的网络系统,比如web应用)。
它是一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件,它主要用于客户端和服务端交互类的软件。基于这个风格设计的软件可以更简介,更有层次,更易于实现缓存等机制。
// 原来的方式: http://localhost:8080/springmvc_05_controller_war_exploded/add?a=1&b=1
@RequestMapping("/add1")
public String test1(int a , int b, Model model){
int res = a + b;
model.addAttribute("msg","结果为"+res);
return "hello";
}
//RestFul方式: http://localhost:8080/springmvc_05_controller_war_exploded/add/a/b
@RequestMapping("/add2/{a}/{b}")
public String test2(@PathVariable int a , @PathVariable int b, Model model){
int res = a + b;
model.addAttribute("msg","结果为"+res);
return "hello";
}
//http:..../add2/1/3
@GetMapping("add1/{a}/{b}")