本章介绍ssh(struts2+spring+hibernate)整合方法。不多说直接如整题。
在整合之前,本人根据对三种框架的理解对其在系统中所扮演的角色进行了分工:
struts作为控制层,接受前端发送过来的数据,传递给service处理,把处理后的结果返回到前端。(承上启下)
spring作为管理层,使用spring容器来管理Controller,Service,Dao层。(服务)
hibernate作为数据持久层,与数据库打交道(介于java与数据库的中间层)
总体来说系统是一个遵守MVC模式的系统,对于各个框架不熟悉的同学可以先自行查找资料了解一下。
本次整合在非maven环境下配置,jar包都是手动导入,需要下载jar包的同学可以在这里下载
地址:lib下载通道
配置环境:jdk1.7/IntelliJ IDEA 14/myqsl/tomcat 1.7
下面开始介绍整合流程:
创建工程目录(默认加入struts框架支持)
通过以上配置后,项目会自动添加struts支持jar包,并在src下生成struts.xml配置文件,同时在web.xml中添加struts2配置。
web.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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>
</web-app>
创建基本目录结构用于测试struts2
当前目录结构
创建LoginAction处理前端发送过来的请求
LoginAction
package com.ssh.web.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* 登录处理action
* Created by zhugp on 2017/6/23.
*/
public class LoginAction extends ActionSupport{
private static final long serialVersionUID = 228428617014514506L;
private String result = "";
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
@Override
public String execute() throws Exception {
System.out.println("登录");
return SUCCESS;
}
}
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>
<package name="login" extends="struts-default">
<!--请求com.ssh.web.action.LoginAction处理,返回Success时调转到Welcome.jsp界面-->
<action name="loginAction" class="com.ssh.web.action.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
</struts>
index.jsp做一个简单的登录表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<form action="loginAction.action" method="post">
账号:<input type="text" placeholder="输入账号">
密码:<input type="password" placeholder="输入密码">
<button type="submit">登录</button>
</form>
</body>
</html>
测试,当action获取拿到请求时会在控制台打印登录,暂时没有做任何其他的操作,只用来测试Struts是否已经工作。测试结果:通过!
接下来处理spring部分
大致流程:导入jar包→配置spring-config.xml→配置web.xml
为了简化操作,我将hibernate和spring的配置的顺序颠倒了,因为这么做可以直接跳过hibernate.cfg.xml中sessionFactory配置。
注意:通过idea自动导入的spring jar包中不包括spring-web.jar,需要手动将该包导入到lib中,否则在配置web.xml的spring ContextLoaderListener监听器时会找不到文件。另外需要添加struts2-spring-plugin-2.3.16.3.jar 包否则spring注入bean会失败。
导入jar包如图所示:
配置spring-config.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: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">
<!--启动spring注解支持-->
<context:annotation-config />
<!--spring扫描基本包下所有的注解,纳入spring容器管理-->
<context:component-scan base-package="com.ssh.web"/>
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/sshdemo"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
<bean name="loginAction" class="com.ssh.web.action.LoginAction">
</bean>
</beans>
在spring-config.xml文件中我只用spring做了两件事.
1.开启spring注解支持
2.扫描对应包下的注解
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 2运行时使用Spring来创建对象-->
<constant name="struts.objectFactory" value="spring"/>
<package name="login" extends="struts-default">
<!--请求com.ssh.web.action.LoginAction处理,返回Success时调转到Welcome.jsp界面-->
<action name="loginAction" class="loginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
</struts>
现在的目录结构已经重新整理分层:
然后修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
运行,走你。请求能从前端发到后台即可
整合hibernate部分
大致流程:
导入jar包→配置参数(hibernate.cfg.xml)→创建POJO(实体类)→创建POJO映射文件→将映射文件纳入hibernate.cfg.xml管理→测试
项目右键→Add Frameworks Support→选中hibernate→调整hibernate版本→ok
这里我使用的是hibernate4.0.1,以上确认之后将会自动导入hibernate需要的jar包
确认jar包导入后创建hibernate配置文件,实体类已在上一部分创建好了,接着创建实体映射文件。
User.hbm.xml
<?xml version="1.0"?>
<!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.ssh.web.entity.User">
<id name="id" column="id"/>
<property name="name" column="name"/>
<property name="age" column="age"/>
<property name="sex" column="sex"/>
</class>
</hibernate-mapping>
前面已经整合了将struts与spring,hibernate的配置文件我不在和原始配置文件一样去写数据库的配置,而是通过在spring中通过Druid连接池充当数据源,将sessionFactory当做一个bean来交给spring管理。这里我修改了一个spring-config.xml文件,添加了hibernate.cfg.xml文件。
spring-config.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: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">
<!--启动spring注解支持-->
<context:annotation-config />
<!--spring扫描基本包下所有的注解,纳入spring容器管理-->
<context:component-scan base-package="com.ssh.web"/>
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/sshdemo"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
</beans>
hibernate.cfg.xml文件只做了一件事 就是管理映射文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
全部配置已经配置完毕,测试登录。
业务逻辑代码为了节省篇幅就不贴出来了,有兴趣的同学可以去https://github.com/674236876/ssh_test查看源码,由于上传文件到git上的速度实在太慢了,所以我把lib包上传到csdn,点击lib下载通道可下载。
注:
整合时遇到的部分异常:
Caused by: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 37; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'context:annotation-config' 的声明。
解决方案:文件头声明部分xsi:schemaLocation的值添加
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 申明即可
设置数据源时,我尝试将url,username,password等参数封装到一个properties文件中,却抛出了一个很有趣的问题,提示账号密码错误,当时忘了记录,这里提醒一下各位,hibernate发行版中存在一个hibernate.properties文件,如果你自己写的properties文件属性与它一致,那么传给hibernate的连接属性将默认是hibernate.properties里面设置的默认账号和密码,小心咯!