IDEA SpringBoot下创建SpringMVC项目注册登录实例
IDEA SpringBoot下创建SpringMVC项目注册登录实例
1 创建SpringBoot项目
1.1 选择创建新的项目
1.2 选择spring initializr,默认选择default
1.3 点击next,填写项目信息
1.4 点击next,选择web–>web
1.5 点击next,填写项目信息
1.6 点击finish,完成一个SpringBoot项目构建,项目结构如下图:
1.7 观察
2 添加依赖
点击pom.xml文件为其添加依赖,因为我之前有尝试过许多方案,添加了许多的依赖,这里写的已经删了一部分,剩下的应该都是能用的。注意下面依赖中的一句 <scope>provided</scope>
是要注释掉的。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3 添加webapp目录
3.1 添加webapp目录如图所示:
3.2 添加WEB-INF目录,如图所示:
jsp中存放视图
3.3 在resources下的application.properties中添加如下的配置:
spring.mvc.view.prefix= /WEB-INF/jsp/
spring.mvc.view.suffix= .jsp
3.4 设置Project Structure
4 创建User类
在java目录下创建包,在包下model目录用于存放JavaBean类。这里我们创建User类,实现Serializable接口。
package com.laodeng.model;
import java.io.Serializable;
public class User implements Serializable {
private String loginname;
private String username;
private String password;
public User(){}
public String getLoginname(){
return loginname;
}
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
public void setLoginname(String loginname){
this.loginname=loginname;
}
public void setUsername(String username){
this.username=username;
}
public void setPassword(String password){
this.password=password;
}
}
5 创建controllers目录用于存放控制器类
5.1 创建UserController类,具体代码如下:
package com.laodeng.controllers;
import com.laodeng.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping(value = "/user")
public class UserController {
private static List<User> userList;
public UserController(){
this.userList = new ArrayList<>();
}
@RequestMapping(value = "/register",method= RequestMethod.GET)
public String registerForm(){
return "/user/registerForm";
}
@RequestMapping(value = "/register",method = RequestMethod.POST)
public String register(@RequestParam("loginname") String loginname,@RequestParam("username") String username,
@RequestParam("password") String password){
User user = new User();
user.setLoginname(loginname);
user.setUsername(username);
user.setPassword(password);
userList.add(user);
return "/user/loginForm";
}
@RequestMapping(value = "login")
public String login(@RequestParam("loginname") String loginname,
@RequestParam("password") String password,Model model){
for(User user:userList){
if(user.getLoginname().equals(loginname)&&
user.getPassword().equals(password)){
model.addAttribute("user",user);
return "/user/welcome";
}
}
return "/user/loginForm";
}
}
6 创建视图
6.1 创建注册页面registerForm.jsp文件,文件路径:webapp\WEB-INF\jsp\user\registerForm.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<form action="register" method="post">
<table>
<tr>
<td><label>登录名:</label></td>
<td><input type="text" id="loginname" name="loginname"> </td>
</tr>
<tr>
<td><label>密码:</label></td>
<td><input type="password" id="password" name="password"> </td>
</tr>
<tr>
<td><label>真实姓名:</label></td>
<td><input type="text" id="username" name="username"> </td>
</tr>
<tr>
<td><input type="submit" id="submit" value="注册"> </td>
</tr>
</table>
</form>
</body>
</html>
6.2 创建登录页面loginForm.jsp文件,路径为:webapp\WEB-INF\jsp\user\loginForm.jsp.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="login" method="post">
<table>
<tr>
<td><label>登录名:</label></td>
<td><input type="text" id="loginname" name="loginname"> </td>
</tr>
<tr>
<td><label>密码:</label></td>
<td><input type="password" id="password" name="password"> </td>
</tr>
<tr>
<td><input type="submit" id="submit" value="登录"> </td>
</tr>
</table>
</form>
</body>
</html>
6.3 创建登录成功页面welcome.jsp文件,路径为:webapp\WEB-INF\jsp\user\welcome.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎页面</title>
</head>
<body>
<h1>欢迎${requestScope.user.username}</h1>
</body>
</html>
7 配置启动类
Test202Application类继承SpringBootServletInitializer
package com.laodeng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import javax.faces.application.Application;
@SpringBootApplication
public class Test202Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Test202Application.class, args);
}
}
8 运行结果
ps:这是本人第一次写博客,博客中许多地方没有解释也请各位老板多多担待。为了完成本次在idea下SpringBoot项目使用SpringMVC和jsp花了我不少功夫和踩了不少的坑。这里我推荐三个博客,我也是主要参照这三篇博客进行整合迈出了这对我这个小白来说极其不容易的一步,我博客中没有说明的代码部分在这三篇博客都有提到,也请各位老板移步到这三篇博客更深入的理解代码。
链接1: SpringMVC-005】idea创建SpringMVC项目傻瓜教程-简单注册登录.
链接2: Intellij + spring boot + spring MVC创建helloworld示例完整步骤(附代码).
链接3: 使用idea整合 spring boot 和jsp详细教程.
本文地址:https://blog.csdn.net/KaijieDeng/article/details/109273119