springmvc 后台接收前台数据

1,使用HttpServletRequest获取

1.1后台方法

    @RequestMapping(value="/login",method = RequestMethod.POST)
    public String login(HttpServletRequest request) {
        if(userService.selectUser(request.getParameter("username"),request.getParameter("password"))!=null)
        {
            return "index";
        }
        else
            return "login";
    }
1.2前台表单的name属性要和后台参数名保持一致

     <form action="mavenTest/user/login" method="post">
            <input type="text" name="username"  placeholder="Username...">
            <input type="password" name="password"  placeholder="Password...">
             <button type="submit" class="btn">Sign in!</button>
      </form>

2.自动注入bean属性

2.1后台方法

    @RequestMapping(value="/login",method = RequestMethod.POST)
    public String login(User user) {
        if(userService.selectUser(user.getUsername(),user.getPassword())!=null){
            return "index";
        }
        else
            return "login";
    }

2.2前台表单的name属性要和类的参数名保持一致,并且要有相应的get和set方法

user类

public class User implements Serializable{
    private int id;
    private String username;
    private String password;
    private int age;


     <form action="mavenTest/user/login" method="post">
            <input type="text" name="username"  placeholder="Username...">
            <input type="password" name="password"  placeholder="Password...">
             <button type="submit" class="btn">Sign in!</button>
      </form>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值