81.【SpringMVC】(四)

简介: 81.【SpringMVC】

(八)、RestFul 风格【路径】

1.Restful 概念

Restful 一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。就是用于一个资源的定位操作。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

2.RestFul 功能

  • 资源: 互联网所有事务都可以被认为是抽象资源
  • 资源操作: 使用 POST、DELETE、PUT、GET
  • 分别对应着增删改查

3.传统方式操作资源

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

4.使用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 
(1).后端挖空前端怎么输入【旧的】
package Com.Jsxs.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Controller4 {
//    http://localhost/SpringMVC_06_Controller_war_exploded/add?a=1&b=2
//    原来的方式,前端如何给后端赋值?  路径?a=变量1&b=变量2
    @RequestMapping("/add")
    public String test(int a, int b, Model model){
        int result=a+b;
        model.addAttribute("msg","结果为:"+result);
        return "hello";
    }
}

http://localhost/SpringMVC_06_Controller_war_exploded/add?a=1&b=2

(2).后端挖空(路径)前端怎么输入【RestFul风格】
1.添加注解: @PathVariable 
2.(@PathVariable int a, @PathVariable int b, Model model)
package Com.Jsxs.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Controller4 {
//    http://localhost/SpringMVC_06_Controller_war_exploded/add?a=1&b=2
//    原来的方式,前端如何给后端赋值?  路径?a=变量1&b=变量2
    @RequestMapping("/add/{a}/{b}")
    public String test(@PathVariable int a, @PathVariable int b, Model model){
        int result=a+b;
        model.addAttribute("msg","结果为:"+result);
        return "hello";
    }
}

(3).RequestMethod.DELETE方式进行获取【1】

SpringMVC 控制器默认支持GET和POST两种方式

package Com.Jsxs.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class Controller4 {
//    http://localhost/SpringMVC_06_Controller_war_exploded/add?a=1&b=2
//    原来的方式,前端如何给后端赋值?  路径?a=变量1&b=变量2
    @RequestMapping(name = "/add/{a}/{b}",method = RequestMethod.DELETE)
    public String test(@PathVariable int a, @PathVariable int b, Model model){
        int result=a+b;
        model.addAttribute("msg","结果为:"+result);
        return "hello";
    }
}

指定为RequestMethod.GET进行获取

package Com.Jsxs.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class Controller4 {
//    http://localhost/SpringMVC_06_Controller_war_exploded/add?a=1&b=2
//    原来的方式,前端如何给后端赋值?  路径?a=变量1&b=变量2
    @RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
    public String test(@PathVariable int a, @PathVariable int b, Model model){
        int result=a+b;
        model.addAttribute("msg","结果为:"+result);
        return "hello";
    }
}

(4).利用注解提交的方式【2】
package Com.Jsxs.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class Controller4 {
//    http://localhost/SpringMVC_06_Controller_war_exploded/add?a=1&b=2
//    原来的方式,前端如何给后端赋值?  路径?a=变量1&b=变量2
//    @RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
    @GetMapping("/add/{a}/{b}")
    public String test(@PathVariable int a, @PathVariable int b, Model model){
        int result=a+b;
        model.addAttribute("msg","结果为:"+result);
        return "hello";
    }
}

(5).四种注解的提交方式
@GetMapping("/add/{a}/{b}")   get方式进行提交
    @PostMapping("/add/{a}/{b}")   post方式进行提交
    @DeleteMapping("/add/{a}/{b}")  delete方式进行提交
    @PutMapping("/add/{a}/{b}")  put方式进行提交
(6).访问地址相同,但是提交方式不同
实质: 就是提交的方式不同,所以在路径相同的情况下访问的内容不同

首先创建一个a.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="add/1/2" method="post">
    <input type="submit">
</form>
</body>
</html>

创建一个类,类中有两个方法分别对应着不同的调用方式

package Com.Jsxs.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class Controller4 {
//    http://localhost/SpringMVC_06_Controller_war_exploded/add/1/2
    @GetMapping("/add/{a}/{b}")
    public String test(@PathVariable int a, @PathVariable int b, Model model){
        int result=a+b;
        model.addAttribute("msg","结果为get1:"+result);
        return "hello";
    }
//    http://localhost/SpringMVC_06_Controller_war_exploded/add/1/2
    @PostMapping("/add/{a}/{b}")
    public String test1(@PathVariable int a, @PathVariable int b, Model model){
        int result=a+b;
        model.addAttribute("msg","结果为post2:"+result);
        return "hello";
    }
}

通过请求转发,也就是方法一默认的GET方式提交>

通过表单进行提交,提交方式为post,提交的位置是方法2利用post提交

(7).访问地址相同,提交方式也相同
package Com.Jsxs.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class Controller4 {
//    http://localhost/SpringMVC_06_Controller_war_exploded/add?a=1&b=2
//    原来的方式,前端如何给后端赋值?  路径?a=变量1&b=变量2
//    @RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
//    http://localhost/SpringMVC_06_Controller_war_exploded/add/1/2
    @GetMapping("/add/{a}/{b}")
    public String test(@PathVariable int a, @PathVariable int b, Model model){
        int result=a+b;
        model.addAttribute("msg","结果为get1:"+result);
        return "hello";
    }
//    http://localhost/SpringMVC_06_Controller_war_exploded/add/1/2
    @GetMapping("/add/{a}/{b}")
    public String test1(@PathVariable int a, @PathVariable int b, Model model){
        int result=a+b;
        model.addAttribute("msg","结果为post2:"+result);
        return "hello";
    }
}

(九)、ModelAndView (视图解析器)

设置ModelAndView对象,根据View的名称和视图解析器跳到指定的页面;

页面: {视图解析器前缀}+viewName+{视图解析器后缀}

1.跟据视图解析器进行转发的两种形式

(1).利用Servlet进行跳转
package Com.Jsxs.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Controller
public class Controller5 {
    @RequestMapping("/m1/t1")
    public String test(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        session.setAttribute("msg","通过Session进行获取");
        return "hello";
    }
}

(2).利用Model进行跳转
package Com.Jsxs.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Controller
public class Controller5 {
    @RequestMapping("/m1/t1")
    public String test(Model model){
        model.addAttribute("msg","通过Model进行视图的跳转");
        return "hello";
    }
}

(3).路径映射的问题怎么解决

因为在JSP中我们不能使用绝对路径,所以我们要使用EL表达式进行干涉

如何干涉? ${pageContext.request.contextPath}/login.do

相关文章
|
9月前
|
JSON JavaScript 前端开发
|
9月前
|
前端开发 Java 网络架构
|
9月前
|
Java Spring
|
9月前
|
XML JSON fastjson
|
1月前
|
JSON 前端开发 Java
SpringMVC
SpringMVC
|
1月前
|
前端开发 Java API
|
1月前
|
XML 前端开发 Java
SpringMvc专题
SpringMvc笔记(持续更新)此方法的执行时机是在控制器方法执行之前,所以我们通常是使用此方法对请求部分进行增强。同时由于结果视图还没有创建生成,所以此时我们可以指定响应的视图。
|
11月前
|
XML 设计模式 开发框架
springMVC篇
Spring MVC是基于Java的Web应用程序开发框架,它是Spring框架的一部分,用于简化和加速Web应用程序的开发过程。
55 1
|
10月前
|
XML JSON 前端开发
你真的了解SpringMVC吗?(中)
你真的了解SpringMVC吗?(中)
57 0
|
JSON 前端开发 JavaScript
springmvc(一)
springmvc(一)
78 0
springmvc(一)