- 环境: MacOS + IntelliJ IDEA 2019.3.1 (Ultimate Edition)
基于Java Web项目单体架构,创建登录模块。
-
开发顺序:
1、 entity 实体类
2、dao 数据访问层
3、service 业务逻辑
4、view 视图层 -
开发示例:
1、在entity目录下创建实体类User
package com.coding.demo.entity;
/**
* @author 码出高薪
* @date 2020/1/8
*/
public class User {
private String name;
private String password;
public User(String name,String password){
this.name = name;
this.password = password;
}
public void setName(String name){
this.name = name;
}
public void setPassword(String password){
this.password = password;
}
public String getName(){
return this.name;
}
public String getPassword(){
return this.password;
}
@Override
public String toString() {
return "User{" +
"name=\'" + name + \'\\\'\' +
", password=\'" + password + \'\\\'\' +
\'}\';
}
}
2、在dao目录下创建IUserDao接口
package com.coding.demo.dao;
/**
* 用户数据访问
* @author 码出高薪
* @date 2020/1/8
*/
public interface IUserDao {
/**
* 用户登录
* @param name 用户名
* @param password 密码
* @return User 用户
*/
public User login(String name,String password);
}
3、在dao目录下创建存放数据访问层接口实现类的文件夹
mkdir -p ~/Documents/test/demo/src/main/java/com/coding/demo/dao/impl
4、在impl文件夹创建UserDao接口的实现类UserDaoImpl
package com.coding.demo.dao.impl;
import com.coding.demo.dao.IUserDao;
import com.coding.demo.entity.User;
/**
* 用户数据访问
* @author 码出高薪
* @date 2020/1/8
*/
public class UserDaoImpl implements IUserDao {
// 已知用户名和密码
private String username = "Jack";
private String password = "1234";
/**
* 用户登录
* @param username 用户名
* @param password 密码
* @return User 用户
*/
public User login(String username, String password) {
User user = null;
// 根据用户名查找用户信息
if(this.username.equals(username)){
if(this.password.equals(password)){
user = new User();
user.setUsername(username);
user.setPassword(password);
}
}
return user;
}
}
5、在service目录下创建IUserLoginService接口
package com.coding.demo.service;
import com.coding.demo.entity.User;
/**
* 处理登录业务逻辑
* @author 码出高薪
* @date 2020/1/8
*/
public interface IUserLoginService {
/**
* 登录
* @param username 用户名
* @param password 密码
* @return User 用户
*/
User login(String username, String password);
}
6、在service目录下创建存放业务逻辑层接口实现类的文件夹
mkdir -p ~/Documents/test/demo/src/main/java/com/coding/demo/service/impl
7、在impl文件夹创建IUserLoginService接口的实现类
package com.coding.demo.service;
import com.coding.demo.entity.User;
/**
* 实现登录业务逻辑接口
* @author 码出高薪
* @date 2020/1/8
*/
public interface IUserLoginService {
/**
* 登录
* @param username
* @param password
* @return
*/
User login(String username, String password);
}
8、在web文件夹创建controller
mkdir -p ~/Documents/test/demo/src/main/java/com/coding/demo/web/controller
9、在controller 文件夹下创建UserLoginController
package com.coding.demo.web.controller;
import com.coding.demo.entity.User;
import com.coding.demo.service.IUserLoginService;
import com.coding.demo.service.impl.IUserLoginServiceImpl;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 登录控制器
* @author 码出高薪
* @date 2020/1/8
*/
public class UserLoginController extends HttpServlet {
private IUserLoginService userService = new IUserLoginServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
User user = userService.login(username, password);
// 登录失败处理
if(user == null){
req.getRequestDispatcher("/fail.jsp").forward(req,resp);
}
// 登录成功处理
else {
req.getRequestDispatcher("/success.jsp").forward(req,resp);
}
}
}
10、创建WEB-INF
mkdir -p /Users/liujia/Documents/test/demo/src/main/webapp/WEB-INF
11、在WEB-INF 目录下创建web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>demo</display-name>
<servlet>
<servlet-name>LoginController</servlet-name>
<servlet-class>com.coding.demo.web.controller.UserLoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
12、在webapp 目录下创建index.jsp
<%--
Created by IntelliJ IDEA.
User: 码出高薪
Date: 2020/1/8
Time: 11:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<form action="/login" method="post" >
用户名:<input name="username" type="text" /> <br>
密码:<input name="password" type="password" /><br>
<input type="submit" value="登录"/>
</form>
</body>
</html>
13、在webapp 目录下创建success.jsp
<%--
Created by IntelliJ IDEA.
User: 码出高薪
Date: 2020/1/8
Time: 12:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
登录成功
</body>
</html>
14、在webapp 目录下创建fail.jsp
<%--
Created by IntelliJ IDEA.
User: 码出高薪
Date: 2020/1/8
Time: 12:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录失败</title>
</head>
<body>
登录失败
</body>
</html>
15、运行,打开http://localhost:8080