<!-- Struts2的核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置Struts2的常量 -->
<constant name="struts.action.extension" value="action" />
</struts>
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///crm
jdbc.username=root
jdbc.password=root
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
# error warn info debug trace
log4j.rootLogger= info, stdout
<?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: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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 引入外部属性文件================================== -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0链接池=================== -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入属性 -->
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>
<!-- 配置Spring的核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载Spring的配置文件的路径的,默认加载的/WEB-INF/applicationContext.xml,设置加载classpath:applicationContext.xml -->
<!-- 全局初始化参数 -->
<context-param>
<!-- 参数名称 -->
<param-name>contextConfigLocation</param-name>
<!-- 参数的值 -->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
页面下载地址:https://download.csdn.net/download/qq_44757034/12615556
package com.itzheng.crm.domain;
/*
* 用户的实体
*/
public class User {
}
package com.itzheng.crm.dao;
/*
* 用户管理的Dao的接口
*/
public interface UserDao {
}
package com.itzheng.crm.dao.impl;
import com.itzheng.crm.dao.UserDao;
/*
* 用户管理的Dao的实现类
*/
public class UserDaoImpl implements UserDao {
}
package com.itzheng.crm.service;
/*
* 用户管理的Service的接口
*/
public interface UserService {
}
package com.itzheng.crm.service.impl;
import com.itzheng.crm.service.UserService;
/*
* 用户管理的Service的接口的实现类
*/
public class UserServiceImpl implements UserService {
}
package com.itzheng.crm.web.action;
import com.itzheng.crm.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/*
* 用户管理的Action的类
*/
public class UserAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
在applicationContext.xml当中
<!-- 配置Action -->
<bean id="userAction" class="com.itzheng.crm.web.action.UserAction" scope="prototype">
</bean>
<!-- 配置Service -->
<bean id="userService" class="com.itzheng.crm.service.impl.UserServiceImpl">
</bean>
<!-- 配置Dao -->
<bean id="userDao" class="com.itzheng.crm.dao.impl.UserDaoImpl">
</bean>
<input type="button" value="注册" onclick="registUI()"/>
<script type="text/javascript">
function registUI() {
window.location = "${ pageContext.request.contextPath }/regist.jsp";
}
</script>
CREATE TABLE `sys_user` (
`user_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '用户id',
`user_code` VARCHAR(32) NOT NULL COMMENT '用户账号',
`user_name` VARCHAR(64) NOT NULL COMMENT '用户名称',
`user_password` VARCHAR(32) NOT NULL COMMENT '用户密码',
`user_state` CHAR(1) NOT NULL COMMENT '1:正常,0:暂停',
PRIMARY KEY (`user_id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
/*Data for the table `sys_user` */
LOCK TABLES `sys_user` WRITE;
INSERT INTO `sys_user`(`user_id`,`user_code`,`user_name`,`user_password`,`user_state`) VALUES (5,'m0003','小军','123','1'),(6,'m0001','小红','123','1'),(7,'m0001','小明','123','1'),(8,'m0001','小红','123','1');
UNLOCK TABLES;
package com.itzheng.crm.domain;
/*
* 用户的实体
*/
public class User {
private Long user_id;
private String user_code;
private String user_name;
private String user_password;
private String user_state;
public Long getUser_id() {
return user_id;
}
public void setUser_id(Long user_id) {
this.user_id = user_id;
}
public String getUser_code() {
return user_code;
}
public void setUser_code(String user_code) {
this.user_code = user_code;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_password() {
return user_password;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
}
public String getUser_state() {
return user_state;
}
public void setUser_state(String user_state) {
this.user_state = user_state;
}
public User(Long user_id, String user_code, String user_name, String user_password, String user_state) {
super();
this.user_id = user_id;
this.user_code = user_code;
this.user_name = user_name;
this.user_password = user_password;
this.user_state = user_state;
}
@Override
public String toString() {
return "User [user_id=" + user_id + ", user_code=" + user_code + ", user_name=" + user_name + ", user_password="
+ user_password + ", user_state=" + user_state + "]";
}
}
在domian下创建User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 建立类与表的映射 -->
<class name="com.itzheng.crm.domain.User"
table="sys_user">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="user_id" column="user_id">
<!-- 主键生成策略 -->
<generator class="native" />
</id>
<!-- 建立类中的普通的属性和表的字段的对应映射 -->
<property name="user_code" column="user_code" />
<property name="user_name" column="user_name"/>
<property name="user_password" column="user_password" />
<property name="user_state" column="user_state" />
</class>
</hibernate-mapping>
编写regist方法
package com.itzheng.crm.web.action;
import com.itzheng.crm.domain.User;
import com.itzheng.crm.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/*
* 用户管理的Action的类
*/
public class UserAction extends ActionSupport implements ModelDriven<User> {
//模型驱动使用的对象(自动封装表单提交的数据)
private User user = new User();
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
//注入Service
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
/*
* 用户注册的方法:regist
*/
public String regist() {
userService.regist(user);
return NONE;
}
}
package com.itzheng.crm.utils;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {
public static String md5(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("û��md5����㷨��");
}
String md5code = new BigInteger(1, secretBytes).toString(16);// 16��������
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
public static void main(String[] args) {
System.out.println(md5("123"));
}
}
package com.itzheng.crm.service.impl;
import com.itzheng.crm.dao.UserDao;
import com.itzheng.crm.domain.User;
import com.itzheng.crm.service.UserService;
import com.itzheng.crm.utils.MD5Utils;
/*
* 用户管理的Service的接口的实现类
*/
public class UserServiceImpl implements UserService {
//注入DAO:
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
//业务层注册用户的方法
public void regist(User user) {
//对密码进行加密的处理:
String password = MD5Utils.md5(user.getUser_password());
user.setUser_password(password);
user.setUser_state("1");//正常的状态
//调用DAO
userDao.save(user);
}
}
在applicationContext.xml的UserService当中注入UserDao
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置Hibernate的相关的属性 -->
<property name="hibernateProperties">
<!-- 注入复杂数据类型Properties -->
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 引入映射文件 -->
<property name="mappingResources">
<list>
<value>com/itzheng/crm/domain/User.hbm.xml</value>
</list>
</property>
</bean>
在UserDaoImpl 当中HibernateDaoSupport向数据库当中保存dao
package com.itzheng.crm.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.UserDao;
/*
* 用户管理的Dao的实现类
*/
import com.itzheng.crm.domain.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
//DAO 当中保存用户的方法
public void save(User user) {
this.addActionMessage("注册成功!请登录!");
this.getHibernateTemplate().save(user);
}
}
在applicationContext.xml当中配置注入sessionFactory
<!-- 配置Dao -->
<bean id="userDao" class="com.itzheng.crm.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
在applicationContext.xml当中
<!-- 配置事务事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 可以理解为在transactionManager事务管理器当中注入sessionFactory数据库连接池 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
在applicationContext.xml当中
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
public String login() {
// 调用业务层查询用户
User existUser = userService.login(user);
if (existUser == null) {
// 登录失败
//添加错误信息
this.addActionError("用户名或密码错误!");
return LOGIN;
} else {
// 登录成功
// ServletActionContext.getRequest().getSession().setAttribute("existUser",
// existUser);
ActionContext.getContext().getSession().put("existUser", existUser);
return SUCCESS;
}
}
UserService
UserServiceImpl
对登录的密码进行加密处理(和数据库当中的加密的密码进行对比)
UserDao
UserDaoImpl
@Override
//DAO 中根据用户名和密码进行查询的方法:
public User login(User user) {
// TODO Auto-generated method stub
List<User> list = (List<User>) this.getHibernateTemplate().find("from User where user_code = ? and user_password = ?",user.getUser_code(),user.getUser_password());
//判断一下
if(list.size() > 0) {
return list.get(0);
}
return null;
}