HTTP请求地址映射

HTTP请求地址映射

1编写UserController 类

package com.xiang.lesson01.controller;

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

@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(){
        return "/user/success";
    }

}

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/dologin/123" 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运行结果

原文地址:https://www.cnblogs.com/d534/p/15248680.html