Restful风格路径代码编写
作者:互联网
常规路径get提交
前端页面
<%-- Created by IntelliJ IDEA. User: wsh Date: 2022/5/31 Time: 9:08 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/spring_mvc02_war_exploded/add" method="get"> <input type="text" name="a"> <br/> <input type="text" name="b"> <br/> <input type="submit"> </form> </body> </html>
浏览器传递地址
controller后端处理代码
@GetMapping("/add") public String test2( String a,String b,Model model){ String res=a+b; model.addAttribute("msg","结果为:"+res); return "hello"; }
注意:springMVC的Controller类里面Mapping映射下所有方法里的参数都可匹配浏览器地址传递的参数
用Restful方式传递参数,可在需要传递的参数前加
@PathVariable
注解
可用
@RequestMapping
不同的延申注解接收相同地址的不同提交方式的数据
如下
post提交页面
<%-- Created by IntelliJ IDEA. User: wsh Date: 2022/5/31 Time: 9:45 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/spring_mvc02_war_exploded/add/2/3" method="post"> <input type="submit"> </form> </body> </html>
/2/3为restful传输方式的数据用url传递
后端Controller类代码:
@Controller public class restfulController { @RequestMapping(value = "/add/{a}/{b}",method = {RequestMethod.POST}) public String test(@PathVariable int a, @PathVariable int b, Model model){ model.addAttribute("msg","结果为:"+a+b); return "hello"; }
最后hello.jsp显示页面,补充
<%-- Created by IntelliJ IDEA. User: wsh Date: 2022/5/30 Time: 9:06 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
标签:String,Title,路径,Controller,msg,编写,model,Restful,PathVariable 来源: https://www.cnblogs.com/wupupupu/p/16329404.html