ssh框架整合实例子1-配置文件

ssh框架整合实例子-配置文件


主要配置文件如下:


1.web.xml:

配置actionservlet类和映射
配置Listener,加载spring配置


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 
 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
 </context-param>
 
 
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

 

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


2.  struts-config.xml


 <?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
        "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>

 <form-beans>

<form-bean name="userForm"
   type="com.yanek.demo.ssh.bean.UserForm">
</form-bean>

 </form-beans>
 <global-forwards>
  <forward name="error" path="/error.jsp"></forward>
 </global-forwards>
 <action-mappings>
 

 
 <!--
  <action path="/user" name="userForm"
   type="com.yanek.demo.ssh.action.UserAction"
   scope="request" validate="true" input="/register.jsp">
   <forward name="success" path="/reg_success.jsp" />
  </action>
-->

  <action path="/user" name="userForm"
   type="org.springframework.web.struts.DelegatingActionProxy"
   scope="request" validate="false" input="/register.jsp"

parameter="method">
   <forward name="success" path="/reg_success.jsp" />
   <forward name="list" path="/list.jsp" />
  </action>

 </action-mappings>
 
     <message-resources parameter="ApplicationResources" />

</struts-config>


3.  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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 <!-- 定义数据源 -->
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  destroy-method="close">
  <!-- 指定连接数据库驱动 -->
  <property name="driverClassName"
   value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
  <!-- 指定连接数据库url -->
  <property name="url"
   value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ssh"

/>
  <!-- 指定连接数据库用户名,密码为空 -->
  <property name="username" value="sa" />
  <property name="password" value="" />
 </bean>
 <!-- 定义Hibernate的sessionFactory -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <!-- 指定数据源 -->
  <property name="dataSource" ref="dataSource" />
  <!-- 指定Hibernate映射文件 -->
  <property name="mappingResources">
   <list>
    <value>com/yanek/demo/ssh/vo/User.hbm.xml</value>

   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <!-- 指定使用方言 -->
    <prop key="hibernate.dialect">
     org.hibernate.dialect.SQLServerDialect
    </prop>
    <!-- 是否在控制台输出sql语句 -->
    <prop key="show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.jdbc.batch_size">20</prop>
   </props>
  </property>
 </bean>
 <!-- 定义事务管理 -->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 <!-- 定义事务管理拦截器 -->
 <bean id="transactionInterceptor"
  class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager" ref="transactionManager" />
  <property name="transactionAttributes">
   <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
 <!-- 定义代理自动管理事务 -->
 <bean id="ProxyCreator"
  

class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <!-- 指定需要Spring管理事务的Bean -->
  <property name="beanNames">
   <list>
    <value>userService</value>

   </list>
  </property>
  <!-- 调用事务管理拦截器 -->
  <property name="interceptorNames">
   <list>
    <value>transactionInterceptor</value>
   </list>
  </property>
 </bean>
<!-- 把DAO注入给Session工厂 -->

 <bean id="userDAO" class="com.yanek.demo.ssh.dao.UserDAOImpl">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>

<!-- 把Service注入给DAO -->


 <bean id="userService"
  class="com.yanek.demo.ssh.service.UserServiceImpl">
  <property name="userDAO" ref="userDAO"></property>
 </bean>

 

<!--  把Action注入给Service -->
 <bean name="/user"
  class="com.yanek.demo.ssh.action.UserAction">
  <property name="userService" ref="userService"></property>

 </bean>


</beans>

上一篇:Ubuntu下如何使用截图工具及自定义键盘快捷键


下一篇:聪明的程序员用Delphi,真正的程序员用C++,偷懒的程序员用PowerShell