model 提交表信息java,springmvc后台基于@ModelAttribute获取表单提交的数据

1、通过注解ModelAttribute直接映射表单中的参数到POJO。在from中的action写提交的路径,在input的name写参数的名称。

POJO

package com.demo.model;

public class user {

private String username;

private String password;

private int nsex;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public void setNsex(int nsex) {

this.nsex = nsex;

}

public int getNsex() {return nsex;}

}

FORM

Created by IntelliJ IDEA.

User: wym

Date: 2019/10/8

Time: 23:17

To change this template use File | Settings | File Templates.

--%>

pageEncoding="UTF-8"%>

Login

用户名:

密码:

CONTROLLER

package com.demo.controller;

import com.demo.model.user;

import com.demo.service.Userservice;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

@Controller

public class LoginController {

@Autowired

private Userservice userService;

@RequestMapping(value="/login", method= RequestMethod.POST)

public String hello(@ModelAttribute user u, HttpSession session){

session.setAttribute("user", u);

user user = userService.findbyname(u.getUsername());

if(user == null)

return "loginfail";

else if(!user.getPassword().equals(u.getPassword()))

return "falsepaswd";

else

return "helloworld";

}

}

注意!!这里只有input的参数name名称和pojo中的成员域名称完全相同才可以通过@ModelAttribute进行直接映射,否则无法被赋值的参数将会以默认值的方式呈现。

35b81d5bdaccbedf85aa07743bc3686d.png

2.显然不可能form获取的内容总是某个pojo的属性,完全有可能是单独出现的。这时可以使用@RequestParam获取参数。

public String hello(@RequestParam(value="username") String A, @RequestParam(value="password") String B, HttpSession session){

session.setAttribute("a", A);

session.setAttribute("b", B);

user user = userService.findbyname(A);

if(user == null)

return "loginfail";

else if(!user.getPassword().equals(B))

return "falsepaswd";

else

return "helloworld";

}

这时候只需跟在@RequestParam后的参数和form的name一致即可,String的名称可以随便取。

3.可以直接啥注解都不加,只需保证参数名称和form的name即可

public String hello( String username, String password, HttpSession session){

session.setAttribute("a", username);

session.setAttribute("b", password);

user user = userService.findbyname(username);

if(user == null)

return "loginfail";

else if(!user.getPassword().equals(password))

return "falsepaswd";

else

return "helloworld";

}

4.通过HttpServletRequest接收

public String hello( HttpServletRequest req, HttpSession session){

username=req.getParameter("username");

password=req.getParameter("password");

session.setAttribute("a", username);

session.setAttribute("b", password);

user user = userService.findbyname(username);

if(user == null)

return "loginfail";

else if(!user.getPassword().equals(password))

return "falsepaswd";

else

return "helloworld";

}

此外,还有一些其他的方式接受数据,例如通过@RequestBody等方式传递json数据。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值