1、通過(guò)注解ModelAttribute直接映射表單中的參數(shù)到POJO。在from中的action寫(xiě)提交的路徑,在input的name寫(xiě)參數(shù)的名稱(chēng)。
成都創(chuàng)新互聯(lián)公司專(zhuān)注于企業(yè)全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、河西網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為河西等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
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"> 用戶(hù)名:<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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)頁(yè)名稱(chēng):springmvc后臺(tái)基于@ModelAttribute獲取表單提交的數(shù)據(jù)
網(wǎng)站地址:http://bm7419.com/article14/jcsjge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、Google、動(dòng)態(tài)網(wǎng)站、電子商務(wù)、定制網(wǎng)站、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)