一、搭建开发环境
1、创建web项目,引入jar包
2、引入配置文件
- Struts的框架:web.xml、struts.xml
- web.xml
<!-- 配置Struts的核心过滤器 -->
<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>/*</url-pattern>
</filter-mapping>
- Spring的框架
- jdbc.properties
- log4j.properties
- applicationContext.xml
- web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
- Hibernate的框架
- hibernate.cfg.xml
3、创建相关包结构
- Java Resources
- src
- com.ithou.crm
- dao
- domain
- service
- web
- action
- interceptor
- com.ithou.crm
- src
4、引入相关页面
5、创建与用户相关的类
- UserDao、UserDaoImpl、User、UserService、UserServiceImpl、UserAction
6、将相关的类配置到Spring中
<!-- 配置用户管理相关的类 -->
<!-- 配置Dao -->
<bean id="userDao" class="com.ithou.crm.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置Service -->
<bean id="userService" class="com.ithou.crm.service.impl.UserServiceImpl">
</bean>
<!-- 配置Action -->
<bean id="UserAction" class="com.ithou.crm.web.action.UserAction" scope="prototype">
</bean>
功能一:用户模块---用户注册代码实现
1、在登陆页面login.jsp提供一个注册页面
2、创建用户表
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;
3、创建实体和映射
public class User {
private Long user_id;
private String user_code;
private String user_name;
private String user_password;
private String user_state;
get/set...
}
- 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.ithou.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>
4、编写Action
public class UserAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
@Override
public User getModel() {
return user;
}
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String regist() {
System.out.println("用户注册的方法实现了......");
userService.regist(user);
return LOGIN;
}
}
- 在Action中注入service:
<!-- 配置Action -->
<bean id="UserAction" class="com.ithou.crm.web.action.UserAction" scope="prototype">
<property name="userService" ref="userService" />
</bean>
- 配置Action的访问
<action name="user_*" class="com.ithou.crm.web.action.UserAction" method="{1}">
<result name="login">/login.jsp</result>
</action>
5、编写service
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void regist(User user) {
//对密码进行加密处理
user.setUser_password(MD5Utils.md5(user.getUser_password()));
user.setUser_state("1");
//调用Dao
userDao.regist(user);
}
}
- 在service中注入dao:
<bean id="userService" class="com.ithou.crm.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
6、编写Dao
- 整合Hibernate
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置Hibernate的相关属性 -->
<property name="hibernateProperties">
<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/ithou/crm/domain/user.hbm.xml</value>
</list>
</property>
</bean>
- 在Dao中继承HibernateDaoSurrport类
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
@Override
/**
* 保存用户
*/
public void regist(User user) {
this.getHibernateTemplate().save(user);
}
}
- 在Dao中注入SessionFactory
<bean id="userDao" class="com.ithou.crm.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
7、配置Spring的事务管理
- 配置事务管理器
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
- 开启注解事务
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
- 在业务层添加事务注解
功能二:用户模块---用户登录代码实现
1、修改登陆页面
2、编写Action中login方法
public String login() {
User existUser = userService.login(user);
if(existUser == null) {
this.addActionError("用户名或密码错误!");
return LOGIN;
}else {
ActionContext.getContext().getSession().put("existUser",existUser);
return SUCCESS;
}
}
3、编写Service
@Override
public User login(User user) {
user.setUser_password(MD5Utils.md5(user.getUser_password()));
return userDao.login(user);
}
4、编写Dao
@Override
public User login(User user) {
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;
}
5、配置页面的跳转
<action name="user_*" class="com.ithou.crm.web.action.UserAction" method="{1}">
<result name="login">/login.jsp</result>
<result name="success" type="redirect">/index.jsp</result>
</action>
6、在页面中显示数据
- 在成功页面显示用户信息
- top.jsp:
当前用户:<s:property value="#session.existUser.user_name"/>
- 在失败页面上显示信息
- login.jsp
<s:actionerror/>