博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.安装java环境,jdk1.7.0_01,配置环境变量及JAVA_HOME.

2.环境准备Myeclipse  9,通过fq下载后破解(具体破解方法可以在网上搜索)。

3.安装tomcat-6.0.35.

环境准备好后,打开Myeclipse,window-Preferences-myeclipse

配置tomcat环境。

4.新建项目,选择webproject.

5.copy struts jar包到WebRoot-WEB-INF-lib.我加的jar包如下。

6.增加spring,选中项目后点击Myeclipse-Project Capabilities-Add Spring Capabilities.选择

Spring 3.0 Core Library

Spring 3.0 persistence  Core Library

Spring 3.0 AOP Library

Spring 3.0 Web Library

增加至lib目录下,点击下一步,取消Enable AOP Builder,点击Finish。完成后增加struts2-spring-plugin-2.2.3.jar到lib目录。

7.增加Hibernate ,选中项目后点击Myeclipse-Project Capabilities-Add Hibernate Capabilities.选择(默认先择就好)

接着如下图

下一步,在页面选择Existing Spring configuration file ,点击下一步页面,配置数据库连接,点击finish.

8.copy struts.xml文件到src目录下,内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.devMode" value="true" />
 <package name="sshdemo3" extends="struts-default">

 

 </package>

</struts>

9.配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name>First SSH Project</display-name>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!-- 配置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>

10.配置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:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="com.mysql.jdbc.Driver">  
  </property>
  <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
  <property name="username" value="root"></property>
  <property name="password" value="gzcss"></property>
 </bean>
 <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_show">true</prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>com/ssh/model/Customer.hbm.xml</value>
   </list>
  </property>
 </bean>
 <bean id="customerDao" class="com.ssh.dao.impl.CustomerDao">
 <property name="sessionFactory">
 <ref bean="sessionFactory"/>
 </property>
 </bean>
 <bean id="CustomerserviceBo" class="com.ssh.bo.impl.CustomerService">
 <property name="customerDao" >
  <ref bean="customerDao"/>
 </property>
 </bean>
 <bean name="customerListBean" class="com.ssh.user.action.UserAction" scope="prototype">
  <property name="customerservice" ref="CustomerserviceBo"></property>
 </bean>
 </beans>

11.需要另外copy如下jar包到lib目录下。

commons-dbcp-1.4.jar

commons-pool-1.5.6.jar

12.注意事项,hibernate映射文件一定要配置正确,很重要。否则会报错。如下:

Customer.hbm.xml

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
 <class name="com.ssh.model.Customer" table="customer" >
  <id name="customerid" type="java.lang.Integer">
            <column name="CustomerId" />
            <generator class="native" />
        </id>
  
  <property name="customername" column="CustomerName" type="java.lang.String"></property>

 </class>
</hibernate-mapping>

 

以上基本配置完成,如大家碰到问题可以一起讨论。