spring整合struts

整合目标:使用spring的bean管理struts action service。

整合步骤:

一、加入spring

1、加入spring jar包

2、配置web.xml文件

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

3、加入spring配置文件

二、加入struts2

1、加入struts2 jar包

2、在web.xml配置

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

 

 

2、加入struts2 对spring支持的插件包struts2-spring-plugin-2.3.31.jar

3、加入struts2配置文件

4、创建一个简单service和action

package com.hy.service;

public class PersonService {
    public void save() {
        System.out.println("personService save...");
    }
}
package com.hy.action;

import com.hy.service.PersonService;

public class PersonAction {
    
    private PersonService personService;
    
    
    
    public PersonService getPersonService() {
        return personService;
    }



    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }



    public String execute() {
        System.out.println("execute...");
        personService.save();
        return "success";
    }
}

5、在spring的配置文件中配置action和service

<bean name="personService" class="com.hy.service.PersonService"></bean>
    <bean name="personAction" class="com.hy.action.PersonAction" scope="prototype">
        <property name="personService" ref="personService"></property>
    </bean>

注意action的scope必须是prototype(非单例模式)

 

6、在struts2配置文件中配置action

<action name="person-save" class="personAction">
            <result>/success.jsp</result>
        </action>

7、创建测试页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<a href="person-save">person-save</a>
</body>
</html>

 

附录:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

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

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

    <bean name="person" class="com.hy.bean.Person">
        <property name="name" value="wanghai"></property>
    </bean>
    
    <bean name="personService" class="com.hy.service.PersonService"></bean>
    <bean name="personAction" class="com.hy.action.PersonAction" scope="prototype">
        <property name="personService" ref="personService"></property>
    </bean>
</beans>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" namespace="/" extends="struts-default">
        <action name="person-save" class="personAction">
            <result>/success.jsp</result>
        </action>
    </package>


</struts>

 

posted on 2016-12-07 02:28  _故乡的原风景  阅读(272)  评论(0编辑  收藏  举报