SpringMVC 入门——③[登录注册 (小demo)]
前期准备
建议先看完 Spring-MVC ② [常见和只需要了解的注释] 再食用
因为是小demo 所以一切从简,主要演示基本功能
1.注册.jsp 登录.jsp 主界面.jsp 登录成功.jsp
A 主界面.jsp

能用就行
<a href="${pageContext.request.contextPath}/user/addUser">用户注册</a>
<a href="${pageContext.request.contextPath}/user/showLogin">用户登录</a>
注意!!!
建议养成写
{pageContext.request.contextPath}
这个EL表达式的习惯,这个表达式的作用是:取出部署的应用程序名,这样不管如何部署,所用路径都是正确的
B 注册界面.jsp

<%--
Created by IntelliJ IDEA.
User: Tenkinoko
Date: 2021/6/24
Time: 19:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户注册</title>
</head>
<body>
<h3>用户注册</h3>
<form method="post" action="${pageContext.request.contextPath}/user/addregUser">
<table>
<tr>
<td><label>登录名</label></td>
<td><input type="text" name="uname"></td>
</tr>
<tr>
<td><label>登录密码</label></td>
<td><input type="password" name="upassword"></td>
</tr>
<tr>
<td><label>用户名</label></td>
<td><input type="text" name="un"></td>
</tr>
<tr>
<td><label>注册</label></td>
<td><input type="submit" name="注册"></td>
</tr>
</table>
</form>
</body>
</html>
C 登录界面.jsp

<%--
Created by IntelliJ IDEA.
User: Tenkinoko
Date: 2021/6/24
Time: 19:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录界面</title>
</head>
<body>
<h3>用户登录</h3>
<form method="post" action="${pageContext.request.contextPath}/user/loginUser">
<table>
<tr>
<td><label>登录账号</label></td>
<td><input type="text" name="uname"></td>
</tr>
<tr>
<td><label>登录密码</label></td>
<td><input type="password" name="upassword"></td>
</tr>
<tr>
<td><input id="submit" type="submit" name="登录"></td>
</tr>
</table>
</form>
</body>
</html>
D 登录成功
写个你喜欢的jsp文件就行
<%--
Created by IntelliJ IDEA.
User: Tenkinoko
Date: 2021/6/23
Time: 19:06
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>
<h1>皆さん❕</h1>
<h2> 大好き❕</h2>
<h3>あいしてる!</h3>
</body>
</html>
2.Controller层的一个方法
package com.Ten.handler;
import com.Ten.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.List;
@RequestMapping("/user")
@Controller
public class UserHandler {
private static List<User> list = new ArrayList<>();
@RequestMapping("/addUser")
public String addUser(){
return "regUser";
}
@RequestMapping("/loggin")
public String logins(){ return "login";}
@RequestMapping("/addregUser")
public String regUser(@RequestParam("uname") String name,
@RequestParam("upassword") String password,
@RequestParam("un") String un){
User user = new User(name,password,un);
list.add(user);
return "login";
}
@RequestMapping("/loginUser")
public String login(@RequestParam("uname") String name,
@RequestParam("upassword") String password){
for(User user:list){
if(user.getUname().equals(name)&&user.getUpassword().equals(password)){
return "mvc";
}
}
return "login";
}
}
3.pojo实现层的一个方法
package com.Ten.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String uname;
private String upassword;
private String un;
}
4.结语
现学现卖吧,不知道怎么转的话就多看,其实就是反复横跳。
“你目前觉得不想做的事情 就是你现在需要做的。”