在Spring mvc 3中使用AjaxUrlBasedViewResolver时发生NoClassDefFoundError

在Spring mvc 3中使用AjaxUrlBasedViewResolver时发生NoClassDefFoundError,ajax,spring,spring-mvc,tiles,Ajax,Spring,Spring Mvc,Tiles,有一段时间,我使用Spring框架开发web应用程序,一切都很好。然而最近我发现我不得不在我的页面中引入一些ajax调用。通过谷歌搜索,我发现Tiles2在接收ajax调用时,使用参数“fragments=nameOfTile”为解析视图提供了很好的支持。要实现这一点,您必须使用org.springframework.js.ajax.ajaxurlbasedviewresolvewhich和org.springframework.js.ajax.tiles2.AjaxTilesView 下面是我

有一段时间,我使用Spring框架开发web应用程序,一切都很好。然而最近我发现我不得不在我的页面中引入一些ajax调用。通过谷歌搜索,我发现Tiles2在接收ajax调用时,使用参数“fragments=nameOfTile”为解析视图提供了很好的支持。要实现这一点,您必须使用org.springframework.js.ajax.ajaxurlbasedviewresolvewhich和org.springframework.js.ajax.tiles2.AjaxTilesView

下面是我对视图解析器的配置请注意,我使用TILES.3.0.1:

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions" value="WEB-INF/tiles-config.xml"/>
</bean>
<bean id="ajaxViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver" p:order="0">
        <property name="viewClass" value="org.springframework.js.ajax.tiles2.AjaxTilesView"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" p:order="1">
</bean>
因此,我的问题是:

我可以将AjaxUrlBasedViewResolver与Tiles3一起使用吗? 如果是,如何解决我的问题,原因是什么? 如果没有,是否有“现成”的解决方案? 谢谢你的帮助, K


编辑1:@Bar说:你包括SpringWebFlow罐子了吗?springsource.org/spring-web-flowdownload

我使用maven作为依赖关系管理器。下面您可以看到my pom.xml:

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
      </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.webflow</groupId>
        <artifactId>spring-webflow</artifactId>
        <version>2.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator-annotation-processor</artifactId>
      <version>4.1.0.Final</version>
    </dependency>

所以应该包括在内

我认为您缺少tiles核心和tiles api依赖项

tiles api包含缺少的类

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-core</artifactId>
    <version>3.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-api</artifactId>
    <version>3.0.1</version>
</dependency>

我认为您缺少tiles核心和tiles api依赖项

tiles api包含缺少的类

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-core</artifactId>
    <version>3.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-api</artifactId>
    <version>3.0.1</version>
</dependency>

好的,我已经检测到问题了。自第3页以来,RequestContext上面的抽象已经改变,org.springframework.js.ajax.tiles2.AjaxTilesView使用的是旧的抽象。这就是为什么它不起作用,它引用的是不存在的类

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-core</artifactId>
    <version>3.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-api</artifactId>
    <version>3.0.1</version>
</dependency>
经过一些实验和搜索tails3javadocs,我成功地重写了这个ajaxtilesview类,并将其应用于tails3环境中。我已经做了一些测试,现在它运行良好。唯一的问题是,您必须在您的ajax请求中指定header或其他参数,这些参数将通知您这实际上是ajax调用。下面可以看到重写的AjaxTilesView类、配置示例和jquery调用示例

AjaxTilesView.Java:

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tiles.Attribute;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.Definition;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.impl.BasicTilesContainer;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.Request;
import org.apache.tiles.request.jsp.JspRequest;
import org.apache.tiles.request.servlet.ServletRequest;
import org.apache.tiles.request.servlet.ServletUtil;
import org.springframework.js.ajax.AjaxHandler;
import org.springframework.js.ajax.SpringJavascriptAjaxHandler;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.support.JstlUtils;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.view.tiles3.TilesView;

public class AjaxTilesView extends TilesView {

    private static final String FRAGMENTS_PARAM = "fragments";

    private ApplicationContext applicationContext;

    private AjaxHandler ajaxHandler = new SpringJavascriptAjaxHandler();

    public void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();
    }

    public AjaxHandler getAjaxHandler() {
        return ajaxHandler;
    }

    public void setAjaxHandler(AjaxHandler ajaxHandler) {
        this.ajaxHandler = ajaxHandler;
    }

    protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        ServletContext servletContext = getServletContext();
        if (ajaxHandler.isAjaxRequest(request, response)) {
            String[] fragmentsToRender = getRenderFragments(model, request, response);
            if (fragmentsToRender.length == 0) {
                logger.warn("An Ajax request was detected, but no fragments were specified to be re-rendered.  "
                        + "Falling back to full page render.  This can cause unpredictable results when processing "
                        + "the ajax response on the client.");
                super.renderMergedOutputModel(model, request, response);
                return;
            }

            this.applicationContext = ServletUtil.getApplicationContext(getServletContext());
            BasicTilesContainer container = (BasicTilesContainer) TilesAccess.getContainer(this.applicationContext);
            if (container == null) {
                throw new ServletException("Tiles container is not initialized. "
                        + "Have you added a TilesConfigurer to your web application context?");
            }

            exposeModelAsRequestAttributes(model, request);
            JstlUtils.exposeLocalizationContext(new RequestContext(request, servletContext));
            Request tilesRequestContext =  new ServletRequest(this.applicationContext, request, response);
            Definition compositeDefinition = container.getDefinitionsFactory().getDefinition(getUrl(),
                    tilesRequestContext);
            Map flattenedAttributeMap = new HashMap();
            flattenAttributeMap(container, tilesRequestContext, flattenedAttributeMap, compositeDefinition, request,
                    response);
            addRuntimeAttributes(container, flattenedAttributeMap, request, response);
            if (fragmentsToRender.length > 1) {
                request.setAttribute(JspRequest.FORCE_INCLUDE_ATTRIBUTE_NAME, true);
            }

            for (int i = 0; i < fragmentsToRender.length; i++) {
                Attribute attributeToRender = (Attribute) flattenedAttributeMap.get(fragmentsToRender[i]);

                if (attributeToRender == null) {
                    throw new ServletException("No tiles attribute with a name of '" + fragmentsToRender[i]
                            + "' could be found for the current view: " + this);
                } else {
                    container.startContext(tilesRequestContext).inheritCascadedAttributes(compositeDefinition);
                    container.render(attributeToRender, tilesRequestContext);
                    container.endContext(tilesRequestContext);
                }
            }
        } else {
            super.renderMergedOutputModel(model, request, response);
        }
    }

    protected String[] getRenderFragments(Map model, HttpServletRequest request, HttpServletResponse response) {
        String attrName = request.getParameter(FRAGMENTS_PARAM);
        String[] renderFragments = StringUtils.commaDelimitedListToStringArray(attrName);
        return StringUtils.trimArrayElements(renderFragments);
    }

    /**
     * <p>
     * Iterate over all attributes in the given Tiles definition. Every attribute value that represents a template (i.e.
     * start with "/") or is a nested definition is added to a Map. The method class itself recursively to traverse
     * nested definitions.
     * </p>
     * 
     * @param container the TilesContainer
     * @param requestContext the TilesRequestContext
     * @param resultMap the output Map where attributes of interest are added to.
     * @param compositeDefinition the definition to search for attributes of interest.
     * @param request the servlet request
     * @param response the servlet response
     */
    protected void flattenAttributeMap(BasicTilesContainer container, Request requestContext,
            Map resultMap, Definition compositeDefinition, HttpServletRequest request, HttpServletResponse response) {
        Set<String> locAttr = compositeDefinition.getLocalAttributeNames();
        Set<String> cascAttr = compositeDefinition.getCascadedAttributeNames();


        for (String s : locAttr) {
            String attributeName = s;
            Attribute attribute = compositeDefinition.getAttribute(attributeName);
            if (attribute.getValue() == null || !(attribute.getValue() instanceof String)) {
                continue;
            }
            String value = attribute.getValue().toString();
            if (value.startsWith("/")) {
                resultMap.put(attributeName, attribute);
            } else if (container.isValidDefinition(value, new ServletRequest(this.applicationContext, request, response))) {
                resultMap.put(attributeName, attribute);
                Definition nestedDefinition = container.getDefinitionsFactory().getDefinition(value, requestContext);
                Assert.isTrue(nestedDefinition != compositeDefinition, "Circular nested definition: " + value);
                flattenAttributeMap(container, requestContext, resultMap, nestedDefinition, request, response);
            }
        }

        if(cascAttr == null)
            return;

        for (String s : cascAttr) {
            String attributeName = s;
            System.out.println(s);
            Attribute attribute = compositeDefinition.getAttribute(attributeName);
            if (attribute.getValue() == null || !(attribute.getValue() instanceof String)) {
                continue;
            }
            String value = attribute.getValue().toString();
            if (value.startsWith("/")) {
                resultMap.put(attributeName, attribute);
            } else if (container.isValidDefinition(value, new ServletRequest(this.applicationContext, request, response))) {
                resultMap.put(attributeName, attribute);
                Definition nestedDefinition = container.getDefinitionsFactory().getDefinition(value, requestContext);
                Assert.isTrue(nestedDefinition != compositeDefinition, "Circular nested definition: " + value);
                flattenAttributeMap(container, requestContext, resultMap, nestedDefinition, request, response);
            }
        }


    }

    /**
     * <p>
     * Iterate over dynamically added Tiles attributes (see "Runtime Composition" in the Tiles documentation) and add
     * them to the output Map passed as input.
     * </p>
     * 
     * @param container the Tiles container
     * @param resultMap the output Map where attributes of interest are added to.
     * @param request the Servlet request
     * @param response the Servlet response
     */
    protected void addRuntimeAttributes(BasicTilesContainer container, Map resultMap, HttpServletRequest request,
            HttpServletResponse response) {
        AttributeContext attributeContext = container.getAttributeContext(new ServletRequest(this.applicationContext, request, response));
        Set attributeNames = new HashSet();
        if (attributeContext.getLocalAttributeNames() != null) {
            attributeNames.addAll(attributeContext.getLocalAttributeNames());
        }
        if (attributeContext.getCascadedAttributeNames() != null) {
            attributeNames.addAll(attributeContext.getCascadedAttributeNames());
        }
        Iterator iterator = attributeNames.iterator();
        while (iterator.hasNext()) {
            String name = (String) iterator.next();
            Attribute attr = attributeContext.getAttribute(name);
            resultMap.put(name, attr);
        }
    }
}
dispatcher-config.xml:

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions" value="WEB-INF/tiles-config.xml"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" p:order="1"/>
<bean id="ajaxViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver" p:order="0">
    <property name="viewClass" value="com.springframework.web.views.AjaxTilesView"/>
</bean>

好的,我已经检测到问题了。自第3页以来,RequestContext上面的抽象已经改变,org.springframework.js.ajax.tiles2.AjaxTilesView使用的是旧的抽象。这就是为什么它不起作用,它引用的是不存在的类

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-core</artifactId>
    <version>3.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-api</artifactId>
    <version>3.0.1</version>
</dependency>
经过一些实验和搜索tails3javadocs,我成功地重写了这个ajaxtilesview类,并将其应用于tails3环境中。我已经做了一些测试,现在它运行良好。唯一的问题是,您必须在您的ajax请求中指定header或其他参数,这些参数将通知您这实际上是ajax调用。下面可以看到重写的AjaxTilesView类、配置示例和jquery调用示例

AjaxTilesView.Java:

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tiles.Attribute;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.Definition;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.impl.BasicTilesContainer;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.Request;
import org.apache.tiles.request.jsp.JspRequest;
import org.apache.tiles.request.servlet.ServletRequest;
import org.apache.tiles.request.servlet.ServletUtil;
import org.springframework.js.ajax.AjaxHandler;
import org.springframework.js.ajax.SpringJavascriptAjaxHandler;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.support.JstlUtils;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.view.tiles3.TilesView;

public class AjaxTilesView extends TilesView {

    private static final String FRAGMENTS_PARAM = "fragments";

    private ApplicationContext applicationContext;

    private AjaxHandler ajaxHandler = new SpringJavascriptAjaxHandler();

    public void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();
    }

    public AjaxHandler getAjaxHandler() {
        return ajaxHandler;
    }

    public void setAjaxHandler(AjaxHandler ajaxHandler) {
        this.ajaxHandler = ajaxHandler;
    }

    protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        ServletContext servletContext = getServletContext();
        if (ajaxHandler.isAjaxRequest(request, response)) {
            String[] fragmentsToRender = getRenderFragments(model, request, response);
            if (fragmentsToRender.length == 0) {
                logger.warn("An Ajax request was detected, but no fragments were specified to be re-rendered.  "
                        + "Falling back to full page render.  This can cause unpredictable results when processing "
                        + "the ajax response on the client.");
                super.renderMergedOutputModel(model, request, response);
                return;
            }

            this.applicationContext = ServletUtil.getApplicationContext(getServletContext());
            BasicTilesContainer container = (BasicTilesContainer) TilesAccess.getContainer(this.applicationContext);
            if (container == null) {
                throw new ServletException("Tiles container is not initialized. "
                        + "Have you added a TilesConfigurer to your web application context?");
            }

            exposeModelAsRequestAttributes(model, request);
            JstlUtils.exposeLocalizationContext(new RequestContext(request, servletContext));
            Request tilesRequestContext =  new ServletRequest(this.applicationContext, request, response);
            Definition compositeDefinition = container.getDefinitionsFactory().getDefinition(getUrl(),
                    tilesRequestContext);
            Map flattenedAttributeMap = new HashMap();
            flattenAttributeMap(container, tilesRequestContext, flattenedAttributeMap, compositeDefinition, request,
                    response);
            addRuntimeAttributes(container, flattenedAttributeMap, request, response);
            if (fragmentsToRender.length > 1) {
                request.setAttribute(JspRequest.FORCE_INCLUDE_ATTRIBUTE_NAME, true);
            }

            for (int i = 0; i < fragmentsToRender.length; i++) {
                Attribute attributeToRender = (Attribute) flattenedAttributeMap.get(fragmentsToRender[i]);

                if (attributeToRender == null) {
                    throw new ServletException("No tiles attribute with a name of '" + fragmentsToRender[i]
                            + "' could be found for the current view: " + this);
                } else {
                    container.startContext(tilesRequestContext).inheritCascadedAttributes(compositeDefinition);
                    container.render(attributeToRender, tilesRequestContext);
                    container.endContext(tilesRequestContext);
                }
            }
        } else {
            super.renderMergedOutputModel(model, request, response);
        }
    }

    protected String[] getRenderFragments(Map model, HttpServletRequest request, HttpServletResponse response) {
        String attrName = request.getParameter(FRAGMENTS_PARAM);
        String[] renderFragments = StringUtils.commaDelimitedListToStringArray(attrName);
        return StringUtils.trimArrayElements(renderFragments);
    }

    /**
     * <p>
     * Iterate over all attributes in the given Tiles definition. Every attribute value that represents a template (i.e.
     * start with "/") or is a nested definition is added to a Map. The method class itself recursively to traverse
     * nested definitions.
     * </p>
     * 
     * @param container the TilesContainer
     * @param requestContext the TilesRequestContext
     * @param resultMap the output Map where attributes of interest are added to.
     * @param compositeDefinition the definition to search for attributes of interest.
     * @param request the servlet request
     * @param response the servlet response
     */
    protected void flattenAttributeMap(BasicTilesContainer container, Request requestContext,
            Map resultMap, Definition compositeDefinition, HttpServletRequest request, HttpServletResponse response) {
        Set<String> locAttr = compositeDefinition.getLocalAttributeNames();
        Set<String> cascAttr = compositeDefinition.getCascadedAttributeNames();


        for (String s : locAttr) {
            String attributeName = s;
            Attribute attribute = compositeDefinition.getAttribute(attributeName);
            if (attribute.getValue() == null || !(attribute.getValue() instanceof String)) {
                continue;
            }
            String value = attribute.getValue().toString();
            if (value.startsWith("/")) {
                resultMap.put(attributeName, attribute);
            } else if (container.isValidDefinition(value, new ServletRequest(this.applicationContext, request, response))) {
                resultMap.put(attributeName, attribute);
                Definition nestedDefinition = container.getDefinitionsFactory().getDefinition(value, requestContext);
                Assert.isTrue(nestedDefinition != compositeDefinition, "Circular nested definition: " + value);
                flattenAttributeMap(container, requestContext, resultMap, nestedDefinition, request, response);
            }
        }

        if(cascAttr == null)
            return;

        for (String s : cascAttr) {
            String attributeName = s;
            System.out.println(s);
            Attribute attribute = compositeDefinition.getAttribute(attributeName);
            if (attribute.getValue() == null || !(attribute.getValue() instanceof String)) {
                continue;
            }
            String value = attribute.getValue().toString();
            if (value.startsWith("/")) {
                resultMap.put(attributeName, attribute);
            } else if (container.isValidDefinition(value, new ServletRequest(this.applicationContext, request, response))) {
                resultMap.put(attributeName, attribute);
                Definition nestedDefinition = container.getDefinitionsFactory().getDefinition(value, requestContext);
                Assert.isTrue(nestedDefinition != compositeDefinition, "Circular nested definition: " + value);
                flattenAttributeMap(container, requestContext, resultMap, nestedDefinition, request, response);
            }
        }


    }

    /**
     * <p>
     * Iterate over dynamically added Tiles attributes (see "Runtime Composition" in the Tiles documentation) and add
     * them to the output Map passed as input.
     * </p>
     * 
     * @param container the Tiles container
     * @param resultMap the output Map where attributes of interest are added to.
     * @param request the Servlet request
     * @param response the Servlet response
     */
    protected void addRuntimeAttributes(BasicTilesContainer container, Map resultMap, HttpServletRequest request,
            HttpServletResponse response) {
        AttributeContext attributeContext = container.getAttributeContext(new ServletRequest(this.applicationContext, request, response));
        Set attributeNames = new HashSet();
        if (attributeContext.getLocalAttributeNames() != null) {
            attributeNames.addAll(attributeContext.getLocalAttributeNames());
        }
        if (attributeContext.getCascadedAttributeNames() != null) {
            attributeNames.addAll(attributeContext.getCascadedAttributeNames());
        }
        Iterator iterator = attributeNames.iterator();
        while (iterator.hasNext()) {
            String name = (String) iterator.next();
            Attribute attr = attributeContext.getAttribute(name);
            resultMap.put(name, attr);
        }
    }
}
dispatcher-config.xml:

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions" value="WEB-INF/tiles-config.xml"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" p:order="1"/>
<bean id="ajaxViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver" p:order="0">
    <property name="viewClass" value="com.springframework.web.views.AjaxTilesView"/>
</bean>

您是否包括SpringWebFlow罐@Bart检查我的编辑1是否包含spring webflow JAR@Bart检查我的编辑1不是这样的,tiles jsp依赖于这两个或更多库,它们由maven提供。只是为了确定,我尝试用你的代码片段手动添加这些依赖项,但仍然得到了相同的结果NoClassDefFoundError但是在tailes 3中似乎没有TileApplication Context这样的类,或者它位于不同的包中,因为我在org.apache.tilesWell中没有看到它,tiles jsp依赖于这两个或更多的库,它们由maven提供。为了确定,我尝试用你的代码片段手动添加这些依赖项,但仍然得到了相同的结果NoClassDefFoundError,但是在tailes 3中似乎没有TilesApplicationContext这样的类,或者它位于不同的包中,因为我在org.apache.tiles中没有看到它