Spring—struts2整合

来源:互联网 发布:客户无忧软件 编辑:程序博客网 时间:2024/06/03 07:26

1.整合步骤:

    一、整合目标
           Spring来管理Struts的Action
    二、新建一个web项目,配置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"
id="WebApp_ID" version="3.0">


<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


<!-- 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!-- 配置 Struts2 的 Filter 配置的可以在struts2的web.xml下blank项目下找到 -->
<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>

      三、加入相应的jar包:
           Spring 在WEB环境下应用,需要加入额外的两个jar包
           spring-web-4.0.4.RELEASE.jar
           spring-webmvc-4.0.4.RELEASE.jar

     四、Struts2配置文件(可在下载的Struts2的blank项目下找到)
              <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE struts PUBLIC
           "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>

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


<package name="default" extends="struts-default" namespace="/">
<!-- class是指向Spring里面的Bean的名字,不再是全类名,利用反射自己创建,而是将创建的工厂 指向Spring(利用struts2-spring-plugin-2.1.8.jar包实现) -->
<action name="person-save" class="personAction" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>

</struts>
             五、spring配置文件(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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


<bean id="person" class="com.struct2.beans.Person">
<property name="username" value="yebin"></property>
</bean>


<!-- 注意: 在IOC容器配置struts2的Action时,必须要这样配置 scope="prototype" -->
<bean id="personAction" class="com.struct2.action.PersonAction"
scope="prototype">
<property name="personService" ref="personService"></property>
</bean>


<bean id="personService" class="com.struct2.service.PersonService"></bean>


</beans>
        六、各个包的类文件
            1>、com.struct2.action下
                     PersonAction.java
                package com.struct2.action;


import com.struct2.service.PersonService;


/**
 * person的Action
 * 
 * @author yb
 *
 */
public class PersonAction {


// 添加PersonService注入
private PersonService personService;


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


public String execute() {


System.out.println("personAction....");
personService.save();
return "success";
}


}

     2>、com.struct2.beans下
              Person.java
       package com.struct2.beans;
/**
 * person类
 * @author yb
 *
 */
public class Person {


private  String username;

public void setUsername(String username) {
this.username = username;
}

public void hello(){
System.out.println("My name is"+ username);
}
}

    3>、com.struct2.service下
            PersonService.java
package com.struct2.service;


public class PersonService {


public void save(){
System.out.println("Personservice...save...");
}
}

0 0
原创粉丝点击