ssh整合

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="LoginForm" type="sshTest.form.LoginForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
  <action
   path="/login"
//用org.springframework.web.struts.DelegatingActionProxy类截获Action的生成, 将Action的生成在这里截取掉,命令其在Spring框架中生成
   type="org.springframework.web.struts.DelegatingActionProxy"
   name="LoginForm"
   scope="request"
   parameter="method"
   input="/loginFail.jsp">
<forward name="success" path="/loginSuccess.jsp"></forward>
</action>
  </action-mappings>
  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
*让Struts框架认识Spring配置文件方法一(在这里配置了就不用在web.xml中配置了)
// 配置该插件,让Struts框架认识Spring配置文件
  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-daos.xml,/WEB-INF/applicationContext-actions.xml" />
  </plug-in>

</struts-config>

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="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  *让Struts框架认识Spring配置文件方法二(在这里配置了就不用在struts-config.xml中配置了)
//1.加载Spring配置文件 2. Spring监听器注册  两个步骤让Struts框架认识Spring配置文件
//加载Spring配置文件
  <context-param>
    <param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-*.xml</param-value>
  </context-param>
//Spring监听器注册
  <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>
    <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>
</web-app>

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.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/sshdb">
           </property>

           <property name="username" value="root"></property>

           <property name="password" value="123210"></property>

</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--数据源 -->

<property name="dataSource">
<ref bean="dataSource" />
</property>

<!-- 在此加入hibernate.cfg.xml映射文件 -->

<property name="configLocation">
<value>/WEB-INF/hibernate.cfg.xml</value>
</property>

</bean>

<!-- 声明Hibernate事务管理,注入sessionFactory -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory">
       <ref local="sessionFactory"/>
     </property>
    </bean>

<!-- 配置事务代理,注入事务管理transactionManager,由Spring来代理事务,设置事务属性 -->
    <bean id="transactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
     <property name="transactionManager">
       <ref bean="transactionManager"/>
     </property>
     <property name="transactionAttributes">
       <props>
              <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
               <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
               <prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
               <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
               <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
               <prop key="create*">PROPAGATION_REQUIRED,-Exception</prop>
               <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
               <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
               <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
               <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
               <prop key="*">PROPAGATION_REQUIRED</prop>
       </props>
     </property>
    </bean>

</beans>

applicationContext-daos.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.5.xsd">
<!—将生成的sessionFactory注入到dao中-->
<bean id="cdao" class="sshTest.dao.impl.loginImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

</beans>

applicationContext-actions.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.5.xsd">
<!—生成action,将生成的dao注入到action中-->
<bean name="/login" class="sshTest.action.LoginAction">
<property name="cdao">
<ref bean="cdao" />
</property>
</bean>

</beans>

LoginAction.java 文件

在LoginAction中定义接口类型的cdao,并生成set和get函数,便可调用cdao。
private login cdao;
public login getCdao() {
return cdao;
}

public void setCdao(login cdao) {
this.cdao = cdao;
}
利用cdao调用接口方法
List list=cdao.denglu(username);

你可能感兴趣的:(spring,xml,struts,配置管理,ssh)