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

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;}
}

POJO

<%--
Created by IntelliJ IDEA.
User: wym
Date: 2019/10/8
Time: 23:17
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="username"/> <br><br>
密码:<input type="password" name="password"/> <br><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>

FORM

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";
} }

CONTROLLER

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

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数据。

springmvc后台获取表单提交的数据——@ModelAttribute等方式的更多相关文章

  1. (五)springmvc之获取表单提交的数据

    8.1:使用Request <form method="post" id="form1" action="<%=request.getCo ...

  2. koa 基础(十一)koa 中 koa-bodyparser 中间件获取表单提交的数据

    1.app.js /** * koa 中 koa-bodyparser 中间件获取表单提交的数据 * 1.npm install --save koa-bodyparser * 2.引入 const ...

  3. koa 基础(十)原生node.js 在 koa 中获取表单提交的数据

    1.app.js // 引入模块 const Koa = require('koa'); const router = require('koa-router')(); /*引入是实例化路由 推荐*/ ...

  4. 获取表单提交的数据getParameter()方法

    请求对象:request public String getParameter(String name); 通过request的getParameter(String name)方法获取 表单里面的n ...

  5. springMVC+thymeleaf form表单提交前后台数据传递

    后端: @RequestMapping(value = "/add", method=RequestMethod.POST) public String save(@ModelAt ...

  6. 在html中如何获取表单提交的数据

    a.html: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www ...

  7. 在Action中获取表单提交数据

    -----------------siwuxie095 在 Action 中获取表单提交数据 1.之前的 Web 阶段是提交表单到 Servlet,在其中使用 Request 对象 的方法获取数据 2 ...

  8. Struts2_day02--Action获取表单提交数据

    Action获取表单提交数据 1 之前web阶段,提交表单到servlet里面,在servlet里面使用request对象里面的方法获取,getParameter,getParameterMap 2 ...

  9. 非form表单提交的数据就要用@requestbody注解获取http传过来的值,尤其json

    非form表单提交的数据就要用@requestbody注解获取http传过来的值,尤其json

随机推荐

  1. DAY 吐

    今天所学: 一,Linux的文件和目录管理 #1 cd( 变更用户所在目录)直接运行cd会进入root的/root下,后面跟目录名,会进入指定目录下( 后面只能是目录名,不能跟文件名). #2 pwd ...

  2. Linux文件属性之用户与用户组基础知识回顾

    回顾: 用户.用户组的概念: 每个文件和进程,都需要对应一个用户和用户组. linux系统通过UID和GID来识别用户和组的. 用户名相当于人名 UID和GID  身份证号 管理员:root   do ...

  3. [转帖]使用Grafana和Telegraf监视VMware ESXi的方法

    使用Grafana和Telegraf监视VMware ESXi的方法 2019-04-03 15:28:30作者:曾秀珠稿源:云网牛站 https://ywnz.com/linuxyffq/4660. ...

  4. 初识numpy库

    numpy是一个在Python中做科学计算的基础库,重在数值计算,也是大部分Python科学计算库的基础库,多用于在大型.多维数组上执行数值运算 numpy创建数组(矩阵): numpy中的数据类型: ...

  5. 菜单ACTION控制栏位字段编辑,点击菜单ACTION才能编辑指定的栏位

    范例(axmt500): 目的,控制新增的栏位(价格清单2),需点击菜单栏“修改价格清单2”才能对相应的栏位进行编辑修改,并记录修改人.日期: 1)在规格上增加新ACTION——action_modi ...

  6. Codeforces 1178E. Archaeology

    传送门 首先一定有解,考虑归纳法证明 首先 $n<=3$ 时显然 考虑 $n=4$ 时,那么因为 $s[1]!=s[2],s[3]!=s[4]$ ,并且 $s[i] \in {a,b,c}$ 由 ...

  7. C Looooops

    看了半天的同余 扩展欧几里得 练练手 C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27079   A ...

  8. sccrapy 爬虫框架网数据库储存时去重的问题

    from scrapy.exceptions import DropItem #导入异常处理模块 class Baidu03Pipeline(object): def __init__(self): ...

  9. 转:git上传本地项目到github

    转自:https://blog.csdn.net/Lucky_LXG/article/details/77849212 将本地项目上传到Github(两种简单.方便的方法) 一.第一种方法:首先你需要 ...

  10. px自动换算rem

    //designWidth:设计稿的实际宽度值,需要根据实际设置//maxWidth:制作稿的最大宽度值,需要根据实际设置//这段js的最后面有两个参数记得要设置,一个为设计稿实际宽度,一个为制作稿最 ...