1.编写web.xml(模板)
2.springmvc配置文件
3.编写对应数据库字段的pojo实体类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String name; private int age; }View Code
4.编写Controller类
首先是从前端获取单个参数的测试
这里要注意的是注解@GetMapping只接收get方式的前端请求,@RequestParam限制了前端url传入的参数必须是username=xxx,否则报错,不加的话,在前端乱输入都会返回null
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Controller public class UserController { @GetMapping("/t1") public String test1(@RequestParam("username") String name, Model model){ //1.接收前端参数 System.out.println("从前端接收的参数是"+name); //2.将返回的结果传递给前端 model.addAttribute("msg",name); //3.视图跳转 return "test"; } }View Code
其次是从前端获取多个参数(封装成对象)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@RequestMapping("/t2") public String test2( User user){//当前端返回的是一个对象 System.out.println(user); return "test"; }View Code
5.最后配置tomcat测试
标签:String,SpringMVC,前端,private,参数,后台,public,name From: https://www.cnblogs.com/MyBlogs-joyiyii/p/17344141.html