1. 程式人生 > 其它 >springMVC 中引數繫結

springMVC 中引數繫結

引數繫結

1編寫UserController 類

package com.xiang.lesson01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @RequestMapping("/login")
    public String register() {
        return "user/login";
    }


    @RequestMapping(value = "/dologin/{userId}", method = RequestMethod.POST)
    public String login(@PathVariable("userId") int userId) {
//       PathVariable 前臺 傳入後臺
        System.out.println("userid====>" + userId);
        return "/user/success";
    }

    @RequestMapping(value = "/logintwo", method = RequestMethod.POST)
    public String login2(@RequestParam("user") String user, @RequestParam("pwd") String pwd) {
        if (user.equals("xiang") && pwd.equals("123")) {
            return "/user/success";
        } else {
            return "/user/fail";
        }
    }
}

2編寫login.jsp 檔案

<%--
  Created by IntelliJ IDEA.
  User: Xiang
  Date: 2021/9/9
  Time: 11:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>

<body>
<div align="center">
    <form action="<%=request.getContextPath()%>/user/logintwo" method="post">
        <table border="1">
            <tr>
                <td>user</td>
                <td>
                    <input type="text" name="user">
                </td>
            </tr>
            <tr>
                <td>pwd</td>
                <input type="password" name="pwd">
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit">
                </td>
            </tr>
        </table>
    </form>
</div>


</body>
</html>

3編寫success.jsp 檔案

<%--
  Created by IntelliJ IDEA.
  User: Xiang
  Date: 2021/9/9
  Time: 15:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>
  success
</h3>
</body>
</html>

4編寫login.jsp 檔案

<%--
  Created by IntelliJ IDEA.
  User: Xiang
  Date: 2021/9/9
  Time: 16:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<div align="center">
    <form action="<%=request.getContextPath()%>/user/logintwo" method="post">
        <table border="1">
            <tr>
                <td>user</td>
                <td>
                    <input type="text" name="user">
                </td>
            </tr>
            <tr>
                <td>pwd</td>
                <input type="password" name="pwd">
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit">
                </td>
            </tr>
        </table>
    </form>
</div>


</body>
</html>

5執行截圖