1.登录页面
<!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>