如何使用 struts-spring 插件配置 struts 约定插件,并使用注释映射操作 class

How do configure struts convention plugin with struts-spring plugin with Action class mapped with annotations

我正在尝试使用已经是 运行 约定插件的 strut 2 应用程序配置 spring 插件,因此我正在使用注释。我正在使用 ExtJs 提交表单,在我引入 spring 插件之前它最初运行良好,现在 ajax 请求无法定位操作并且它在 firebug 中没有显示任何响应。

pom.xml

    <!-- struts 2 dependencies -->
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>${struts2.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-convention-plugin</artifactId>
    <version>${struts2.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-json-plugin</artifactId>
    <version>${struts2.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-spring-plugin</artifactId>
    <version>${struts2.version}</version>
</dependency>

<!-- Import the CDI API -->
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-cdi-plugin</artifactId>
    <version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>1.0-SP1</version><!--$NO-MVN-MAN-VER$-->
    <scope>provided</scope>
</dependency>

<!-- Spring framework -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring.version}</version>
</dependency>

struts.xml

<?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.custom.i18n.resources" value="ApplicationResources" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.convention.result.path" value="/content" />
    <constant name="struts.multipart.saveDir" value="/tmp" />
    <constant name="struts.multipart.maxSize" value="4194304" />
    <constant name="struts.action.excludePattern" value="/api/.*?" />

</struts>

web.xml

<display-name>Application</display-name>
<welcome-file-list>
  <welcome-file>index.html</welcome-file>
</welcome-file-list>

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

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

<servlet>
  <servlet-name>Resteasy</servlet-name>
  <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>Resteasy</servlet-name>
  <url-pattern>/api/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
  <param-name>resteasy.servlet.mapping.prefix</param-name>
  <param-value>/api</param-value>
</context-param>
<listener>
  <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

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:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>

动作Class

@Namespace("/units")
@Result(
    type = "stream",
    params = {
        "inputName", "stream"
    }
)
public class PropertyTypeAction extends BaseAction implements ActionImpl{

    @PersistenceUnit
    private EntityManagerFactory emf;

    @Action(value="add")
    public String add() {
        .......

    }
}

Ajax 请求 firebug 报告,XML 选项卡

XML Parsing Error: no element found Location: moz-nullprincipal:{7fc640bd-f293-4956-8cf2-178765cec735} Line Number 1, Column 1:

我的问题是如何配置 struts-spring 插件以使用带有注释的 struts 约定插件。

您正在导入 struts2-spring-pluginstruts2-CDI-plugin

不要。

仅选择以上插件之一,然后正确配置:

  • 要使用 CDI 插件,只需使用 Maven 导入 JAR 并开始注释要使用 the (right) @Inject annotation 注入的对象。

  • 要使用 Spring 插件,导入 JAR,在 web.xml 中添加 ContextLoaderListener(您已经设置), 并在 struts.xml 中指定要 use Spring as objectFactory, 常数:

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

恕我直言 CDI plugin is the better option,如果您使用的是 Java EE >= 6.