Spring、Bootstrap简单练习(03/04)
Spring、Bootstrap简单练习(03/04) -
登录功能编码
User实体类
实现序列化接口
package com.hhhqqq.my.shop.entity;
import java.io.Serializable;
public class User implements Serializable {
private String username;
private String password;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
}
UserDao 接口
package com.hhhqqq.my.shop.dao;
import com.hhhqqq.my.shop.entity.User;
public interface UserDao {
/**
* 根据邮箱和密码返回用户
* @author HHQ
* @param email 邮箱
* @param password 密码
* @return com.hhhqqq.my.shop.entity.User 用户
*/
public User getUser(String email, String password);
}
UserDaoImol
实现UserDao接口
package com.hhhqqq.my.shop.dao.impl;
import com.hhhqqq.my.shop.dao.UserDao;
import com.hhhqqq.my.shop.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @ClassName UserDaoImpl
* @Description TODO
* @Author HHQ
* @Date 2020/3/30 14:03
* @Version 1.0
*/
public class UserDaoImpl implements UserDao {
private static final Logger logger = LoggerFactory.getLogger(UserDaoImpl.class);
public User getUser(String email, String password) {
logger.debug("调用 getUser, 参数为:email:{} password:{}", email, password);
User user = null;
if ("admin@qq.com".equals(email)) {
if ("admin".equals(password)) {
user = new User();
user.setEmail("admin@qq.com");
user.setUsername("HHQ");
logger.info("获取用户 成功 用户:{}", user.getUsername());
}
}
else {
logger.warn("获取用户 失败 用户:{}", email);
}
return user;
}
}
UserService 接口
package com.hhhqqq.my.shop.service;
import com.hhhqqq.my.shop.entity.User;
public interface UserService {
/**
* 登录
* @author HHQ
* @param email 邮箱
* @param password 密码
* @return com.hhhqqq.my.shop.entity.User 用户
*/
public User login(String email, String password);
}
注入UserDao
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- DAO -->
<bean id="userDao" class="com.hhhqqq.my.shop.dao.impl.UserDaoImpl"></bean>
</beans>
创建SpringContext获取bean实例
package com.hhhqqq.my.shop.commons.context;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 获取 bean 实例
* @ClassName SpringContext
* @Description TODO
* @Author HHQ
* @Date 2020/3/30 14:55
* @Version 1.0
*/
public final class SpringContext {
public Object getBean(String beanId){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
return context.getBean(beanId);
}
}
UserServiceImpl
实现UserService接口
package com.hhhqqq.my.shop.service.impl;
import com.hhhqqq.my.shop.commons.context.SpringContext;
import com.hhhqqq.my.shop.dao.UserDao;
import com.hhhqqq.my.shop.entity.User;
import com.hhhqqq.my.shop.service.UserService;
public class UserServiceImpl implements UserService {
public User login(String email, String password) {
SpringContext springContext = new SpringContext();
UserDao userDao = (UserDao) springContext.getBean("userDao");
return userDao.getUser(email, password);
}
}
注入UserService
web层控制器 要调用service层
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- DAO -->
<bean id="userDao" class="com.hhhqqq.my.shop.dao.impl.UserDaoImpl"></bean>
<!-- Service -->
<bean id="userService" class="com.hhhqqq.my.shop.service.impl.UserServiceImpl"></bean>
</beans>
LoginController.java
web/controller/LoginController.java
package com.hhhqqq.my.shop.web.controller;
import com.hhhqqq.my.shop.commons.context.SpringContext;
import com.hhhqqq.my.shop.entity.User;
import com.hhhqqq.my.shop.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
/**
* @ClassName LoginController
* @Description TODO
* @Author HHQ
* @Date 2020/3/30 15:00
* @Version 1.0
*/
public class LoginController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String email = req.getParameter("email");
String password = req.getParameter("password");
SpringContext springContext = new SpringContext();
UserService userService = (UserService) springContext.getBean("userService");
User user = userService.login(email, password);
if (user != null) {
resp.sendRedirect("/main.jsp");
}
else {
req.getRequestDispatcher("/index.jsp").forward(req,resp);
}
}
}
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">
<servlet>
<servlet-name>LoginController</servlet-name>
<servlet-class>com.hhhqqq.my.shop.web.controller.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
目录结构
了解:
埋点
静态域