springmvc后臺(tái)基于@ModelAttribute獲取表單提交的數(shù)據(jù)
1、通過(guò)注解ModelAttribute直接映射表單中的參數(shù)到POJO。在from中的action寫(xiě)提交的路徑,在input的name寫(xiě)參數(shù)的名稱(chēng)。
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.
--%>
<%@ 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>
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的參數(shù)name名稱(chēng)和pojo中的成員域名稱(chēng)完全相同才可以通過(guò)@ModelAttribute進(jìn)行直接映射,否則無(wú)法被賦值的參數(shù)將會(huì)以默認(rèn)值的方式呈現(xiàn)。

2.顯然不可能form獲取的內(nèi)容總是某個(gè)pojo的屬性,完全有可能是單獨(dú)出現(xiàn)的。這時(shí)可以使用@RequestParam獲取參數(shù)。
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";
}
這時(shí)候只需跟在@RequestParam后的參數(shù)和form的name一致即可,String的名稱(chēng)可以隨便取。
3.可以直接啥注解都不加,只需保證參數(shù)名稱(chēng)和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.通過(guò)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";
}
此外,還有一些其他的方式接受數(shù)據(jù),例如通過(guò)@RequestBody等方式傳遞json數(shù)據(jù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)之棧的詳解
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)的棧的應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來(lái)幫助2021-08-08
Java線上CPU內(nèi)存沖高問(wèn)題排查解決步驟
這篇文章主要介紹了Java線上CPU內(nèi)存沖高問(wèn)題排查解決步驟的相關(guān)資料,Java程序在實(shí)際生產(chǎn)過(guò)程中經(jīng)常遇到CPU或內(nèi)存使用率高的問(wèn)題,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
關(guān)于socket發(fā)送數(shù)據(jù)需要注意的問(wèn)題
這篇文章主要介紹了關(guān)于socket發(fā)送數(shù)據(jù)需要注意的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java操作SSH2實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令
這篇文章主要為大家詳細(xì)介紹了Java如何操作SSH2實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-01-01
Java concurrency之公平鎖(二)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency之公平鎖的第二篇內(nèi)容,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Java C++題解leetcode1620網(wǎng)絡(luò)信號(hào)最好的坐標(biāo)
這篇文章主要為大家介紹了Java C++題解leetcode1620網(wǎng)絡(luò)信號(hào)最好的坐標(biāo)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
spring boot配合前端實(shí)現(xiàn)跨域請(qǐng)求訪問(wèn)
本篇文章主要介紹了spring boot配合前端實(shí)現(xiàn)跨域請(qǐng)求訪問(wèn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04

