http://www.cnblogs.com/amosli/p/3577645.html

一 . 需求

学了这么久的ssh,一直都是别人整合好的框架去写代码,自己实际动手时才发现框架配置真是很坑爹,一不小心就踏错,真是纸上得来终觉浅!

本文将记录整合struts + spring的过程 ,都是采用目前最新的 jar包,spring 是4.0.2, struts是2.3.16版本.

本篇文章是在上一篇文章 使用spring 4.0 + maven 构建超简单的web项目 的基础上完成的,把上一个项目copy一份,改一下名称即可,然后接着往下看。

二.实现

1.效果图

http://www.cnblogs.com/amosli/p/3577645.html_第1张图片

2.项目架构图:

 

3.实现代码:

pom.xml

复制代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.amos</groupId>
    <artifactId>ssh_integrated</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>ssh_integrated Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.16</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.16</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>ssh_integrated_struts2</finalName>
    </build>
</project>
复制代码

注:这里要注意一下引入的struts2的插件.

 

com.amos.web.HelloActionSpring.java 

复制代码
package com.amos.web;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.amos.service.IHello;
import com.opensymphony.xwork2.ActionContext;

public class HelloActionSpring {
    private IHello hello;
    public String hellomethod(){
        ApplicationContext applicationContext  = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
        hello= applicationContext.getBean(IHello.class);
        String sayHi = hello.sayHi();
        ActionContext.getContext().put("msg", sayHi);
        return "SUCCESS";
    }
}
复制代码

注:这里使用的是手工去加载beans.xml,可以实现action的功能,但这种方式不推荐,没有完全实现spring和struts的集成.

 

com.amos.web.HelloAction.java

复制代码
package com.amos.web;

import com.amos.service.IHello;
import com.opensymphony.xwork2.ActionContext;

public class HelloAction {
    private IHello hello;
    public void setHello(IHello hello) {
        this.hello = hello;
    }
    public String hellomethod(){
        String sayHi = hello.sayHi();
        ActionContext.getContext().put("msg", sayHi);
        return "SUCCESS";
    }
}
复制代码

注:这里暴露一个set方法,让spring去注入,以达到spring管理struts的bean的目的.

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.objectFactory.spring.autoWire" value="type"></constant>
    <package name="hellopackage" extends="struts-default"
        namespace="/">
        <action name="helloaction" class="com.amos.web.HelloAction"
            method="hellomethod">
            <result name="SUCCESS" type="dispatcher">
                success.jsp
            </result>
        </action>
    </package>
</struts>
复制代码

注:spring和struts集成要用到sturts2的一个插件如下所示:

struts2-spring-plugin-2.3.16-sources.jar/struts-plugin.xml

<constant name="struts.objectFactory" value="spring" />

 这里就是把struts的bean交给spring管理.

 struts2-core-2.3.16-sources.jar/org/apache/struts2/default.properties

### valid values are: name, type, auto, and constructor (name is the default)

struts.objectFactory.spring.autoWire = type

这里表示注入的方式是根据类型,因为类型一般只有一个,但名称有可能会重复.

 

4.遇到的问题

1).异常如下:

复制代码
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ssh_integrated]]
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
    at java.util.concurrent.FutureTask.get(FutureTask.java:111)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ssh_integrated]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 7 more
Caused by: java.lang.IllegalArgumentException: Filter mapping specifies an unknown filter name sturts2
    at org.apache.catalina.core.StandardContext.validateFilterMap(StandardContext.java:3040)
    at org.apache.catalina.core.StandardContext.addFilterMap(StandardContext.java:3005)
    at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1271)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1346)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:376)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 7 more
复制代码

 

写错单词了:strtus-default

 

2)异常如下:

复制代码
WARNING: Could not create JarEntryRevision for [jar:file:/home/amosli/develop/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ssh_integrated_struts2/WEB-INF/lib/struts2-spring-plugin-2.3.16.jar]!
java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
    at com.opensymphony.xwork2.util.fs.JarEntryRevision.build(JarEntryRevision.java:50)
    at com.opensymphony.xwork2.util.fs.DefaultFileManager.monitorFile(DefaultFileManager.java:97)
    at com.opensymphony.xwork2.util.fs.DefaultFileManager.loadFile(DefaultFileManager.java:74)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadConfigurationFiles(XmlConfigurationProvider.java:1011)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadDocuments(XmlConfigurationProvider.java:165)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.init(XmlConfigurationProvider.java:132)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:233)
复制代码

 

缺少jar包.pom.xml加上:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

 

5.本文源码

https://github.com/amosli/ssh_integrated_spring_struts2


你可能感兴趣的:(http://www.cnblogs.com/amosli/p/3577645.html)