SSH框架整合--注解形式
准备struts2框架需要的jar包
今天带来ssh框架的整合struts2+spring+hibernate,用的工具是IDEA。各框架版本如下:struts-2.3、hibernate-5.4,spring-4.2.现在开始整合吧
准备struts2框架需要的jar包
因为要使用struts2直接,这里还需要struts2注解jar包
以及整合spring的jar包
Hibernate所需jar包
这里使用c3p0连接池所以导入c3p0jar包
数据库jar包
最后准备spring所需jar包
1.spring基本包
2.aop联盟包
3.还有commons-loging依赖包,可在struts2lib下找到
最后因为是使用ieda,还需要导入tomcat中的jar包用来支持jsp页面
开始搭建框架
所需要的配置文件:
struts2框架 src/struts.xml
Hibernate框架配置文件可以不需要
spring框架 src/applicationContext.xml
数据库连接池:db.properties
项目结构如下:
其中struts.xml文件头可在导入的jar包中找到
struts2-core–>struts-2.3.dtd
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
</struts>
applicationContext.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:context="http://www.springframework.org/schema/context" 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"> <!-- bean definitions here -->
</beans>
如果配置像这样
那是因为idea还需要在设置中导入lib
还需要在导入srping配置文件
首先,spring整合Hibernate
web.xml中配置spring监听器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
c3p0数据库配置文件db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///user
jdbc.user=root
jdbc.password=root
applicationContext.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: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.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"> <!-- bean definitions here -->
<!--导入数据库配置文件-->
<context:property-placeholder location="classpath:db.properties" />
<!--c3p0配置连接池-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="c3p0" >
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置hibernate的session工厂,根据hibernate版本选择-->
<bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="sessionFactory">
<!--连接池-->
<property name="dataSource" ref="c3p0"/>
<!--hibernate配置-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto" >update</prop>
<prop key="hibernate.show_sql"> true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
<!--配置实体类扫描位置-->
<property name="packagesToScan" value="com.org.wa.domain" />
</bean>
<!--配置hibernate事务管理-->
<bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--配置事务注解-->
<tx:annotation-driven transaction-manager="transactionManager" />
<!--开启注解扫描-->
<context:component-scan base-package="com.org.wa" />
</beans>
设置实体类Student.java
@Entity
@Table(name = "t_student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String className;
因为在hibernate中配置了自动建表,所以此时可以启动项目,数据库中会自动创建student表,同时在控制台会打印sql语句
开始整合struts2
首先需要编写Dao层,与service层,这里使用接口是实现类的方法,实现用户登录的方法
首先在web.xml中加入struts的过滤器
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在执行数据库查找是最好在web.xml加入延迟加载过滤器设置,次设置一定在struts过滤器之前
<filter>
<filter-name>codeFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>codeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1.dao层
public interface StudentDao {
Student login(Student student);
}
@Repository
public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao {
@Autowired
public void setSuperSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
@Override
public Student login(Student student) {
List<Student> students = this.getHibernateTemplate().findByExample(student);
if(students.size()>0){
return students.get(0);
}
return null;
}
}
3.service层
public interface StudentService {
Student login(Student student);
}
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public Student login(Student student) {
return studentDao.login(student);
}
}
3.action层
import java.io.IOException;
@Controller
@Namespace("/")
@ParentPackage("struts-default")
public class StudentAction extends ActionSupport implements ModelDriven<Student> {
private Student student = new Student();
@Autowired
private StudentService studentService;
@Action(value = "/login")
public void login() throws IOException {
Student student1 = studentService.login(student);
String mess = "用户名或密码错误!";
if(student1!=null){
mess = "登录成功!";
}
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.getWriter().write(mess);
}
@Override
public Student getModel() {
return student;
}
}
4.登录用的表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login" method="post">
帐号:<input name="name" /><br/>
密码:<input name="password" /><br />
<input type="submit" />
</form>
</body>
</html>
好了,ssh整合到这里就完成了。谢谢大家!