欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SSH整合(配置文件)

程序员文章站 2022-04-30 09:42:52
...
package com.haitao.services.imp;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.haitao.pojos.Category;
import com.haitao.services.categoryServiceInter;


@Transactional
public class categoryServiceImp implements categoryServiceInter {
    @Resource
    private SessionFactory sessionFactory;
    @Override
    public void addCategory(Category cg) {
    sessionFactory.getCurrentSession().save(cg);
    }
}

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="categoryForm" type="com.gaoruan.struts.form.CategoryForm" />
    <form-bean name="addBookForm" type="com.gaoruan.struts.form.AddBookForm" />
  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="categoryForm"
      input="/WEB-INF/webPages/addCategory.jsp"
      name="categoryForm"
      parameter="flag"
      path="/category">
      <set-property property="cancellable" value="false" />
      <forward name="error" path="/WEB-INF/webPages/addCateSucce.jsp" />
      <forward name="ok" path="/WEB-INF/webPages/addCateSucce.jsp" />
    </action>
    <action
      input="/WEB-INF/webPages/addCateSucce.jsp"
      parameter="flag"
      name="addBookForm"
      path="/book">

      <set-property property="cancellable" value="false" />
      <forward name="addBook" path="/WEB-INF/webPages/addBook.jsp" />
      <forward name="addSuccess" path="/WEB-INF/webPages/addBookSuccess.jsp" />

    </action>


  </action-mappings>


    <!-- 配置代理请求处理 DelegatingRequestProcessor -->
<controller>
   <set-property value="org.springframework.web.struts.DelegatingRequestProcessor" property="processorClass"/>
</controller>

  <message-resources parameter="com.gaoruan.struts.ApplicationResources" />


</struts-config>

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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context" 

    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                     http://www.springframework.org/schema/tx 
                     http://www.springframework.org/schema/tx/spring-tx.xsd 
                     http://www.springframework.org/schema/aop 
                     http://www.springframework.org/schema/aop/spring-aop.xsd
                      http://www.springframework.org/schema/context     
                     http://www.springframework.org/schema/context/spring-context-2.5.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/xuhaitao">
        </property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></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>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/haitao/pojos/Category.hbm.xml</value>
                <value>com/haitao/pojos/Book.hbm.xml</value></list>
        </property></bean>

    <!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>



    <!-- 启用注解方式 -->
<context:annotation-config/>



    <bean name="/category" class="com.gaoruan.struts.action.CategoryAction"  scope="prototype"/>
    <bean name="/book" class="com.gaoruan.struts.action.BookAction"  scope="prototype"/>    
    <bean class="com.haitao.services.imp.categoryServiceImp" id="categoryServiceImp" />
        <bean class="com.haitao.services.imp.bookService" id="bookService" />
    </beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name />
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>



  <filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


  <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

  <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>
</web-app>


FR:海涛高软(hunk Xu)