基于SpringMVC中的路径参数和URL参数代码实例

作者:袖梨 2021-02-23

本篇文章小编给大家分享一下基于SpringMVC中的路径参数和URL参数代码实例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

1、SpringMVC中的路径参数就是指在路径中添加参数,用于实现伪静态是很好的。

2、路径参数实现方式(一个Controller方法)

@RequestMapping(value="/page/{name}/{age}",method=RequestMethod.GET)
public String getName(ModelMap map,@PathVariable("name") String name,@PathVariable("age") int age)
{
  map.addAttribute("name",name);
  map.addAttribute("age",age);
  return "name";
}

3、创建name.jsp文件

<%@page pageEncoding="UTF-8"%>


  
  test


名字:${name}
年龄:${age}

4、在浏览器请求这个controller

http://localhost:8080/page/xiaoming/18

需要注意的是,我这里使用的编辑器是IDEA旗舰版

5、在controller中接受请求参数的实现(controller)

@RequestMapping(value="/result",method=RequestMethod.GET)
public String resultParam(ModelMap map,@RequestParam String name,@RequestParam int age)
{
  map.addAttribute("name",name);
  map.addAttribute("age",age);
  return "result";
}

6、创建result.jsp文件

<%@page pageEncoding="UTF-8">


  
  测试


名字:${name}
年龄:${age}

7、在浏览器中请求这个controller

http://localhost:8080/result?name=xiaoming&age=20

相关文章

精彩推荐