如何使用带有注释映射的Action类的struts-spring插件配置struts约定插件 [英] How do configure struts convention plugin with struts-spring plugin with Action class mapped with annotations

查看:93
本文介绍了如何使用带有注释映射的Action类的struts-spring插件配置struts约定插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用已经与约定插件一起运行的strut 2应用程序配置spring插件,所以我正在使用注释.我正在使用ExtJs进行表单提交,在我引入spring插件之前,最初一直运行良好,现在ajax请求无法找到操作,并且在firebug中未显示任何响应.

I am trying to configure spring plugin with strut 2 application which is already running with convention plugin, so I am using annotations. I am using ExtJs for my form submission which was initially working well until I introduced the spring plugin, now the the ajax request cannot locate the actions and it's not showing any response in 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>

动作类

@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常规插件一起使用.

My question is how can I configure struts-spring plugin to work with struts convention plugin with the annotations.

推荐答案

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

不要.

仅选择上述插件之一,然后对其进行正确配置:

Choose only one of the above plugins, and then configure it properly:

  • 要使用 CDI插件,只需使用Maven导入JAR并开始使用

  • To use the CDI plugin, simply import the JAR with Maven and start annotating the objects you want to be injected with the (right) @Inject annotation.

要使用 Spring插件,请导入JAR,在web.xml(已设置)中添加ContextLoaderListener,然后指定要

To use the Spring plugin, import the JAR, add the ContextLoaderListener in web.xml (that you have already set), and specify that you want to use Spring as objectFactory in struts.xml, with the constant:

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

恕我直言 CDI插件是更好的选择,如果您使用的是Java EE> = 6.

IMHO the CDI plugin is the better option, if you are using Java EE >= 6.

这篇关于如何使用带有注释映射的Action类的struts-spring插件配置struts约定插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆