Intellij IDEA 构建Spring Web项目 — 用户登录功能

相关软件:
1、Intellij IDEA14:http://pan.baidu.com/s/1nu16VyD
3、Tomcat(apache-tomcat-6.0.43):http://pan.baidu.com/s/1kUwReQF
4、MySQL(mysql-essential-5.1.68-winx64):http://pan.baidu.com/s/1gdZZgMB
6:相关Lib打包:http://pan.baidu.com/s/1slSmWqx
 
案例来自于《Spring 3.X 企业应用开发实战》 Chapter2 : http://pan.baidu.com/s/1qWXEE3E
 
1、创建一个普通的Java Web项目
Intellij IDEA创建一个普通的Java Web项目可参考:http://www.cnblogs.com/yangyquin/p/5285272.html
项目名称为:SpringProject,Module名称为SpringCh2LoginModule。
2、手动创建以下目录
在Module下先创建一个文件夹为test,与src同级,然后右键,选择Mark Directory As,再选择Test Sources Root即可。 
3、导入相关的jar包
我把Spring开发要用到的jar包都弄成一个文件夹了,把里面所有的jar包复制然后黏贴到项目里面的lib目录下即可。
4、配置项目结构
打开Project Structure(Ctrl+Alt+Shift+S),选择Modules,选择Paths,选择Use module compile output path,两个都选择为刚刚创建的classes文件夹。
接着看一下Modules下的Dependencies是否已经加入lib文件夹,如果没有,点击右边的“+”号,选择“Jars or Directories ”,选择刚刚创建的lib文件夹。如果已经加入则提个勾,表示选中。最后OK即可。
5、创建库表
我的数据库用户名为root,密码为123456。
创建库表的SQL语句可参见:\chapter2\schema。
创建数据库sampledb:
创建表t_user:
 
 创建表t_login_log:
 
插入数据:
 
 
6、构建主体层
用户领域对象:User
package com.yyq.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 User() {
    }
    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;
    }
}

登录日志领域对象:LoginLog.java

package com.yyq.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 LoginLog() {
    }
    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;
    }
}

7、构建持久层

UserDao:
package com.yyq.dao;
import com.yyq.domain.User;
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 java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public UserDao() {
        System.out.println("UserDao");
    }
    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,credits "
                + " 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);
                        user.setCredits(rs.getInt("credits"));
                    }
                });
        return user;
    }
    public void updateLoginInfo(User user) {
        String sqlStr = " UPDATE t_user SET last_visit=?,last_ip=?,credits=? "
                + " WHERE user_id =?";
        jdbcTemplate.update(sqlStr, new Object[]{user.getLastVisit(),
                user.getLastIp(), user.getCredits(), user.getUserId()});
    }
}

LoginLogDao:

package com.yyq.dao;
import com.yyq.domain.LoginLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@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);
    }
}

8、业务层

package com.yyq.service;
import com.yyq.dao.LoginLogDao;
import com.yyq.dao.UserDao;
import com.yyq.domain.LoginLog;
import com.yyq.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@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) {
        user.setCredits( 5 + user.getCredits());
        LoginLog loginLog = new LoginLog();
        loginLog.setUserId(user.getUserId());
        loginLog.setIp(user.getLastIp());
        loginLog.setLoginDate(user.getLastVisit());
        userDao.updateLoginInfo(user);
        loginLogDao.insertLoginLog(loginLog);
    }
}
9、配置applicationContext.xml文件
在src目录下创建一个applicationContext.xml文件,点中src,右键,选择New -> File  ->输入applicationContext.xml,确认即可。
<?xml version="1.0" encoding="UTF-8"?>
<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:p="http://www.springframework.org/schema/p"
       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.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <context:component-scan base-package="com.yyq.dao"/>
    <context:component-scan base-package="com.yyq.service"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"
          p:driverClassName="com.mysql.jdbc.Driver"
          p:url="jdbc:mysql://localhost:3306/sampledb"
          p:username="root"
          p:password="123456"/>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
          p:dataSource-ref="dataSource"/>
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          p:dataSource-ref="dataSource"/>
    
    <aop:config proxy-target-class="true">
        <aop:pointcut id="serviceMethod" expression="execution( * com.yyq.service..*(..))"/>
        <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>

10、编写单元测试

这里使用Junit4编写单元测试,在test文件夹下创建类com.yyq.TestUserService。
package com.yyq;
import com.yyq.domain.User;
import com.yyq.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Date;
import static org.junit.Assert.*;
/**
 * Created by gao on 16-4-28.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
public class TestUserService {
    @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 testFindUserByUserName() {
        User user = userService.findUserByUserName("admin");
        assertEquals(user.getUserName(), "admin");
    }
    @Test
    public void testAddLoginLog() {
        User user = userService.findUserByUserName("admin");
        user.setUserId(1);
        user.setUserName("admin");
        user.setLastIp("192.168.12.7");
        user.setLastVisit(new Date());
        userService.loginSuccess(user);
    }
}

11、运行测试用例

把鼠标放在某个测试方法,比如hasMatchUser()方法,右键,选择Run 'hasMatchUser()',即可运行特定的方法。当然,也可以把鼠标放在类名那里,右键,选择“TestUserService”,即将类里面的所有方法都运行一遍。
 
看到”All Tests Passed“,表示全部通过啦~~~
 
12、控制层
在src目录下创建包com.yyq.service,在该包下创建类LoginCommand和类LoginController。
LoginCommand:
package com.yyq.controller;

/**
 * Created by gao on 16-4-28.
 */
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;
    }
}

LoginController:

package com.yyq.controller;

import com.yyq.domain.User;
import com.yyq.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;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @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", "用户名或密码错误。");
        } 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");
        }
    }
}

13、编写JSP视图页面

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>宝宝淘论坛登录</title>
</head>
<body>
<c:if test="${!empty error}">
    <font color="red"><c:out value="${error}" /></font>
</c:if>
<form action="<c:url value="/loginCheck.html"/>" method="post">
    用户名:
    <input type="text" name="userName">
    <br>
    密 码:
    <input type="password" name="password">
    <br>
    <input type="submit" value="登录" />
    <input type="reset" value="重置" />
</form>
</body>
</html>

main.jsp:

<%--
  Created by IntelliJ IDEA.
  User: gao
  Date: 16-3-17
  Time: 下午8:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>宝宝淘论坛</title>
</head>
<body>
${user.userName},欢迎您进入宝宝淘论坛,您当前积分为${user.credits};
</body>
</html>
14、配置Spring MVC框架
1)修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>baobaotao</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>baobaotao</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>

2)在WEB-INF下创建一个文件baobaotao-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd">
    <!-- 扫描web包,应用Spring的注解 -->
    <context:component-scan base-package="com.yyq.controller"/>
    <!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:viewClass="org.springframework.web.servlet.view.JstlView"
          p:prefix="/"
          p:suffix=".jsp"/>
</beans>

 

15、配置Tomcat容器,运行程序

Run -> Edit Configuration -> Run/Debug Configurations -> 点击"+"号 -> 选择Tomcat -> 选择Local -> 点击Deployment -> 点击“+” 号 -> 选择SpringCh2LoginModule:war exploded->OK
界面显示:
用户名:admin,密码:123456

 

登录成功进入主界面。

16、项目结构图
 
 
 
posted @ 2016-03-17 21:48  我是一名老菜鸟  阅读(60902)  评论(6编辑  收藏  举报