SpringMVC前台往后台传递参数的方式

1.使用原始的方式,通过请求对象来获取参数

 @RequestMapping("/login")
    public void login(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username);
        System.out.println(password);
    }

2.使用同名形参的方式(表单的参数名要和形参的相同,这种方式适用于参数数量较少的时候)
表单的参数名和形参的名称不同,就使用requestParam注解

 @RequestMapping("/login")
    public void login(@RequestParam("username") String username, String password){
        System.out.println(username);
        System.out.println(password);
    }

3.将参数封装到对象中,使用对象来接收

 @RequestMapping("/login")
    public void login(User u){
        System.out.println(u.getPassword());
        System.out.println(u.getUsername());
    }
public class User {
    private String username;
    private String password;
}

4.当前台是使用地址栏传递参数的时候.例如使用restful风格的时候
如localhost/login/5

 @RequestMapping("/login/{id}")
    public void login(@PathVariable("id") Long id ){
        System.out.println(id);
    }

5.针对于请求参数中有中文的问题,可以在web.xml中配置一个编码过滤器
但是只针对于post请求有效,如果是get请求,还需要使用原始的方式来配置,所以一般不建议在get请求中有中文参数
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值