原文:Intellij IDEA 构建Spring Web项目 — 用户登录功能
相关软件:
1、Intellij IDEA14:http://pan.baidu.com/s/1nu16VyD
2、JDK7:http://pan.baidu.com/s/1dEstJ5f
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
5、Spring 3.x : http://pan.baidu.com/s/1o7i8daM
6:相关Lib打包:http://pan.baidu.com/s/1ntUxjLJ
案例来自于《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
命名为:springwebdemo_3
2、手动创建以下目录
其中test为测试目录,可以先创建一个文件夹为test,然后右键,选择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 baobaotao.domain; import java.io.Serializable; import java.util.Date; /** * Created by gao on 16-3-17. */ 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 baobaotao.domain; import java.io.Serializable; import java.util.Date; /** * Created by gao on 16-3-17. */ 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; } public LoginLog() { } }
7、构建持久层
UserDao:
package baobaotao.dao; import baobaotao.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; /** * Created by gao on 16-3-17. */ @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 baobaotao.dao; import baobaotao.domain.LoginLog; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; /** * Created by gao on 16-3-17. */ @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 baobaotao.service; import baobaotao.dao.LoginLogDao; import baobaotao.dao.UserDao; import baobaotao.domain.LoginLog; import baobaotao.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Created by gao on 16-3-17. */ @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文件
这里和原来的文件有些差别,因为貌似好像书本里面的源文件运行不了~so,我就改了~
在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" 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"> <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 --> <context:annotation-config/> <context:component-scan base-package="baobaotao.dao"/> <context:component-scan base-package="baobaotao.service"/> <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/sampledb"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <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" /> </beans>
10、编写单元测试
这里使用Junit4编写单元测试,在test文件夹下的baobaotao.service包创建类TestUserService。
package baobaotao.service; import baobaotao.domain.User; 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.assertEquals; import static org.junit.Assert.assertTrue; /** * Created by gao on 16-3-17. */ @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目录下创建包baobaotao.web,在该包下创建类LoginCommand和类LoginController。
LoginCommand:
package baobaotao.web; /** * Created by gao on 16-3-17. */ 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 baobaotao.web; import baobaotao.domain.User; import baobaotao.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.util.Date; /** * Created by gao on 16-3-17. */ @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视图页面
web文件夹下本来有一个index.jsp,现在再创建login.jsp和main.jsp这两个文件。
index.jsp:
<%@ page contentType="text/html;charset=utf-8" %> <jsp:forward page="login.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"> <display-name>baobaotao</display-name> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </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="baobaotao.web"/> <!-- 配置视图解析器,将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 -> 点击“+” 号 -> 选择springwebdemo_3 ->OK
界面:
用户名:admin,密码:123456
登录成功进入主界面。