关闭

SSH简单项目实例(附下载)

374人阅读 评论(0) 收藏 举报
分类:

一个简单的SSH项目框架,基本配置,增删改查,分页都有,jar包在WEB-INF下的lib下面,可以直接运行。
1.项目环境

myeclipse10
JDK1.7
Tocat7.0
Spring3.2.5
Struts2.3.24
Hibernate3.0
Mysql5.5

2.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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- 引入外部属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 配置hibernate的相关属性 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置hibernate属性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop><!-- 有表自动更新表结构,没有就创建表 -->
            </props>
        </property>

        <!-- 配置关联关系映射文件 -->
        <property name="mappingResources">
            <list>
                <value>com/entity/Department.hbm.xml</value>
                <value>com/entity/Employee.hbm.xml</value>
                <value>com/entity/User.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!-- 配置action,action交由spring管理 -->
    <bean id="employeeAction" class="com.action.EmployeeAction" scope="prototype">
        <property name="employeeService" ref="employeeService"></property>
        <property name="departmentService" ref="departmentService"></property>
    </bean>
    <bean id="departmentAction" class="com.action.DepartmentAction"
        scope="prototype">
        <property name="departmentService" ref="departmentService"></property>
    </bean>
    <bean id="userAction" class="com.action.UserAction" scope="prototype">

    </bean>
    <!-- 配置业务层service -->
    <bean id="employeeService" class="com.service.impl.EmployeeServiceImpl">
        <property name="employeeDao" ref="employeeDao"></property>
    </bean>
    <bean id="departmentService" class="com.service.impl.DepartmentServiceImpl">
        <property name="departmentDao" ref="departmentDao"></property>
    </bean> 
    <!-- 配置dao -->
    <bean id="employeeDao" class="com.dao.impl.EmployeeDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="departmentDao" class="com.dao.impl.DepartmentDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

update,配置好表对应的hbm.xml,项目运行的时候直接生成数据库。
3.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="default" extends="struts-default" namespace="/">
        <action name="user_*" class="userAction" method="{1}" >
            <result name="INPUT">/index.jsp</result>
            <result name="SUCCESS" type="redirect" >/frame.jsp</result>
            <result name="HOME" >/frame/right.jsp</result>
        </action>

        <action name="employee_*" class="employeeAction" method="{1}" >
            <result name="INPUT">/index.jsp</result>
            <result name="SUCCESS" type="redirect" >/frame.jsp</result>
            <result name="findAll">/jsp/employee/listEmployee.jsp</result>
            <result name="goAddEmployee">/jsp/employee/addEmployee.jsp</result>
            <result name="addSuccess" type="redirect">employee_findAll.action</result>
            <result name="goEditEmployee">/jsp/employee/editEmployee.jsp</result>
            <result name="editSuccess" type="redirect">employee_findAll.action</result>
            <result name="deleteSuccess" type="redirect">employee_findAll.action</result>
        </action>

        <action name="department_*" class="departmentAction" method="{1}">
            <result name="findAll">/jsp/department/listDepartment.jsp</result>
            <result name="goAddDepartment">/jsp/department/addDepartment.jsp</result>
            <result name="addSuccess" type="redirect">department_findAll.action</result>
            <result name="goEditDepartment">/jsp/department/editDepartment.jsp</result>
            <result name="updateSuccess" type="redirect">department_findAll.action</result>
            <result name="deleteSuccess" type="redirect">department_findAll.action</result>
        </action>
    </package>
</struts>    

4.web.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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>skyLine</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
      <param-name>flushMode</param-name>
      <param-value>AUTO</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

5.项目结构这里写图片描述
6.运行
这里写图片描述
这里写图片描述

项目比较简单,直接可以运行,因为只是一个简单基础框架,直接附上下载地址:

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:504849次
    • 积分:5680
    • 等级:
    • 排名:第5219名
    • 原创:125篇
    • 转载:79篇
    • 译文:0篇
    • 评论:59条
    最新评论