CRM后台开发【Customer Relation Manager ---- 客户关系管理 ---- 管理员登录功能实现】
1、创建一个Web Project 工程,其Project Name 为:CRM,并在src目录下创建如下包:
cn.chuang.crm.pojo
cn.chuang.crm.web
cn.chuang.crm.service
cn.chuang.crm.dao
cn.chuang.crm.util
cn.chuang.crm.config
2、创建crm数据库,以及admins表,插入用户名和密码
// 创建CRM数据库
drop database if exists crm;
create database if not exists crm;
// 创建admin表
drop table if exists admins;
create table if not exists admins(
id int(5) auto_increment,
username varchar(10) not null,
password varchar(32) not null,
primary key(id)
);
//使用该数据库
use crm;
// 插入用户名和密码
insert into admins(username,password) values('黄伟创',md5('123123'));
3、在cn.chuang.crm.pojo包下创建Admin.java
/**
* @author chuang
* 管理员模块
*/
public class Admin {
private Integer id;
private String username;
private String password;
public Admin(){}
public Admin(String username, String password){
this.username = username;
this.password = password;
}
......
4、在cn.chuang.crm.config包下创建Admin.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.chuang.crm.pojo.Admin" table="admin">
<id name="id" type="integer">
<column name="id" />
<generator class="native" />
</id>
<property name="username" type="string">
<column name="username" length="10" not-null="true" />
</property>
<property name="password" type="string">
<column name="password" length="32" not-null="true" />
</property>
</class>
</hibernate-mapping>
5、在cn.chuang.crm.util包下创建BaseDao.java
package cn.chuang.crm.util;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
/**
*
* @author Administrator
* 泛型Dao父类
* 注意:该类中不能出现具体类型
*/
public class BaseDao<T> {
private HibernateTemplate ht;
private Class clazz;
public BaseDao(){
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
clazz = (Class) type.getActualTypeArguments()[0];
}
public void setHt(HibernateTemplate ht){
this.ht = ht;
}
public HibernateTemplate getHt(){
return ht;
}
public void add(T t){
ht.save(t);
}
public void update(T t){
ht.update(t);
}
public void delete(T t){
ht.delete(t);
}
public void deleteAll(List<T> tList){
ht.deleteAll(tList);
}
public T findById(Integer id){
return (T) ht.get(clazz, id);
}
public List<T> findAll(){
String hql = "FROM " + clazz.getName();
return ht.find(hql);
}
public List<T> findAllByFy(final Integer start, final Integer size){
final String hql = "FROM " + clazz.getName();
return (List<T>) ht.execute(new HibernateCallback<List<T>>() {
@Override
public List<T> doInHibernate(Session session) throws HibernateException,SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(start);
query.setMaxResults(size);
return query.list();
}
});
}
}
6、在cn.chuang.crm.util包下创建MD5Util.java
package cn.chuang.crm.util;
import java.security.MessageDigest;
/**
*
* @author Administrator
* 将明文密码用Java中的MD5算法加密
*/
public final class MD5Util {
public static String encode(String password) throws Exception{
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] byteArray = md5.digest(password.getBytes());
String passwordMD5 = byteArrayToHexString(byteArray);
return passwordMD5;
}
private static String byteArrayToHexString(byte[] byteArray){
StringBuffer sb = new StringBuffer();
for(byte b : byteArray){
String hexString = byteToHexString(b);
sb.append(hexString);
}
return sb.toString();
}
private static String byteToHexString(byte b){
int n = b;
if(n < 0){
n += 256;
}
int d1 = n/16;
int d2 = n%16;
return hex[d1] + hex[d2];
}
private static String hex[] = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
}
7、在cn.chuang.crm.dao包下创建AdminDao.java
package cn.chuang.crm.dao;
import java.util.List;
import cn.chuang.crm.pojo.Admin;
import cn.chuang.crm.util.BaseDao;
import cn.chuang.crm.util.MD5Util;
/**
* @author Administrator
* 管理层模块--持久层
*/
public class AdminDao extends BaseDao<Admin> {
/**
* 根据用户名和密码实现登录功能
* @throws Exception
*/
public Admin login(String username, String password) throws Exception{
String hql = "FROM Admin a WHERE a.username = ? and a.password = ?";
String passwordMD5 = MD5Util.encode(password);
Object[] params = {username,passwordMD5};
List<Admin> adminList = this.getHt().find(hql,params);
if(adminList != null && adminList.size() > 0){
return adminList.get(0);
}else{
return null;
}
}
}
8、在cn.chuang.crm.util包下创建spring-base.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置C3P0数据源 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/crm"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置LocalSessionFactoryBean -->
<bean id="localSessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="comboPooledDataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>cn/chuang/crm/config/Admin.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="localSessionFactoryBean"/>
</bean>
<!-- Hibernate事务管理器 -->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="localSessionFactoryBean" />
</bean>
<!-- 配置事务增强 -->
<tx:advice id="txadvice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="add"/>
</tx:attributes>
</tx:advice>
<!-- 配置AOP -->
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* cn.chuang.crm.service.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pointCut"/>
</aop:config>
</beans>
9、在cn.chuang.crm.service包下创建adminService.java
package cn.chuang.crm.service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.chuang.crm.dao.AdminDao;
import cn.chuang.crm.pojo.Admin;
/**
* @author Administrator
* 管理员模块--业务层
*/
public class AdminService {
private AdminDao adminDao;
public void setAdminDao(AdminDao adminDao){
this.adminDao = adminDao;
}
/**
* 管理员登录
* @throws Exception
*/
public Admin login(Admin admin) throws Exception{
if(admin != null){
try {
return adminDao.login(admin.getUsername(), admin.getPassword());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}else{
throw new RuntimeException("AdminService::login::admin参数为空");
}
}
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"cn/chuang/crm/util/spring-base.xml","cn/chuang/crm/config/spring-admin.xml"});
AdminService adminService = (AdminService) ac.getBean("adminService");
Admin admin = adminService.login(new Admin("黄伟创","123123"));
if(admin != null){
System.out.println("登陆成功!");
}else{
System.out.println("登录失败!");
}
}
}
10、在cn.chuang.crm.web包下创建AdminAction.java
package cn.chuang.crm.web;
import java.util.Map;
import cn.chuang.crm.pojo.Admin;
import cn.chuang.crm.service.AdminService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author Administrator
* 管理员模块--表现层--控制器
*/
public class AdminAction extends ActionSupport {
private Admin admin;
private AdminService adminService;
private String checkNum;
public void setAdmin(Admin admin){
this.admin = admin;
}
public Admin getAdmin(){
return admin;
}
public void setAdminService(AdminService adminService){
this.adminService = adminService;
}
public void setCheckNum(String checkNum){
this.checkNum = checkNum;
}
/**
* 管理员登录
* @throws Exception
*/
public String loginMethod() throws Exception{
//获取用户的HttpSession对象
Map<String, Object> httpSessionMap = ActionContext.getContext().getSession();
//获取HttpSession中的验证码
String checkNumSession = (String) httpSessionMap.get("CHECKNUM");
//如果客户端验证码不为空
if(checkNumSession != null){
//验证码正确
if(checkNum.equals(checkNumSession)){
Admin realAdmin = adminService.login(admin);
if(realAdmin != null){
httpSessionMap.put("admin", realAdmin);
//将位于HttpSessionMap中的验证码删除
httpSessionMap.remove("CHECKNUM");
return "toMainJsp";
}else{
httpSessionMap.put("message", "管理员登录失败");
//将位于HttpSessionMap中的验证码删除
httpSessionMap.remove("CHECKNUM");
return "toErrorJsp";
}
}else{
//验证码不正确
httpSessionMap.put("message", "验证码输入不正确");
//将位于HttpSessionMap中的验证码删除
httpSessionMap.remove("CHECKNUM");
return "toErrorJsp";
}
}else{
//客户端验证码为空
httpSessionMap.put("message", "验证码为空");
//将位于HttpSessionMap中的验证码删除
httpSessionMap.remove("CHECKNUM");
return "toErrorJsp";
}
}
/**
* 管理员退出
*/
public String exitMethod(){
Map<String, Object> httpSessionMap = ActionContext.getContext().getSession();
httpSessionMap.remove("admin");
return "toLoginJsp";
}
}
11、在cn.chuang.crm.config包下创建spring-admin.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置AdminDao -->
<bean id="adminDao" class="cn.chuang.crm.dao.AdminDao">
<property name="ht" ref="hibernateTemplate"/>
</bean>
<!-- 配置adminService -->
<bean id="adminService" class="cn.chuang.crm.service.AdminService">
<property name="adminDao" ref="adminDao" />
</bean>
<!-- 配置adminAction -->
<bean id="adminAction" class="cn.chuang.crm.web.AdminAction">
<property name="adminService" ref="adminService"/>
</bean>
</beans>
12、在在cn.chuang.crm.config包下创建sturts-admin.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="adminPackage" namespace="/" extends="struts-default">
<action name="loginRequest" class="adminAction" method="loginMethod">
<result name="toMainJsp" type="redirect">
/menu/main.jsp
</result>
<result name="toErrorJsp" type="dispatcher">
/error.jsp
</result>
</action>
<action name="exitRequest" class="adminAction" method="exitMethod">
<result name="toLoginJsp" type="redirect">
/login.jsp
</result>
</action>
</package>
</struts>
13、在src目录下创建struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="cn/chuang/crm/config/struts-admin.xml" />
</struts>
14、对项目中WebRoot/WEB-INF 路径下的web.xml中进行配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<!-- 配置struts2核心过滤器 -->
<filter>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置spring核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/cn/chuang/crm/util/spring-base.xml,
/WEB-INF/classes/cn/chuang/crm/config/spring-admin.xml
</param-value>
</context-param>
<!-- 配置欢迎页面 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>