前端向后端傳遞數據


參見 :

https://blog.csdn.net/a532672728/article/details/78057218

https://www.cnblogs.com/unknows/p/11276345.html

前端:

<!--用table布局、 label的inline-block 對齊input-->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title>Title</title>
</head>
<body>
<div class="web_login">
    <ul>
        <ul>
            <!--accept-charset 屬性規定服務器處理表單數據所接受的字符集.此屬性的默認值是
            "unknown",表示表單的字符集與包含表單的文檔的字符集相同-->
            <form name="form2" accept-charset="utf-8"  action="/security/addregister" method="post">
                <!--用table對齊-->
                <table >
                    <tr>
                        <td>用戶名:</td>
                        <td><input type="text" name="username" maxlength="16"/></td>
                    </tr>
                    <tr>
                        <td>密碼:</td>
                        <td><input type="password" name="password1" maxlength="16" /></td>
                    </tr>
                    <tr>
                        <td>確認密碼:</td>
                        <td><input type="password" name="password2" maxlength="16" /></td>
                    </tr>
                </table>

                <div class="inputArea">
                    <a th:href="@{/security/readdoc}" target="_blank">閱讀注冊協議</a>
                    <input type="submit" value="同意協議並注冊"/>
                </div>
            </form>
        </ul>
    </ul>
</div>
</body>
</html>

 

后端:

    @RequestMapping("/addregister")
    public String register(HttpServletRequest request){
        String username = request.getParameter("username");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        if (password1.equals(password2))
        {
            User1 user = new User1();
            user.setUserName(username);
            user.setUserPwd(password1);
            userService.addUser(user);
            return "login";
        }
        else
        {
            return "register";
        }
    }

 

二、form提交: 

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>課程管理系統登錄頁面</title>
  <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}" />
  <script type="text/javascript" th:src="@{/js/jquery-3.1.1.min.js}"></script>
</head>
<body>
<div>
  <div id="wrapper" style="text-align:center">
    <img id="login"   width="100" height="20"   th:src="@{/pics/logo.png}" />
    <div id="f_title">課程管理系統用戶登錄</div>
    <!--${user}取后台轉遞的map參數,其中有個key是user-->
    <form th:action="@{/security/login}" method="post" th:object="${user}">
      <div  class="f_row">
        <span>用戶姓名:</span>
        <input type="text" class="form-control" name="userName" th:value="*{userName}" placeholder="請輸入姓名">
      </div>
      <div class="f_row">
        <span>登錄密碼:</span>
        <input type="text" class="form-control" name="userPwd" th:value="*{userPwd}" placeholder="請輸入密碼">
      </div>
      <br>
      <div class="f_row">
        <input type="submit" value=" 登 錄 "></input>
      </div>
    </form>
  </div>
</div>
<div id="footer">
  <div th:insert="footer :: copy"></div>
  <div th:insert="footer :: time"></div>
</div>
</body>
</html>

 

 登錄,執行到控制器:/security/login

    @PostMapping(value="/login")
    public String login( User1 user,Map<String, Object> map ) {
        String userName=user.getUserName();
        String userPwd=user.getUserPwd();
        List<User1> lus=userService.loadUserByUserName(userName);
        if(lus!=null){
            User1 user1=lus.get(0);
            if(user1.getUserPwd().equals(userPwd))
            {
                map.put("user",user1);
                return "main";
            }
        }
        return "login";
    }

兩個參數都收到數據??

user的值:

 

 

map的值:

 

如果將參數改為:

 

則map收不到數據。

 


免責聲明!

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



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