@RequestMapping说明
@RequestMapping
@Controller
public class TestController {
@RequestMapping("/h1")
public String test(){
return "test";
}
}
访问路径:http:// localhost:8080 / 项目名 / h1
- 同时注解类与方法
@Controller
@RequestMapping("/admin")
public class TestController {
@RequestMapping("/h1")
public String test(){
return "test";
}
}
访问路径:http:// localhost:8080 / 项目名 / admin /h1,需要先指定类的路径再指定方法的路径;
RestFul风格
概念:
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
功能:
传统方式操作资源: 通过不同的参数来实现不同的效果!方法单一,post和get
- http://127.0.0.1/item/queryltem.action?id=1 查询,GET
- http://127.0.0.1/item/saveltem.action 新增,POST
- http://127.0.0.1/item/updateltem.action 更新,POST
- http://127.0.0.1/item/deleteltem.action?id=1 删除,GET或POST
使用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
学习测试:
新建一个类,在SpringMVC中可以使用@PathVariable注解,让方法参数的值对应绑定到一个URL模板变量上
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class RestfulController {
// 原来的: http://localhost:8080/add?a=10&b=5
// ResuFul: http://localhost:8080/add/a/b
@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
// @PostMapping("/add/{a}/{b}")
public String test1(@PathVariable int a,@PathVariable int b, Model model){
int res = a + b;
model.addAttribute("msg","结果为"+res);
return "test";
}
}
使用路径变量的好处:
Spring MVC的@RequestMapping注解能够处理HTTP请求的方法,比如GET、PUT、POST、DELETE以及PATCH。
所有的地址栏请求默认都会是HTTP GET 类型的。
方法级别的注解变体有如下几个:组合注解
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
@GetMapping是一个组合注解
它所扮演的是@RequestMapping(method = RequestMethod.GET)的一个快捷方式。
SpringMVC:结果跳转方式
ModelAndView
设置ModelAndView对象,根据view的名称,和视图解析器跳到指定的页面。
页面:{视图解析器前缀} + viewName + {视图解析器后缀}
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
对应的controller类
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletReqeust httpServletRequest,HttpServletResponse response) throws Exception{
// 返回一个模型视图对象
ModelAndView mv = new ModelAndView();
mv.addobject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
}
ServletAPI
通过设置ServletAPI,不需要视图解析器
@Controller
public class ModelTest1 {
@RequestMapping("/m1/t1")
public String test1(Model model){
// 转发
model.addAttribute("msg","ModelTest1");
// return "forward:/WEB-INF/jsp/test.jsp";
// 重定向
return "redirect:/index.jsp";
}
}
Spring MVC
通过SpringMVC来实现转发和重定向 - 无需视图解析器;
@Controller
public class ResultSpringMVC {
@RequestMapping("/rsm/t1")
public String test1(){
// 转发
return "/index.jsp";
}
@RequestMapping("/rsm/t2")
public String test2(){
// 转发二
return "forword:/index.jsp";
}
@RequestMapping("/rsm/t3")
public String test3(){
// 重定向
return "redirect:/index.jsp";
}
}
通过SpringMVC来实现转发和重定向 - 有视图解析器;
重定向,不需要视图解析器,本质就是重新请求一个新地方嘛,所以注意路径问题。可以重定向到另外一个请求实现。
@Controller
public class ResultSpringMVC2 {
@RequestMapping("/rsm2/t1")
public String test1(){
// 转发
return "test";
}
@RequestMapping("/rsm2/t2")
public String test2(){
// 重定向
return "redirect:/index.jsp";
}
}
接收请求参数及数据回显
处理提交数据
1、 提交的域名和处理方法的参数名一致
提交数据:http://localhost:8080/hello?name=zhangsan
处理方法:
@RequestMapping("/hello")
public String hello(String name){
System.out.println(name);
return "hello";
}
2、 提交的域名称和处理方法的参数名不一致
提交数据:http://localhost:8080/hello?username=zhangsan
处理方法:
// @RequestParam("username") : username提交的域的名称
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name) {
System.out.println(name);
return "hello";
}
3、 提交的是一个对象
要求提交的表单域和对象的属性名一致,参数使用对象即可
说明:如果使用对象的话,前端传递的参数名和对象名必须一致,否则就是null。
数据显示到前端
第一种:通过ModelAndView
第二种:通过ModelMap
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name,Model model){
// 封装要显示到视图中的数据
// 相当于req.setAttribute("name",name);
model.addAttribute("name",name);
System.out.println(name);
return "test";
}
第三种:通过Model
@RequestMapping("/ct2/hello")
public String hello(@RequestParam("username") String name,Model model){
// 封装要显示到视图中的数据
// 相当于req.setAttribute("name",name);
model.addAttribute("msg",name);
System.out.println(name);
return "test";
}
对比
就是对于新手而言简单来说使用区别就是:
Model只有寥寥几个方法适用于储存数据,简化了新手对于Model对象的操作和理解;
ModelMap继承了LinkedMap,除了实现了自身的一些方法,同样的继承LinkedMap的方法和特性;
ModelAndView可以在储存数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。