spring mvc 编写处理带参数的Controller
2024-08-21 14:50:25
在上一随笔记录的基础上,现记录编写处理带有参数的Controller。
@Controller //这个注解会告知<context:component:scan> 将HomeController自动检测为一个Bean
@RequestMapping("/home") //这是根Url
public class HomeController {
private UserService userService;
@Autowired
public HomeController(UserService userService){
this.userService = userService;
}
//指定请求的路径,处理get请求的路径为 xxx/home/home.html的请求
@RequestMapping(value="home.html",method=RequestMethod.GET)
public String showHomePage(@RequestParam("name")String username,Model model){
model.addAttribute("username",username);
model.addAttribute("count", userService.getUsersCount());
return "home";
}
}
注意到,在showHomePage方法的输入参数中,有@RequestParam("name")String username这个参数,表示接收请求路径的参数name,并赋值给username.
如果请求的路径的参数名,与方法的入参的变量名一致,就可以不需要@RequestParam这个注解。
以上showHomePage这个方法处理的请求路径为:项目根路径/home/home.html?name="who"
最新文章
- Java可变参数讲解
- 【Mysql】权限管理
- 07.19 Linux命令 cd
- 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap good
- Git 和 Github的关系
- Python-接口自动化(七)
- pg_dump 数据处理
- html标签必备
- 遇到问题---hosts不起作用问题的解决方法
- MonkeyRunner进坑——errors importing other modules
- 获取spring的ApplicationContext几种方式【转】
- Three Pieces CodeForces - 1065D (BFS)
- selenium测试(Java)-- 一组元素操作(十一)
- web开发之微信公众号---微信公众好开发
- HttpHelper类及调用
- 算法笔记_151:算法提高 01背包(Java)
- 9.spring:事务管理(下):声明式事务管理
- python学习笔记:第7天 深浅拷贝
- 设置eclipse中jsp/html文件好看的自动排版
- 使用Git操作码云