实现一个用户登陆的web界面,登陆成功后页面跳转并显示登陆用户名和积分(每次登陆加5分),登陆失败返回error
记录一下过程
1、在MySQL中建立名为sampledb的数据库,并新建两张表,分别命名t_user(存储用户信息)、t_login_log(存储登陆日志)
表内属性如下:
2、新建一个maven管理的web项目
3、pom.xml配置(因为功能比较简单最后没有使用junit进行测试)
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>6.0.6version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>5.0.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>5.0.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.0.3.RELEASEversion>
<scope>testscope>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
<version>1.5.8.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
<version>1.5.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>5.0.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.0.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.3.RELEASEversion>
dependency>
dependencies>
4、建立如下目录结构
5、LoginLogDao
package com.smart.dao;
import com.smart.domain.LoginLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class LoginLogDao {
private JdbcTemplate jdbcTemplate;
//保存登录日志SQL
private final static String INSERT_LOGIN_LOG_SQL = "INSERT INTO t_login_log (user_id,ip,login_datetime) VALUES (?,?,?)";
public void insertLoginLog(LoginLog loginLog){
Object[] args = {loginLog.getUserId(),loginLog.getIp(),loginLog.getLoginDate()};
jdbcTemplate.update(INSERT_LOGIN_LOG_SQL, args);
}
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
}
6、UserDao
package com.smart.dao;
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;
import java.sql.ResultSet;
import java.sql.SQLException;
@Repository
public class UserDao {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
/**
* 根据用户名和密码获取匹配的用户
* @param userName 用户名
* @param password 密码
* @return 1表示用户名密码正确,0表示用户名密码错误
*/
public int getMatchCount(String userName,String password){
String sqlStr = "SELECT count(*) FROM t_user WHERE user_name=? and password=?";
return jdbcTemplate.queryForObject(sqlStr, new Object[]{userName, password},Integer.class);
}
private final static String MATCH_COUNT_SQL = "SELECT * FROM t_user WHERE user_name=?";
private final static String UPDATE_LOGIN_INFO_SQL = "UPDATE t_user SET last_visit=?,last_ip=?,credits=? WHERE user_id=?";
/**
* 通过用户名获得用户对象
* @param userName 用户名
* @return user对象
*/
public User findUserByUserName(final String userName){
final User user = new User();
jdbcTemplate.query(MATCH_COUNT_SQL, new Object[]{userName},
new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
user.setUserId(rs.getInt("user_id"));
user.setUserName(userName);
user.setCredits(rs.getInt("credits"));
}
});
return user;
}
/**
* 更新用户积分、最后登录IP、最后登录时间
* @param user
*/
public void updateLogeinInfo(User user){
jdbcTemplate.update(UPDATE_LOGIN_INFO_SQL, new Object[]{user.getLastVisit(), user.getLastIP(), user.getCredits(), user.getUserId()});
}
}
7、LoginLog
关于Serializable参考文章
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 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;
}
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;
}
}
8、User
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 int credits;
private String lastIP;
private Date 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;
}
public int getCredits() {
return credits;
}
public void setCredits(int credits) {
this.credits = credits;
}
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;
}
}
9、UserService
package com.smart.service;
import com.smart.dao.LoginLogDao;
import com.smart.dao.UserDao;
import com.smart.domain.LoginLog;
import com.smart.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserService {
private UserDao userDao;
private LoginLogDao loginLogDao;
@Autowired
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
@Autowired
public void setLoginLogDao(LoginLogDao loginLogDao){
this.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);
}
@Transactional
public void loginSuccess(User user){
user.setCredits(5+user.getCredits());
LoginLog loginLog = new LoginLog();
loginLog.setUserId(user.getUserId());
loginLog.setIp(user.getLastIP());
loginLog.setLoginDate(user.getLastVisit());
userDao.updateLogeinInfo(user);
loginLogDao.insertLoginLog(loginLog);
}
}
10、LoginCommand
package com.smart.web;
public class LoginCommand {
private String userName;
private String password;
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;
}
}
11、LoginController
package com.smart.web;
import com.smart.domain.User;
import com.smart.service.UserService;
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 javax.servlet.http.HttpServletRequest;
import java.util.Date;
//spring MVC的Controller
@Controller
public class LoginController {
private UserService userService;
//处理静态网页/index.html请求
@RequestMapping(value = "/index.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","用户名或密码错误。");
return new ModelAndView("logError");
}
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");
}
}
@Autowired
public void setUserService(UserService userService){
this.userService = userService;
}
}
12、smart-context(ApplicationContext文件)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<context:component-scan base-package="com.smart.dao"/>
<context:component-scan base-package="com.smart.service"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" primary="true">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/sampledb?serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="123456" />
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod" expression="(execution(* com.smart.service..*(..))) and
(@annotation(org.springframework.transaction.annotation.Transactional))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
tx:attributes>
tx:advice>
beans>
13、smart-servlet
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.smart.web"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
bean>
beans>
14、web.xml配置
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:smart-context.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
listener>
<servlet>
<servlet-name>smartservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:smart-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>smartservlet-name>
<url-pattern>*.htmlurl-pattern>
servlet-mapping>
web-app>
15、login.jsp 登陆界面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>logintitle>
<script>
function checkUser(){
var result = document.getElementById("userid").value;
var password = document.getElementById("userpassid").value;
if(result == "" ){
alert("用户名不能为空");
return false;
}
if(password == "" ){
alert("密码不能为空");
return false;
}
// document.getElementById("check").onsubmit();
}
script>
head>
<body>
<%--if test="${!empty error}">--%>
<%--"red">"%{error}" />--%>
<%--if>--%>
<form action="loginCheck.html " />" method="post" onsubmit="return checkUser()">
<table align="center" >
<tr>
<td>用户名:td><td><input type="text" name="userName" id="userid" />td>
tr>
<tr>
<td>密码:td><td><input type="password" name="password" id="userpassid" />td>
<tr/>
<tr>
<td colspan="2",align="center">
<input type="submit" value="登录" id="check" />
<input type="reset" value="重置" />
td>
tr>
table>
form>
body>
html>
16、logError.jsp 登陆信息错误,提示返回
<%--
Created by IntelliJ IDEA.
User: like
Date: 2018/3/6
Time: 16:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>errortitle>
head>
<body>
<form action="index.html " />" method="post">
<table align="center" >
<tr>
<td>errortd>
tr>
<tr>
<td colspan="2",align="center">
<input type="submit" value="返回" />
td>
tr>
table>
form>
body>
html>
17、main.jsp 登陆成功
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>你好title>
head>
<body>
${user.userName},当前积分为${user.credits};
body>
html>
18、Tomcat配置
19、运行结果