springmvc后台獲取表單提交的數據——@ModelAttribute等方式


 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獲取參數。

 1     public String hello(@RequestParam(value="username") String A, @RequestParam(value="password") String B, HttpSession session){
 2         session.setAttribute("a", A);
 3         session.setAttribute("b", B);
 4         user user = userService.findbyname(A);
 5         if(user == null)
 6             return "loginfail";
 7         else if(!user.getPassword().equals(B))
 8             return "falsepaswd";
 9         else
10             return "helloworld";
11 
12     }

這時候只需跟在@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數據。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM