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

Eclipse整合SSH2框架

2018-01-25 15:07 501 查看
MyEclipse整合SSH2很容易,现在尝试着在Eclipse中搭建一个SSH2项目,经过努力终于成功的实现。现在记录下来,分享给需要的朋友们!

完整项目下载地址:下载地址

1、创建JavaWeb项目

首先新建一个Dynamic Web Project,其次在WEB-INF下创建一个web.xml文件,然后导入所有相关的struts2 spring Hibernate的jar包到WEB-INF下的lib文件夹下(相应的jar包均在项目中已经加入,下载项目即可)

web.xml文件内容:spring配置中这样写是为了加载所有的application.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssh2</display-name>

<!-- struts2 -->
<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>*.action</url-pattern>
</filter-mapping>

<!-- 用来定位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>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2、在src下创建相应的包及struts、application文件



3、struts.xml文件中的主要配置信息如下

struts交由spring管理,所有这里的class应该与application.xml中的Action配置的bean的id相对应。

<!-- 测试spring管理 -->
<package name="three" namespace="/three" extends="struts-default">
<action name="exe" class="testAction">
<result name="success">/test/test_add.jsp</result>
</action>
<!-- 此处的class的内容要与Spring配置文件中的bean的id相同 -->
<action name="add" class="testAction" method="add">
<result name="success">/index2.jsp</result>
</action>
</package>
4、application.xml文件中的主要信息如下

Dao Action Service的配置取名均与相应的类中的注入有关,项目启动的联系入口为引入文件的set方法

<?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"> <!-- Dao配置 -->
<bean id="testDao" class="com.test.dao.TestDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- Action配置 -->
<bean id="testAction" class="com.test.action.TestAction" scope="prototype">
<property name="testService" ref="testService"></property>
</bean>
<!-- Service配置 -->
<bean id="testService" class="com.test.service.TestServiceImpl">
<property name="testDao" ref="testDao"></property>
</bean>

<!-- 配置数据源 -->
<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/ssh2" />
<!-- 指定连接数据库的用户名 -->
<property name="username" value="root" />
<!-- 指定连接数据库的密码 -->
<property name="password" value="root" />
</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/test/entity/Test.hbm.xml</value>
</list>
</property>
</bean>
</beans>
5、action的主要内容

private TestService testService;
private Test test;
private List<Test> list;
public Test getTest() {
return test;
}
public void setTest(Test test) {
this.test = test;
}
public List<Test> getList() {
return list;
}
public void setList(List<Test> list) {
this.list = list;
}

public void setTestService(TestService testService) {
this.testService = testService;
}
public String add(){
this.testService.addTest(test);
return "success";
}
testService在Action中必须有对应的set方法即

public void setTestService(TestService testService) {

this.testService = testService;

}

这样项目在启动的时候加载application.xml文件才会以set方法为入口,保证testService加载进来。这里的testService名称要与application中Action配置的property的属性名一致

6、Service主要内容

public class TestServiceImpl implements TestService {

private TestDao testDao;
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
这里的testDao的名字要与application.xml中的Dao配置的id名称相一致。

7、Dao的主要内容

@Repository
public class TestDaoImpl extends HibernateDaoSupport implements TestDao {

@Override
public void addTest(Test test) {
this.getHibernateTemplate().save(test);
}


8、Test.hbm.xml配置内容

<hibernate-mapping package="com.test.entity">
<class name="Test" table="test">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name" type="java.lang.String"
not-null="true" length="16">
</property>
<property name="password" column="password" type="java.lang.String"
not-null="true" length="16">
</property>
</class>
</hibernate-mapping>
到此基本上项目也就出来了,完整的项目内容直接下载即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: