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运行截图



posted @ 2021-09-10 10:08  阿向向  阅读(48)  评论(0编辑  收藏  举报