1.新建一个web项目(导入相应的需要的jar包,也可以直接用maven在管理)
2.分别建立struts.xml,applicationContext.xml(spring),hibernate.cfg.xml的配置文件
a.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>
<!-- 修改struts支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<!-- 告知Struts2运行时使用Spring来创建对象 -->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="User" namespace="/" extends="struts-default">
<action name="UserAction" class="UserActionSpring">
<result name="loginsuccess">/index.jsp</result>
<result name="loginfail">/login.jsp</result>
</action>
</package>
</struts>
<注意:action里面的class名称是spring配置文件里面的定义的id名称>
b.hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/cars</property>
<property name="connection.username">root</property>
<property name="connection.password">495873513</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hbm2ddl.auto">none</property><!--数据库有表就设置值none,没有第一次设为create-->
<property name="show_sql">true</property><!--log4j数据显示sql查询语句-->
<property name="format_sql">true</property>
<mapping resource="com/oracle/ssh/model/beans/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
c.applicationContext.xml(spring)
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " default-autowire="byName">
<!-- 配置spring管理整个hibernate的对象创建,包括sessionFactory,和事务AOP管理对象 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 声明spring的事务管理器bean组件 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 声明spring的AOP事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!-- 上面是把spring创建的sessionFactory以及spring AOP的事物管理对象以及注解绑定的事务管理对象都配好 -->
<context:component-scan base-package="com.oracle.ssh.model"></context:component-scan>
<!-- 这里将struts的action类配置到spring的工厂里面表示spring接管了struts组件里面的action创建这个工作 -->
<!-- id定义名字,class具体的类,scope="prototype"定义非单例 -->
<bean id="UserActionSpring" class="com.oracle.ssh.control.action.UserAction"></bean>
<bean id="service" class="com.oracle.ssh.model.service.UserService"></bean>
<bean id="dao" class="com.oracle.ssh.model.daos.UserDAOimp"></bean>
</beans>
<注意:在beans里面设置的是自动映射
default-autowire="byName",所以在接管的bean里面配置是对应相应类的属性名称>
3.建立dao层文件,来测试框架
a.jsp传过来的值存在是beans里面
package com.oracle.ssh.model.beans;
import java.io.Serializable;
public class User implements Serializable {
private int userid;
private String username;
private String password;
//提供get()和set()方法
}
b.在struts的配置里面找到的是UserAction这个实例
package com.oracle.ssh.control.action;
import com.opensymphony.xwork2.ActionSupport;
import com.oracle.ssh.model.beans.User;
import com.oracle.ssh.model.service.UserService;
public class UserAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
//service在spring的配置bean的是主动映射ByName
public UserService service;
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
}
public String login()
{
System.out.println(user);
User resultuser=service.login(user);
System.out.println(resultuser);
if(resultuser!=null)
return "loginsuccess";
else
return "loginfail";
}
}
c.在Useraction里面传给UserSerivce来完成(UserAction相当于一个中转站不用太多的业务逻辑代码)
package com.oracle.ssh.model.service;
import com.oracle.ssh.model.beans.User;
import com.oracle.ssh.model.daos.UserDAO;
public class UserService {
/**service是服务层,专门为action和dao层做服务*/
public UserDAO dao;
public UserDAO getDao() {
return dao;
}
public void setDao(UserDAO dao) {
this.dao = dao;
}
public User login(User user)
{
return dao.login(user);
}
}
d.在UserDAOimp实现类里面来执行查询代码
package com.oracle.ssh.model.daos;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.oracle.ssh.model.beans.User;
public class UserDAOimp extends HibernateDaoSupport implements UserDAO {
@Override
public User login(User user) {
Criteria c=getSession().createCriteria(User.class);
c.add(Restrictions.eq("username", user.getUsername()));
c.add(Restrictions.eq("password", user.getPassword()));
List<User> l=(List<User>)c.list();
System.out.println(l);
if(l.size()>0)
return l.get(0);
else
return null;
}
}
以上基本就是ssh搭建的步骤及测试!一个来自新手的总结,不好的地方多多包涵
![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)