关于SSH整合时要用的一些模板

来源:互联网 发布:noteledge mac 编辑:程序博客网 时间:2024/06/05 21:01

首先是beans.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/>
 
  /*添加数据源*/
  <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/main?useUnicode=true&amp;characterEncoding=gbk">
</property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
  <property name="initialSize" value="1"/>
<property name="maxActive" value="500"/>
  <property name="maxIdle" value="2"/>
<property name="minIdle" value="1"/>
</bean>

       /*添加sessionfactory*/
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">          
    <list>
      <value></value>/*添加映射文件*/
    </list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect /* 数据库语言*/
</prop>
</props>
</property>
</bean>

      /*添加事务*/
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/></bean>

    /*添加事务注解*/
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
/*添加你需要配置的Bean*/
</beans>

Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="gbk"/>
    <constant name="struts.action.extension" value="action"/>
    <constant name="struts.serve.static.browserCache" value="false"/>
    <constant name="struts.configuration.xml.reload" value="true"/>
    <constant name="struts.devMode" value="false" />
<constant name="struts.objectFactory" value="spring" /> /*将struts交给spring管理*/
<package name="stu" namespace="/" extends="struts-default" > 
<action></action> /*t添加action*/   注意 :action中的class调用的是beans.xml的action的id;  不是包名加类名!
   
    </package> 
</struts>

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">

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
                 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <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>
  
</web-app>


0 0
原创粉丝点击