您的位置:首页 > 编程语言 > Java开发

Java之基于Eclipse搭建SSH框架(下)

2017-07-22 16:59 441 查看
在上篇博客里,我简介了Tomcat滴配置与Struts2滴搭建,假设对这个还不会滴童鞋去看一下我滴上篇博客《Java之基于Eclipse搭建SSH框架(上)》。今天我们接着上篇博客滴内容。继续搭建我们滴SSH框架。

(一)在上篇博客滴基础上整合Spring:

首先我们把Spring所须要的jar(上篇博客有),拷贝到WebContent下的WEB-INF下的lib里面。

其次在src下创建名为:applicationContext.xml文件。(有些人提示在WEB-INF下创建)个人建议在src下创建

Spring配置文件有两种格式:DTD格式。Schema格式。

基于DTD格式的配置文件格式例如以下:

<?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格式的配置文件拥有自己的命名空间。格式例如以下:

<?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里面这样配置就能够了:

<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是同样的!

。!

其次在web.xml我们须要在加入以下这些代码:

<!-- 用来定位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地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: