微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringMVC 处理提交数据

1、提交的域名称和处理方法的参数名一致

提交数据:http://localhost:8085/springmvc_01_war_exploded/user?name=123

处理方法
 

 @RequestMapping("/user")
    public String test1(String name, Model model){
        //1.接受前端参数
        System.out.println("接受前端的参数为"+name);
        //2.将返回的结果传递给前端,Model
        model.addAttribute("msg",name);
        //3.视图跳转
        return "test";
    }

2、提交的域名称和处理方法的参数名称不一致

提交数据:http://localhost:8085/springmvc_01_war_exploded/t2?username=123

处理方法

 @RequestMapping("/t2")
    public String test2(@RequestParam("username") String name, Model model){
        //1.接受前端参数
        System.out.println("接受前端的参数为"+name);
        //2.将返回的结果传递给前端,Model
        model.addAttribute("msg",name);
        //3.视图跳转
        return "test";
    }

建议所有参数都加上@RequestMapping

3、前端接收的是一个对象

提交数据:http://localhost:8085/springmvc_01_war_exploded/t3?id=123&name=huang&age=18

实体类

public class User {

    private int id;
    private String name;
    private int age;
}

处理方法

/*
假设传递的是一个user对象,匹配user对象中的字段名,如果名字一样成功,否则匹配失败
 */
    @RequestMapping("/t3")
    public String test3(User user){
        //1.接受前端参数
        System.out.println("接受前端的参数为"+user);
        return "test";
    }

说明:如果使用对象传递,前端传递的参数必须和对象名一致,否则传递数据为null

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐