脚本宝典收集整理的这篇文章主要介绍了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>
以上是脚本宝典为你收集整理的Restful风格路径代码编写全部内容,希望文章能够帮你解决Restful风格路径代码编写所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。