struts+spring(2)

本文介绍如何通过Struts与Spring框架集成实现Action依赖注入,包括配置文件设置、使用非单例模式提高灵活性,并展示了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package prj23_1.action;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

 

import prj23_1.form.LoginForm;

import dao.IDAO;

 

public class LoginAction extends Action {//默认单态

      

       private IDAO dao;

       public ActionForward execute(ActionMapping mapping, ActionForm form,

                     HttpServletRequest request, HttpServletResponse response) {

              LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub

              String account = loginForm.getAccount();

              String password = loginForm.getPassword();

 

              dao.getCusomerByAccount(account);

             

              return new ActionForward("/login.jsp");

       }

      

      

       public IDAO getDao() {

              return dao;

       }

       public void setDao(IDAO dao) {

              this.dao = dao;

       }

}

action中定义一个接口对象。由接口对象去调 dao 中方法。

接口对象在spring框架中实例化

 

 

 

applicationContext.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

 

<beans>

    <bean id="dao" class="dao.CustomerDao"></bean>

    <!-- dao装配进LoginActiondao属性

       beannameStruts配置文件中actionpath相同

       相当于将action的生成完全交给Spring去作

       全权委托

       可以让Action以非单态形式运行,让Action更加灵活

     -->

    <bean name="/login" class="prj23_1.action.LoginAction" singleton="false">

       <property name="dao">

           <ref local="dao"/>

       </property>

    </bean>

</beans>

 

 

<?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="prj23_1.form.LoginForm" />

 

  </form-beans>

 

  <global-exceptions />

  <global-forwards />

  <action-mappings >

  <!-- Action的生成在这里截取掉,命令其在Spring框架中生成

    1:Struts框架认识Spring配置文件:配置插件

    2:用org.springframework.web.struts.DelegatingActionProxy类截获Action的生成

   -->

    <action

      attribute="loginForm"

      input="/login.jsp"

      name="loginForm"

      path="/login"

      scope="request"

      type="org.springframework.web.struts.DelegatingActionProxy" />

 

  </action-mappings>

 

  <message-resources parameter="prj23_1.ApplicationResources" />

 

  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

    <set-property property="contextConfigLocation"

                  value="/WEB-INF/applicationContext.xml" />

  </plug-in> 

</struts-config>

 

 

=======================================

 

不用在web.xml中注册spring框架。

上面的这种方法是最好,最常用的。

 

===========================================

 

还有一种方法。只修改struts配制文件

如下:

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="prj23_1.form.LoginForm" />

 

  </form-beans>

 

  <global-exceptions />

  <global-forwards />

  <action-mappings >

  <!-- Action的生成在这里截取掉,命令其在Spring框架中生成

    1:Struts框架认识Spring配置文件:配置插件

    2:配置RequestProcessor来截获

   -->

    <action

      attribute="loginForm"

      input="/login.jsp"

      name="loginForm"

      path="/login"

      scope="request"

      type="prj23_1.action.LoginAction" />

 

  </action-mappings>

//增加了这一句

 <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>

 

  <message-resources parameter="prj23_1.ApplicationResources" />

 <plug-in  className="org.springframework.web.struts.ContextLoaderPlugIn">

    <set-property property="contextConfigLocation"

                  value="/WEB-INF/applicationContext.xml" />

  </plug-in> 

</struts-config>

 

================================================

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值