在上篇博客里,我简介了Tomcat滴配置与Struts2滴搭建,假设对这个还不会滴童鞋去看一下我滴上篇博客《Java之基于Eclipse搭建SSH框架(上)》。今天咱们接着上篇博客滴内容。继续搭建咱们滴SSH框架。
(一)在上篇博客滴基础上整合Spring:
首先咱们把Spring所需要的jar(上篇博客有),拷贝到WebContent下的WEB-INF下的lib里面。java
其次在src下建立名为:applicationContext.xml文件。(有些人提示在WEB-INF下建立)我的建议:在src下建立。
Spring配置文件有两种格式:DTD格式。Schema格式。
基于DTD格式的配置文件格式例如如下:mysql
web
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <beans> <!-- Service配置 --> <bean id="loginService" class="com.hy.service.impl.LoginServiceImpl" /> </beans>
Schema格式的配置文件拥有本身的命名空间。格式例如如下:spring
sql
<?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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- Service配置 --> <bean id="loginService" class="com.hy.service.impl.LoginServiceImpl" /> </beans>
这里我用的是另一种配置方式。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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Service配置 -->
<bean id="loginService" class="com.hy.service.impl.LoginServiceImpl" />
<!-- Action配置 -->
<bean id="loginServer" class="com.hy.action.LoginAction" scope="prototype">
<property name="loginService" ref="loginService"></property>
</bean>
</beans>
在struts里面这样配置就可以了:apache
<package name="struts2" extends="struts-default">
<!-- 此处的class的内容要与Spring配置文件里的bean的id一样 -->
<action name="Login" class="loginServer">
<result name="success">/result.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
这里要注意的是在struts.xml文件里面的action配置中。class=”“与咱们上篇博客讲的Struts搭建不同了。这里的class内容与applicationContext.xml里面的Action配置bean的id是一样的!markdown
。!session
其次在web.xml咱们需要在加入如下这些代码:app
<!-- 用来定位Spring框架配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext*.xml</param-value>
</context-param>
<!-- 配置Spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
这样集成Spring所要配置的文件算是结束了,另外一点要强调整合Struts与Spring需要一个jar(struts2-spring-plugin-2.3.8.jar)。这个jar我放到了struts2所需jar里面了,加入了这个jar才算把Struts与Spring整合在一块儿了。
在集成Hibernate前,说一下关于Spring XML文件上下文配置问题。
applicationContext.xml事实上这个文件可以保存到classpath或者WEB-INF文件下。随着项目增大,Spring的配置文件也会变得庞大,可以依据已定的原则分为几个配置文件。从而使配置更加清晰。提升可维护性。上面代码中的写法是查找classpath和WEB-INF文件下所有的配置文件(好多人都说了当中一种。假设写的查找和文件保存位置不同。就会报错哦~)。
測试一下,整合状况,效果图例如如下:
莫急哈~~demo我会在如下给你们,请你们看清里面的网址,因为这个demo里面也包含最后SSH的搭建測试。
(二)集成Hibernate
首先仍是把Hibernate所需要的jar(上篇博客有)。拷贝到WebContent下的WEB-INF下的lib里面。而后在applicationContext.xml中加入如下的配置:
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 指定链接数据库的驱动 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- 指定链接数据库的URL -->
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<!-- 指定链接数据库的用户名 -->
<property name="username" value="root" />
<!-- 指定链接数据库的密码 -->
<property name="password" value="" />
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autocommit">true </prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="sql_format">true</prop>
</props>
</property>
<property name="mappingResources">
<!-- 指定hibernate映射文件 -->
<list>
<value>com/hy/entity/User.hbm.xml</value>
</list>
</property>
</bean>
到这里框架就算搭建完成了。
有人会有疑惑。不是要建立hibernate.cfg.xml或者hibernate.properties配置嘛。事实上在上面的配置文件里,你是否是发现有一个bean中的文件特别像这两个文件里的内容嘛。事实上这样就可以了,不用再建立那两个文件了。
在此说明一下假设你的数据库是MySQL配置依照上面那种方式配置,别忘了加入相应的jar(有人告诉我:jar包也要与本身的数据版本号相应,不然连不上)。假设你的数据库是Oracle,配置依照如下图中进行配置。
最后在说一下Hibernate映射文件(类与表之间的关系映射)
<hibernate-mapping>
<class name="类名" table="表名">
<!-- 主键 -->
<id name="主键名">
<column name="主键列" />
<!-- 主键生成器 -->
<generator class="生成策略" />
</id>
<property name="属性名" type="数据类型">
<column name="列名" length="长度" not-null="是否不为空" />
</property>
</class>
</hibernate-mapping>
測试一下,效果图:
搭建SSH到这里就结束啦。若有疑问。请给我留言~~
最近有小伙伴反映。看了偶滴博客而后跟着作仍是报错,总结了他们的错误这里我简单说明一下:
1.假设亲尚未配置Spring,就不要把(struts2-spring-plugin-2.3.8.jar)。这个jar包提早导入到项目里(这是我滴错。把这个jar包放到struts所需的包包里面了)。假设按个人博客验证struts时出错。那就把这个包删除就能够。(整合Spring别忘了导入哦~)
2.就是配置问题:(上图,有图有真相~)
配置文件和代码里面的名字要相应,否则就出错哦~
3.就是咱们的框架用到Hibernate,童鞋们都知道写映射表,但是别忘了配置文件里指定。
最后附上:
demo地址