Spring:整合Struts1

本文介绍如何在Web应用中整合Struts与Spring框架。主要内容包括:在web.xml中配置Spring容器监听器及Struts控制器;定义Struts-config.xml文件以实现页面跳转和表单验证;在beans.xml中定义业务逻辑组件;并通过Spring注解方式注入依赖。此外,还展示了如何利用Spring提供的过滤器解决请求乱码问题。

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

在web.xml中声明初始化spring容器的监听器

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

在web.xml中声明struts1的控制器

<servlet>
        <servlet-name>struts</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>struts</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

Struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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="personForm" type="cn.itcast.web.formbean.PersonForm"/>
    </form-beans>
    <action-mappings>
        <action path="/person/list" validate="false">
            <forward name="list" path="/WEB-INF/page/personlist.jsp"/>
        </action>
        <action path="/person/manage" parameter="method"
        validate="false" scope="request" name="personForm">
            <forward name="message" path="/WEB-INF/page/message.jsp"/>
        </action>
    </action-mappings>
    
 <controller>
 <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>

    
</struts-config>

beans.xml中定义action

<bean name="/person/list" class="cn.itcast.web.action.PersonAction"/>

<bean name="/person/manage" class="cn.itcast.web.action.PersonManageAction"/>


action中通过注解将service注入

public class PersonAction extends Action {
    @Resource PersonService personService;

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {/*
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(
                this.getServlet().getServletContext());
        PersonService personService =(PersonService)ctx.getBean("personService");*/
        request.setAttribute("persons", personService.getPersons());
        return mapping.findForward("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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值