From 4aff06b733102031cbb819ca80384b6655cc0687 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=93=81=E9=AA=A8=E9=93=AE=E9=93=AE=E5=A2=99=E5=A4=B4?=
=?UTF-8?q?=E8=8D=89?= <1358159216@qq.com>
Date: Sun, 11 Apr 2021 18:46:30 +0800
Subject: [PATCH 1/4] =?UTF-8?q?dao=E5=B1=82=E5=92=8Cdomain=E7=9A=84?=
=?UTF-8?q?=E7=BC=96=E5=86=99?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
chapter1/chapter1.iml | 48 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)
create mode 100644 chapter1/chapter1.iml
diff --git a/chapter1/chapter1.iml b/chapter1/chapter1.iml
new file mode 100644
index 0000000..8e8594b
--- /dev/null
+++ b/chapter1/chapter1.iml
@@ -0,0 +1,48 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
--
Gitee
From ac49c87e97fbbb94e179739beb5e7e487443edff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=93=81=E9=AA=A8=E9=93=AE=E9=93=AE=E5=A2=99=E5=A4=B4?=
=?UTF-8?q?=E8=8D=89?= <1358159216@qq.com>
Date: Mon, 12 Apr 2021 21:20:45 +0800
Subject: [PATCH 2/4] =?UTF-8?q?service=E5=92=8Cweb=E7=BC=96=E5=86=99?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../main/java/com/smart/dao/LoginLogDao.java | 21 ++++++
.../src/main/java/com/smart/dao/UserDao.java | 46 ++++++++++++
.../main/java/com/smart/domain/LoginLog.java | 47 ++++++++++++
.../src/main/java/com/smart/domain/User.java | 57 +++++++++++++++
.../java/com/smart/service/UserService.java | 37 ++++++++++
.../main/java/com/smart/web/LoginCommand.java | 24 +++++++
.../java/com/smart/web/LoginController.java | 45 ++++++++++++
chapter1/src/main/webapp/WEB-INF/jsp/main.jsp | 13 ++++
chapter1/src/main/webapp/WEB-INF/web.xml | 29 ++++++++
.../test/java/com/smart/dao/UserDaoTest.java | 36 ++++++++++
.../com/smart/service/UserServiceTest.java | 45 ++++++++++++
.../com/smart/web/LoginControllerTest.java | 71 +++++++++++++++++++
12 files changed, 471 insertions(+)
create mode 100644 chapter1/src/main/java/com/smart/dao/LoginLogDao.java
create mode 100644 chapter1/src/main/java/com/smart/dao/UserDao.java
create mode 100644 chapter1/src/main/java/com/smart/domain/LoginLog.java
create mode 100644 chapter1/src/main/java/com/smart/domain/User.java
create mode 100644 chapter1/src/main/java/com/smart/service/UserService.java
create mode 100644 chapter1/src/main/java/com/smart/web/LoginCommand.java
create mode 100644 chapter1/src/main/java/com/smart/web/LoginController.java
create mode 100644 chapter1/src/main/webapp/WEB-INF/jsp/main.jsp
create mode 100644 chapter1/src/main/webapp/WEB-INF/web.xml
create mode 100644 chapter1/src/test/java/com/smart/dao/UserDaoTest.java
create mode 100644 chapter1/src/test/java/com/smart/service/UserServiceTest.java
create mode 100644 chapter1/src/test/java/com/smart/web/LoginControllerTest.java
diff --git a/chapter1/src/main/java/com/smart/dao/LoginLogDao.java b/chapter1/src/main/java/com/smart/dao/LoginLogDao.java
new file mode 100644
index 0000000..d276058
--- /dev/null
+++ b/chapter1/src/main/java/com/smart/dao/LoginLogDao.java
@@ -0,0 +1,21 @@
+package com.smart.dao;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Repository;
+
+import com.smart.domain.LoginLog;
+
+@Repository
+public class LoginLogDao {
+ @Autowired
+ private JdbcTemplate jdbcTemplate;
+
+ public void insertLoginLog(LoginLog loginLog) {
+ String sqlStr = "INSERT INTO t_login_log(user_id,ip,login_datetime) "
+ + "VALUES(?,?,?)";
+ Object[] args = {loginLog.getUserId(), loginLog.getIp(),
+ loginLog.getLoginDate()};
+ jdbcTemplate.update(sqlStr, args);
+ }
+}
\ No newline at end of file
diff --git a/chapter1/src/main/java/com/smart/dao/UserDao.java b/chapter1/src/main/java/com/smart/dao/UserDao.java
new file mode 100644
index 0000000..8e36bb8
--- /dev/null
+++ b/chapter1/src/main/java/com/smart/dao/UserDao.java
@@ -0,0 +1,46 @@
+package com.smart.dao;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.RowCallbackHandler;
+import org.springframework.stereotype.Repository;
+
+import com.smart.domain.User;
+
+@Repository
+public class UserDao {
+
+ @Autowired
+ private JdbcTemplate jdbcTemplate;
+
+
+ public int getMatchCount(String userName, String password) {
+ String sqlStr = " SELECT count(*) FROM t_user "
+ + " WHERE user_name =? and password=? ";
+ return jdbcTemplate.queryForInt(sqlStr, new Object[]{userName, password});
+ }
+
+ public User findUserByUserName(final String userName) {
+ String sqlStr = " SELECT user_id,user_name "
+ + " FROM t_user WHERE user_name =? ";
+ final User user = new User();
+ jdbcTemplate.query(sqlStr, new Object[]{userName},
+ new RowCallbackHandler() {
+ public void processRow(ResultSet rs) throws SQLException {
+ user.setUserId(rs.getInt("user_id"));
+ user.setUserName(userName);
+ }
+ });
+ return user;
+ }
+
+ public void updateLoginInfo(User user) {
+ String sqlStr = " UPDATE t_user SET last_visit=?,last_ip=?"
+ + " WHERE user_id =?";
+ jdbcTemplate.update(sqlStr, new Object[]{user.getLastVisit(),
+ user.getLastIp(), user.getUserId()});
+ }
+}
diff --git a/chapter1/src/main/java/com/smart/domain/LoginLog.java b/chapter1/src/main/java/com/smart/domain/LoginLog.java
new file mode 100644
index 0000000..5c89fcb
--- /dev/null
+++ b/chapter1/src/main/java/com/smart/domain/LoginLog.java
@@ -0,0 +1,47 @@
+package com.smart.domain;
+
+import java.io.Serializable;
+import java.util.Date;
+
+public class LoginLog implements Serializable {
+ private int loginLogId;
+
+ private int userId;
+
+ private String ip;
+
+ private Date loginDate;
+
+ public String getIp() {
+ return ip;
+ }
+
+ public void setIp(String ip) {
+ this.ip = ip;
+ }
+
+ public Date getLoginDate() {
+ return loginDate;
+ }
+
+ public void setLoginDate(Date loginDate) {
+ this.loginDate = loginDate;
+ }
+
+ public int getLoginLogId() {
+ return loginLogId;
+ }
+
+ public void setLoginLogId(int loginLogId) {
+ this.loginLogId = loginLogId;
+ }
+
+ public int getUserId() {
+ return userId;
+ }
+
+ public void setUserId(int userId) {
+ this.userId = userId;
+ }
+
+}
diff --git a/chapter1/src/main/java/com/smart/domain/User.java b/chapter1/src/main/java/com/smart/domain/User.java
new file mode 100644
index 0000000..61eb641
--- /dev/null
+++ b/chapter1/src/main/java/com/smart/domain/User.java
@@ -0,0 +1,57 @@
+package com.smart.domain;
+
+import java.io.Serializable;
+import java.util.Date;
+
+public class User implements Serializable {
+ private int userId;
+
+ private String userName;
+
+ private String password;
+
+ private String lastIp;
+
+ private Date lastVisit;
+
+ public String getLastIp() {
+ return lastIp;
+ }
+
+ public void setLastIp(String lastIp) {
+ this.lastIp = lastIp;
+ }
+
+ public Date getLastVisit() {
+ return lastVisit;
+ }
+
+ public void setLastVisit(Date lastVisit) {
+ this.lastVisit = lastVisit;
+ }
+
+ public int getUserId() {
+ return userId;
+ }
+
+ public void setUserId(int userId) {
+ this.userId = userId;
+ }
+
+ 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;
+ }
+
+}
diff --git a/chapter1/src/main/java/com/smart/service/UserService.java b/chapter1/src/main/java/com/smart/service/UserService.java
new file mode 100644
index 0000000..28abe85
--- /dev/null
+++ b/chapter1/src/main/java/com/smart/service/UserService.java
@@ -0,0 +1,37 @@
+package com.smart.service;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.smart.dao.LoginLogDao;
+import com.smart.dao.UserDao;
+import com.smart.domain.LoginLog;
+import com.smart.domain.User;
+
+@Service
+public class UserService {
+
+ @Autowired
+ private UserDao userDao;
+
+ @Autowired
+ private LoginLogDao loginLogDao;
+
+ public boolean hasMatchUser(String userName, String password) {
+ int matchCount = userDao.getMatchCount(userName, password);
+ return matchCount > 0;
+ }
+
+ public User findUserByUserName(String userName) {
+ return userDao.findUserByUserName(userName);
+ }
+
+ public void loginSuccess(User user) {
+ LoginLog loginLog = new LoginLog();
+ loginLog.setUserId(user.getUserId());
+ loginLog.setIp(user.getLastIp());
+ loginLog.setLoginDate(user.getLastVisit());
+ loginLogDao.insertLoginLog(loginLog);
+ }
+
+}
diff --git a/chapter1/src/main/java/com/smart/web/LoginCommand.java b/chapter1/src/main/java/com/smart/web/LoginCommand.java
new file mode 100644
index 0000000..eda3655
--- /dev/null
+++ b/chapter1/src/main/java/com/smart/web/LoginCommand.java
@@ -0,0 +1,24 @@
+package com.smart.web;
+
+public class LoginCommand {
+
+ private String userName;
+
+ private String password;
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public void setUserName(String userName) {
+ this.userName = userName;
+ }
+}
diff --git a/chapter1/src/main/java/com/smart/web/LoginController.java b/chapter1/src/main/java/com/smart/web/LoginController.java
new file mode 100644
index 0000000..d147de2
--- /dev/null
+++ b/chapter1/src/main/java/com/smart/web/LoginController.java
@@ -0,0 +1,45 @@
+package com.smart.web;
+
+import java.util.Date;
+
+import javax.servlet.http.HttpServletRequest;
+
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.servlet.ModelAndView;
+
+import com.smart.domain.User;
+import com.smart.service.UserService;
+
+@Controller
+@RequestMapping(value = "/admin")
+public class LoginController {
+
+ @Autowired
+ private UserService userService;
+
+ @RequestMapping(value = "/login.html")
+ public String loginPage() {
+ return "login";
+ }
+
+ @RequestMapping(value = "/loginCheck.html")
+ public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand) {
+ boolean isValidUser =
+ userService.hasMatchUser(loginCommand.getUserName(),
+ loginCommand.getPassword());
+ if (!isValidUser) {
+ return new ModelAndView("login", "error", "用户名或密码错误。");
+ } else {
+ User user = userService.findUserByUserName(loginCommand
+ .getUserName());
+ user.setLastIp(request.getLocalAddr());
+ user.setLastVisit(new Date());
+ userService.loginSuccess(user);
+ request.getSession().setAttribute("user", user);
+ return new ModelAndView("main");
+ }
+ }
+}
diff --git a/chapter1/src/main/webapp/WEB-INF/jsp/main.jsp b/chapter1/src/main/webapp/WEB-INF/jsp/main.jsp
new file mode 100644
index 0000000..3233e23
--- /dev/null
+++ b/chapter1/src/main/webapp/WEB-INF/jsp/main.jsp
@@ -0,0 +1,13 @@
+<%@ page language="java" contentType="text/html; charset=UTF-8"
+ pageEncoding="UTF-8" %>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+
+
+
+
+ 景区网站管理
+
+
+${user.userName},欢迎您进入景区网站后台管理!
+
+
\ No newline at end of file
diff --git a/chapter1/src/main/webapp/WEB-INF/web.xml b/chapter1/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..eca96ae
--- /dev/null
+++ b/chapter1/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,29 @@
+
+
+
+ contextConfigLocation
+ classpath:applicationContext.xml
+
+
+
+ org.springframework.web.context.ContextLoaderListener
+
+
+
+
+ viewspace
+
+ org.springframework.web.servlet.DispatcherServlet
+
+ 3
+
+
+
+ viewspace
+ *.html
+
+
diff --git a/chapter1/src/test/java/com/smart/dao/UserDaoTest.java b/chapter1/src/test/java/com/smart/dao/UserDaoTest.java
new file mode 100644
index 0000000..03e2c44
--- /dev/null
+++ b/chapter1/src/test/java/com/smart/dao/UserDaoTest.java
@@ -0,0 +1,36 @@
+package com.smart.dao;
+
+
+import com.smart.domain.User;
+import com.smart.service.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
+import org.testng.annotations.Test;
+
+import java.util.Date;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+@ContextConfiguration(locations = {"/applicationContext.xml"})
+public class UserDaoTest extends AbstractTestNGSpringContextTests {
+
+ @Autowired
+ private UserDao userDao;
+
+ @Test
+ public void hasMatchUser() {
+ int count = userDao.getMatchCount("admin", "123456");
+ assertTrue(count>0);
+ }
+
+ @Test
+ public void findUserByUserName() {
+ User user = userDao.findUserByUserName("admin");
+ assertNotNull(user);
+ assertEquals(user.getUserName(), "admin");
+ }
+
+}
diff --git a/chapter1/src/test/java/com/smart/service/UserServiceTest.java b/chapter1/src/test/java/com/smart/service/UserServiceTest.java
new file mode 100644
index 0000000..4623bb6
--- /dev/null
+++ b/chapter1/src/test/java/com/smart/service/UserServiceTest.java
@@ -0,0 +1,45 @@
+package com.smart.service;
+
+
+import java.util.Date;
+
+
+import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
+import org.testng.annotations.*;
+
+import static org.testng.Assert.*;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import com.smart.domain.User;
+
+@ContextConfiguration(locations = {"/applicationContext.xml"})
+public class UserServiceTest extends AbstractTestNGSpringContextTests {
+
+ @Autowired
+ private UserService userService;
+
+ @Test
+ public void hasMatchUser() {
+ boolean b1 = userService.hasMatchUser("admin", "123456");
+ boolean b2 = userService.hasMatchUser("admin", "1111");
+ assertTrue(b1);
+ assertTrue(!b2);
+ }
+
+ @Test
+ public void findUserByUserName() {
+ User user = userService.findUserByUserName("admin");
+ assertEquals(user.getUserName(), "admin");
+ }
+
+ @Test
+ public void loginSuccess() {
+ User user = userService.findUserByUserName("admin");
+ user.setUserId(1);
+ user.setUserName("admin");
+ user.setLastIp("192.168.12.7");
+ user.setLastVisit(new Date());
+ userService.loginSuccess(user);
+ }
+}
diff --git a/chapter1/src/test/java/com/smart/web/LoginControllerTest.java b/chapter1/src/test/java/com/smart/web/LoginControllerTest.java
new file mode 100644
index 0000000..3353bd3
--- /dev/null
+++ b/chapter1/src/test/java/com/smart/web/LoginControllerTest.java
@@ -0,0 +1,71 @@
+package com.smart.web;
+
+import com.smart.domain.User;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
+import org.springframework.test.util.ReflectionTestUtils;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.servlet.HandlerMapping;
+import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+
+@ContextConfiguration(locations = {"classpath:applicationContext.xml","file:d:/actionSpring/chapter/chapter1/src/main/webapp/WEB-INF/viewspace-servlet.xml"})
+public class LoginControllerTest extends AbstractTestNGSpringContextTests {
+ @Autowired
+ private AnnotationMethodHandlerAdapter handlerAdapter;
+ @Autowired
+ private LoginController controller;
+ //声明Request与Response模拟对象
+ private MockHttpServletRequest request;
+ private MockHttpServletResponse response;
+
+ //执行测试前先初始模拟对象
+ @BeforeMethod
+ public void before() {
+ request = new MockHttpServletRequest();
+ request.setCharacterEncoding("UTF-8");
+ response = new MockHttpServletResponse();
+ request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true); //Spring3.1 存在的BUG
+
+ }
+
+ // 测试LoginController#loginCheck()方法
+ @Test
+ public void loginCheck() throws Exception {
+ //测试登陆成功的情况
+ request.setRequestURI("/admin/loginCheck.html");
+ request.addParameter("userName", "admin"); // 设置请求URL及参数
+ request.addParameter("password", "123456");
+
+ //向控制发起请求 ” /loginCheck.html”
+ ModelAndView mav = handlerAdapter.handle(request, response, controller);
+ User user = (User) request.getSession().getAttribute("user");
+ assertNotNull(mav);
+ assertEquals(mav.getViewName(), "main");
+ assertNotNull(user);
+ request.getSession().removeAttribute("user");
+
+ //测试登陆失败的情况
+ request.setRequestURI("/admin/loginCheck.html");
+ request.addParameter("userName", "test");
+ request.addParameter("password", "123456");
+
+ mav = handlerAdapter.handle(request, response, controller);
+ user = (User) request.getSession().getAttribute("user");
+ assertNotNull(mav);
+ assertEquals(mav.getViewName(), "login");
+ assertNull(user);
+
+ }
+
+
+
+}
--
Gitee
From c72fbac9611f75bb74dfac267c65992f2d6f2bd3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=93=81=E9=AA=A8=E9=93=AE=E9=93=AE=E5=A2=99=E5=A4=B4?=
=?UTF-8?q?=E8=8D=89?= <1358159216@qq.com>
Date: Mon, 12 Apr 2021 21:35:45 +0800
Subject: [PATCH 3/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20chap?=
=?UTF-8?q?ter1/src/test/java/com/smart?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../test/java/com/smart/dao/UserDaoTest.java | 36 ----------
.../com/smart/service/UserServiceTest.java | 45 ------------
.../com/smart/web/LoginControllerTest.java | 71 -------------------
3 files changed, 152 deletions(-)
delete mode 100644 chapter1/src/test/java/com/smart/dao/UserDaoTest.java
delete mode 100644 chapter1/src/test/java/com/smart/service/UserServiceTest.java
delete mode 100644 chapter1/src/test/java/com/smart/web/LoginControllerTest.java
diff --git a/chapter1/src/test/java/com/smart/dao/UserDaoTest.java b/chapter1/src/test/java/com/smart/dao/UserDaoTest.java
deleted file mode 100644
index 03e2c44..0000000
--- a/chapter1/src/test/java/com/smart/dao/UserDaoTest.java
+++ /dev/null
@@ -1,36 +0,0 @@
-package com.smart.dao;
-
-
-import com.smart.domain.User;
-import com.smart.service.UserService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
-import org.testng.annotations.Test;
-
-import java.util.Date;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertNotNull;
-import static org.testng.Assert.assertTrue;
-
-@ContextConfiguration(locations = {"/applicationContext.xml"})
-public class UserDaoTest extends AbstractTestNGSpringContextTests {
-
- @Autowired
- private UserDao userDao;
-
- @Test
- public void hasMatchUser() {
- int count = userDao.getMatchCount("admin", "123456");
- assertTrue(count>0);
- }
-
- @Test
- public void findUserByUserName() {
- User user = userDao.findUserByUserName("admin");
- assertNotNull(user);
- assertEquals(user.getUserName(), "admin");
- }
-
-}
diff --git a/chapter1/src/test/java/com/smart/service/UserServiceTest.java b/chapter1/src/test/java/com/smart/service/UserServiceTest.java
deleted file mode 100644
index 4623bb6..0000000
--- a/chapter1/src/test/java/com/smart/service/UserServiceTest.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package com.smart.service;
-
-
-import java.util.Date;
-
-
-import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
-import org.testng.annotations.*;
-
-import static org.testng.Assert.*;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.test.context.ContextConfiguration;
-import com.smart.domain.User;
-
-@ContextConfiguration(locations = {"/applicationContext.xml"})
-public class UserServiceTest extends AbstractTestNGSpringContextTests {
-
- @Autowired
- private UserService userService;
-
- @Test
- public void hasMatchUser() {
- boolean b1 = userService.hasMatchUser("admin", "123456");
- boolean b2 = userService.hasMatchUser("admin", "1111");
- assertTrue(b1);
- assertTrue(!b2);
- }
-
- @Test
- public void findUserByUserName() {
- User user = userService.findUserByUserName("admin");
- assertEquals(user.getUserName(), "admin");
- }
-
- @Test
- public void loginSuccess() {
- User user = userService.findUserByUserName("admin");
- user.setUserId(1);
- user.setUserName("admin");
- user.setLastIp("192.168.12.7");
- user.setLastVisit(new Date());
- userService.loginSuccess(user);
- }
-}
diff --git a/chapter1/src/test/java/com/smart/web/LoginControllerTest.java b/chapter1/src/test/java/com/smart/web/LoginControllerTest.java
deleted file mode 100644
index 3353bd3..0000000
--- a/chapter1/src/test/java/com/smart/web/LoginControllerTest.java
+++ /dev/null
@@ -1,71 +0,0 @@
-package com.smart.web;
-
-import com.smart.domain.User;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.mock.web.MockHttpServletRequest;
-import org.springframework.mock.web.MockHttpServletResponse;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
-import org.springframework.test.util.ReflectionTestUtils;
-import org.springframework.web.client.RestTemplate;
-import org.springframework.web.servlet.HandlerMapping;
-import org.springframework.web.servlet.ModelAndView;
-import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertNotNull;
-import static org.testng.Assert.assertNull;
-
-@ContextConfiguration(locations = {"classpath:applicationContext.xml","file:d:/actionSpring/chapter/chapter1/src/main/webapp/WEB-INF/viewspace-servlet.xml"})
-public class LoginControllerTest extends AbstractTestNGSpringContextTests {
- @Autowired
- private AnnotationMethodHandlerAdapter handlerAdapter;
- @Autowired
- private LoginController controller;
- //声明Request与Response模拟对象
- private MockHttpServletRequest request;
- private MockHttpServletResponse response;
-
- //执行测试前先初始模拟对象
- @BeforeMethod
- public void before() {
- request = new MockHttpServletRequest();
- request.setCharacterEncoding("UTF-8");
- response = new MockHttpServletResponse();
- request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true); //Spring3.1 存在的BUG
-
- }
-
- // 测试LoginController#loginCheck()方法
- @Test
- public void loginCheck() throws Exception {
- //测试登陆成功的情况
- request.setRequestURI("/admin/loginCheck.html");
- request.addParameter("userName", "admin"); // 设置请求URL及参数
- request.addParameter("password", "123456");
-
- //向控制发起请求 ” /loginCheck.html”
- ModelAndView mav = handlerAdapter.handle(request, response, controller);
- User user = (User) request.getSession().getAttribute("user");
- assertNotNull(mav);
- assertEquals(mav.getViewName(), "main");
- assertNotNull(user);
- request.getSession().removeAttribute("user");
-
- //测试登陆失败的情况
- request.setRequestURI("/admin/loginCheck.html");
- request.addParameter("userName", "test");
- request.addParameter("password", "123456");
-
- mav = handlerAdapter.handle(request, response, controller);
- user = (User) request.getSession().getAttribute("user");
- assertNotNull(mav);
- assertEquals(mav.getViewName(), "login");
- assertNull(user);
-
- }
-
-
-
-}
--
Gitee
From 9bc0ab3561cea7221f35e40a76c11346aa0f2b6c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=93=81=E9=AA=A8=E9=93=AE=E9=93=AE=E5=A2=99=E5=A4=B4?=
=?UTF-8?q?=E8=8D=89?= <1358159216@qq.com>
Date: Tue, 13 Apr 2021 14:25:08 +0800
Subject: [PATCH 4/4] ioc
---
IoC/src/ActorArrangable.java | 14 ++++++++++++++
IoC/src/MoAttack.java | 16 ++++++++++++++++
IoC/src/MoAttack1.java | 18 ++++++++++++++++++
3 files changed, 48 insertions(+)
create mode 100644 IoC/src/ActorArrangable.java
create mode 100644 IoC/src/MoAttack.java
create mode 100644 IoC/src/MoAttack1.java
diff --git a/IoC/src/ActorArrangable.java b/IoC/src/ActorArrangable.java
new file mode 100644
index 0000000..ea347aa
--- /dev/null
+++ b/IoC/src/ActorArrangable.java
@@ -0,0 +1,14 @@
+public class ActorArrangable {
+ void injectGeli(GeLi geli);
+
+}
+public class MoAttack implements ActorArrangable{
+ public GeLi geli;
+ public void injectGeli(GeLi geli){
+ this.geli = geli;
+
+ }
+ public void cityGateAsk(){
+ geli.responseAsk("墨者革离")
+ }
+}
diff --git a/IoC/src/MoAttack.java b/IoC/src/MoAttack.java
new file mode 100644
index 0000000..ffcf703
--- /dev/null
+++ b/IoC/src/MoAttack.java
@@ -0,0 +1,16 @@
+public class MoAttack {
+ private GeLi geli;
+ public MoAttack(GeLi geeli){
+ this.geli = geli;
+ }
+ public void cityGateAsk(){
+ geli.responseAsk("墨者革离!");
+ }
+}
+public class Director{
+ public void director(){
+ GEli geli =new LiuDeHua();
+ MoAttack moAttack = new MoAttack(geki);
+ moAttack.cityGateAsk();
+ }
+}
diff --git a/IoC/src/MoAttack1.java b/IoC/src/MoAttack1.java
new file mode 100644
index 0000000..f700b41
--- /dev/null
+++ b/IoC/src/MoAttack1.java
@@ -0,0 +1,18 @@
+public class MoAttack1 {
+ private GeLi;
+ public void setGeli(Geli geli){
+ this,geli = geli;
+ }
+ public void cityGateAsk(){
+ geli.responceAsk("墨者革离");
+ }
+}
+public class Director{
+ public void director(){
+ GeLi goli =new LiuDeHua();
+ MoAttack moAttack = new MoAttack();
+ moAttack.setGeli(gili);
+ moAttack.cityGateAsk();
+ }
+
+}
--
Gitee