springboot接收浏览器参数

1.登录页面

templates目录下创建登录页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${ctx.contextPath}/login" method="post">
    <table>
        <tr>
            <td>用户名</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="text" name="password"></td>
        </tr>
        <tr>
            <td><input type="reset" value="重置"></td>
            <td><input type="submit" value="登录"></td>
        </tr>
        <#if (errorMsg??)>
            ${errorMsg }
        </#if>
    </table>
</form>
</body>
</html>

http://localhost:8080/view/login

2.登录接口实现

package com.xbb.springboot.tutorial.web;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {

    @RequestMapping("login")
    public String login(HttpServletRequest request, String username, String password) {
        if ("admin".equals(username) && "666666".equals(password)) {
            request.getSession().setAttribute("username", "admin");
            return "redirect:/view/index"; //客户端跳转
        } else {
            request.setAttribute("errorMsg", "用户名或密码有误");
            return "login";//服务器跳转
        }
    }
    
}

3.创建系统首页

创建系统首页
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${username}
</body>
</html>

http://localhost:8080/view/index

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。