需要的jar包链接
https://download.csdn.net/download/lushizhuo9655/12645581
web.xml
<!-- 让spring随web启动而创建的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring配置文件位置参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 扩大session作用范围
注意: 任何filter一定要在struts的filter之前调用
-->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<!-- struts2核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///ssh_crm
jdbc.user=root
jdbc.password=root
struts.xml
<?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>
<constant name="struts.objectFactory" value="spring"></constant>
<package name="crm" namespace="/" extends="struts-default" >
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<action name="UserAction_*" class="userAction" method="{1}" >
<result name="toHome" type="redirect" >/index.htm</result>
<result name="error" >/login.jsp</result>
</action>
</package>
</struts>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 读取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
<property name="driverClass" value="${jdbc.driverClass}" ></property>
<property name="user" value="${jdbc.user}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource" ></property>
<!-- 配置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>
<!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
<property name="mappingDirectoryLocations" value="classpath:cn/itcast/domain" ></property>
</bean>
<!-- 核心事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
<!-- 配置通知 -->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice> -->
<!-- 配置将通知织入目标对象
配置切点
配置切面 -->
<!-- <aop:config>
<aop:pointcut expression="execution(* cn.itcast.service.impl.*ServiceImpl.*(..))" id="txPc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config> -->
<!-- ========================================================================================= -->
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean name="userAction" class="cn.itcast.web.action.UserAction" scope="prototype" >
<property name="userService" ref="userService" ></property>
</bean>
<!-- service -->
<bean name="userService" class="cn.itcast.service.impl.UserServiceImpl" >
<property name="userDao" ref="userDao" ></property>
</bean>
<!-- dao -->
<bean name="userDao" class="cn.itcast.dao.impl.UserDaoImpl" >
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
</beans>
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="cn.itcast.domain.User" table="t_user">
<id name="user_id" column="user_id" >
<generator class="native"></generator>
</id>
<property name="user_code" column="user_code"></property>
<property name="user_name" column="user_name"></property>
<property name="user_password" column="user_password"></property>
<property name="user_state" column="user_state"></property>
</class>
</hibernate-mapping>
public class UserAction extends ActionSupport implements ModelDriven<User>{
private User user=new User();
private UserService userService;
public String login(){
User u=userService.getLoginByUsercode(user);
ActionContext.getContext().getSession().put("user", u);
return "toHome";
}
@Override
public User getModel() {
return user;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
public interface UserService {
User getLoginByUsercode(User user);
}
@Transactional
public class UserServiceImpl implements UserService {
private UserDao userDao;
@Override
public User getLoginByUsercode(User user) {
User existU=userDao.getUserByUsercode(user.getUser_code());
if(existU == null){
throw new RuntimeException("用户名不存在");
}
if(!existU.getUser_password().equals(user.getUser_password())){
throw new RuntimeException("密码错误");
}
return existU;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public User getUserByUsercode(final String usercode) {
//HQL 由于是匿名内部类因此参数变成final类型
/*return getHibernateTemplate().execute(new HibernateCallback<User>() {
@Override
public User doInHibernate(Session session) throws HibernateException {
String hql="from User where user_code=?";
Query query = session.createQuery(hql);
query.setParameter(0,usercode);
User user=(User) query.uniqueResult();
return user;
}
});*/
DetachedCriteria dc=DetachedCriteria.forClass(User.class);
dc.add(Restrictions.eq("user_code", usercode));
List<User> list = (List<User>) getHibernateTemplate().findByCriteria(dc);
if(list !=null && list.size()>0){
return list.get(0);
}else{
return null;
}
}
}
jsp页面只写了部分
<%@ taglib uri="/struts-tags" prefix="s" %>
<FORM id=form1 action="${pageContext.request.contextPath}/UserAction_login" method=post></FORM>
<font color="red" ><s:property value="exception.message"/></font>