欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springmvc后臺(tái)基于@ModelAttribute獲取表單提交的數(shù)據(jù)

 更新時(shí)間:2019年10月14日 14:31:00   作者:LeftBody  
這篇文章主要介紹了springmvc后臺(tái)基于@ModelAttribute獲取表單提交的數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1、通過(guò)注解ModelAttribute直接映射表單中的參數(shù)到POJO。在from中的action寫提交的路徑,在input的name寫參數(shù)的名稱。

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名稱和pojo中的成員域名稱完全相同才可以通過(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的名稱可以隨便取。

3.可以直接啥注解都不加,只需保證參數(shù)名稱和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)文章

  • Nginx啟用壓縮及開啟gzip 壓縮的方法

    Nginx啟用壓縮及開啟gzip 壓縮的方法

    這篇文章主要介紹了Nginx啟用壓縮及開啟gzip 壓縮的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-08-08
  • Java模擬實(shí)現(xiàn)撲克牌洗牌和發(fā)牌的示例代碼

    Java模擬實(shí)現(xiàn)撲克牌洗牌和發(fā)牌的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Java模擬實(shí)現(xiàn)撲克牌洗牌和發(fā)牌的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下
    2022-09-09
  • Java創(chuàng)建型設(shè)計(jì)模式之抽象工廠模式(Abstract?Factory)

    Java創(chuàng)建型設(shè)計(jì)模式之抽象工廠模式(Abstract?Factory)

    當(dāng)系統(tǒng)所提供的工廠所需生產(chǎn)的具體產(chǎn)品并不是一個(gè)簡(jiǎn)單的對(duì)象,而是多個(gè)位于不同產(chǎn)品等級(jí)結(jié)構(gòu)中屬于不同類型的具體產(chǎn)品時(shí)需要使用抽象工廠模式,抽象工廠模式是所有形式的工廠模式中最為抽象和最具一般性的一種形態(tài)
    2022-09-09
  • mybatis interceptor 處理查詢參數(shù)及查詢結(jié)果的實(shí)例代碼

    mybatis interceptor 處理查詢參數(shù)及查詢結(jié)果的實(shí)例代碼

    這篇文章主要介紹了mybatis interceptor 處理查詢參數(shù)及查詢結(jié)果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-01-01
  • 劍指Offer之Java算法習(xí)題精講數(shù)組與字符串

    劍指Offer之Java算法習(xí)題精講數(shù)組與字符串

    跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • IDEA中解決 git pull 沖突的方法

    IDEA中解決 git pull 沖突的方法

    這篇文章主要介紹了IDEA中解決 git pull 沖突的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • IDEA集成Gitee碼云的實(shí)現(xiàn)步驟

    IDEA集成Gitee碼云的實(shí)現(xiàn)步驟

    本文主要介紹了IDEA集成Gitee碼云的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Java正則表達(dá)式工具方法匯總

    Java正則表達(dá)式工具方法匯總

    這篇文章主要介紹了Java正則表達(dá)式工具方法匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Mybatis注解開發(fā)單表、多表操作的實(shí)現(xiàn)代碼

    Mybatis注解開發(fā)單表、多表操作的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Mybatis高級(jí):Mybatis注解開發(fā)單表操作,Mybatis注解開發(fā)多表操作,構(gòu)建sql語(yǔ)句,綜合案例學(xué)生管理系統(tǒng)使用接口注解方式優(yōu)化,需要的朋友可以參考下
    2021-02-02
  • RocketMq 消息重試機(jī)制及死信隊(duì)列詳解

    RocketMq 消息重試機(jī)制及死信隊(duì)列詳解

    這篇文章主要為大家介紹了RocketMq 消息重試機(jī)制及死信隊(duì)列詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10

最新評(píng)論