struts-2.3、spring2.5.6、hibernate3.3整合

1.
hibernate需要的包:这里写图片描述

spring需要的相关包: 这里写图片描述

连接 oracle 所需包:这里写图片描述

Struts2 需要的包:这里写图片描述

Struts2 spring整合所需包:这里写图片描述

2.新建web项目,将以上jar包复制在WebRoot/WEB-INF/lib里面

3.加入model层。

package com.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

@Entity
public class Student {
    private int id;
    private String name;
    private int age;

    @SequenceGenerator(name="sequenceGenerator",sequenceName="student_sequence",allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequenceGenerator")
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

4.加入dao层

package com.dao;

import java.util.List;
import com.model.Student;

public interface StudentDao {
    public void save(Student s);
    public List<Student> queryByName(String name);
}

5.加入DaoImpl层

package com.dao.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import com.dao.StudentDao;
import com.model.Student;

@Component
public class StudentDaoImpl implements StudentDao {
    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }
    @Resource
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }


    public void save(Student s) {
        hibernateTemplate.save(s);
    }

    @SuppressWarnings("unchecked")
    public List<Student> queryByName(String name){
        String sql = "from Student s where s.name=?";
        List<Student> list = hibernateTemplate.find(sql,name);
        return list;
    }
}

6.加入service层

package com.service;

import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.dao.StudentDao;
import com.model.Student;

@Component
public class StudentService {
    private StudentDao studentDao;

    public StudentDao getStudentDao() {
        return studentDao;
    }
    @Resource
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }


    public void addStudent(Student s){
        studentDao.save(s);
    }
}

7.在src目录下加入bean.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-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/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


    <context:annotation-config />
    <context:component-scan base-package="com.dao.impl,com.service,com.action" />

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
        <property name="username" value="cat"/>
        <property name="password" value="cat"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
        <property name="dataSource" ref="myDataSource" />  
        <property name="packagesToScan">
            <list>
                <value>com.model</value>
            </list>
        </property>
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.format_sql">true</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
                <prop key="hibernate.max_fetch_depth">1</prop>  
                <prop key="hibernate.use_sql_comments">true</prop>  
            </props>  
        </property>  
    </bean>

    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="serviceOperation" expression="execution(public * com.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
    </aop:config>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>


</beans>

8.此时可以加入测试类 Test,看看spring和hibernate整合成功没

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.model.Student;
import com.service.StudentService;

public class Test {

    public static void main(String[] args) {

        Student s = new Student();
        s.setName("tom");
        s.setAge(11);       
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        StudentService ss = (StudentService)context.getBean("studentService");
        ss.addStudent(s);

    }

}

9.测试成功后开始加入struts2,先修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
    <display-name></display-name>   
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>    
    <!-- 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>/*</url-pattern>
    </filter-mapping>

    <!-- struts2整合spring-->
    <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:beans.xml</param-value>
    </context-param>

</web-app>

10.加入action类

package com.action;

import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.model.Student;
import com.opensymphony.xwork2.ActionSupport;
import com.service.StudentService;

@Component
@Scope("prototype")
public class StudentAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    private StudentService studentService;
    private String name;
    private int age;


    public StudentService getStudentService() {
        return studentService;
    }
    @Resource
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public String regist(){
        Student s = new Student();
        s.setAge(age);
        s.setName(name);
        try {
            studentService.addStudent(s);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

}

11.在src目录下加入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>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

        <action name="student_action" class="studentAction" method="regist">
            <result name="success">/success.jsp</result>
            <result name="fail">/fail.jsp</result>
        </action>

    </package>

</struts>

12.在webroot下新建index.jsp、success.jsp、fail.jsp
index.jsp的内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    <form action="student_action.action">
        <input type="text" name="name"><br/>
        <input type="text" name="age"><br/>
        <input type="submit" value="提交">
    </form>
  </body>
</html>

13.搭建完成,部署,启动服务。访问:http://127.0.0.1:8080/ssh/index.jsp

完整目录:这里写图片描述

项目源码下载地址:http://download.csdn.net/detail/qwcs163/9116763

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值