集成Spring MVC 3,AJAX和apache磁贴

时间:2013-09-10 07:15:21

标签: ajax spring tiles

我在整合Spring MVC 3,AJAX和apache tile时遇到了问题。特别是使用AJAX。 请为此建议一些链接。

我正在尝试使用包含搜索条件的另一个磁贴的ajax调用来加载磁贴上的结果。

提前致谢。

2 个答案:

答案 0 :(得分:0)

您需要重新配置:

<bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">
  <property name="viewClass" value="org.springframework.js.ajax.tiles3.AjaxTilesView"/>
</bean>

<bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" id="tilesConfigurer">
<property name="definitions">
      <list>
        <value>/WEB-INF/layouts/layouts.xml</value>
        <!-- Scan views directory for Tiles configurations -->
        <value>/WEB-INF/views/**/views.xml</value>
      </list>
    </property>
    </bean>

其中AjaxUrlBasedViewResolver位于spring-js-2.3.1-RELEASE.jar中 AjaxTilesView是一个基于org.springframework.js.ajax.tiles2.AjaxTilesView和org.apache.tiles.web.util.TilesDispatchServlet.doGet()的自定义实现:

package org.springframework.js.ajax.tiles3;

/*
 * Copyright 2004-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import javax.el.ELContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.VariableResolver;

import org.apache.tiles.Attribute;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.Definition;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.context.TilesRequestContextHolder;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.Request;
import org.apache.tiles.request.jsp.JspUtil;
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;

/**
 * Tiles view implementation that is able to handle partial rendering for Spring
 * Javascript Ajax requests.
 * 
 * <p>
 * This implementation uses the {@link SpringJavascriptAjaxHandler} by default
 * to determine whether the current request is an Ajax request. On an Ajax
 * request, a "fragments" parameter will be extracted from the request in order
 * to determine which attributes to render from the current tiles view.
 * </p>
 * 
 * @author Jeremy Grelle
 * @author David Winterfeldt
 */
public class AjaxTilesView extends TilesView {

    private static final String FRAGMENTS_PARAM = "fragments";

    private TilesRequestContextHolder tilesRequestContextFactory;

    private AjaxHandler ajaxHandler = new SpringJavascriptAjaxHandler();

    public void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();
        tilesRequestContextFactory = new TilesRequestContextHolder();
    }

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

            ApplicationContext tilesRequestContext = org.apache.tiles.request.servlet.ServletUtil
                    .getApplicationContext(getServletContext());
            ServletRequest servletRequest = new ServletRequest(tilesRequestContext,
                    request, response);
            TilesContainer container = TilesAccess.getContainer(tilesRequestContext);
            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));
            Definition compositeDefinition = container.getDefinition(getUrl(), servletRequest);

            Map flattenedAttributeMap = new HashMap();
            flattenAttributeMap(container, tilesRequestContext, flattenedAttributeMap, compositeDefinition,
                    servletRequest);
            addRuntimeAttributes(container, flattenedAttributeMap, servletRequest);

            if (fragmentsToRender.length > 1) {
                request.setAttribute(ServletRequest.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.inheritCascadedAttributes(compositeDefinition);
                    container.render(attributeToRender, servletRequest);
                    container.endContext(servletRequest);
                }
            }
        } 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(TilesContainer container, ApplicationContext requestContext, Map resultMap,
            Definition compositeDefinition, ServletRequest servletRequest) {
        Set<String> cascadedAttributeNames = compositeDefinition.getCascadedAttributeNames();
        Iterator iterator = null;
        if (cascadedAttributeNames ==null){
            iterator = compositeDefinition.getLocalAttributeNames().iterator();
        }else{
            iterator = cascadedAttributeNames.iterator();
        }
        while (iterator.hasNext()) {
            String attributeName = (String) iterator.next();
            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, servletRequest)) {
                resultMap.put(attributeName, attribute);
                Definition nestedDefinition = container.getDefinition(value, servletRequest);
                Assert.isTrue(nestedDefinition != compositeDefinition, "Circular nested definition: " + value);
                flattenAttributeMap(container, requestContext, resultMap, nestedDefinition, servletRequest);
            }
        }
    }

    /**
     * <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(TilesContainer container, Map resultMap, ServletRequest servletRequest) {
        AttributeContext attributeContext = container.getAttributeContext(servletRequest);
        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);
        }
    }
}

希望这对你有所帮助。

答案 1 :(得分:0)

Last Spring WebFlow 2.4包含一个新的FlowAjaxTiles3View,它扩展了org.springframework.js.ajax.tiles3.AjaxTilesView,并与Tiles 3一起使用。它允许在流定义中定义渲染片段,除了使用“片段”请求参数:

<bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.webflow.mvc.view.FlowAjaxTiles3View"/>
</bean>

此外,您不应忘记将您的视图工厂指向此viewResolver:

<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator" />

<!-- Configures Web Flow to use Tiles to create views for rendering; Tiles allows for applying consistent layouts to your views -->
<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
  <property name="viewResolvers" ref="tilesViewResolver"/> 
</bean>