Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
集成SpringMVC3、AJAX和ApacheTiles_Ajax_Spring_Tiles - Fatal编程技术网

集成SpringMVC3、AJAX和ApacheTiles

集成SpringMVC3、AJAX和ApacheTiles,ajax,spring,tiles,Ajax,Spring,Tiles,我在集成SpringMVC3、AJAX和ApacheTiles时遇到了一些问题。特别是AJAX。 请提供一些链接 我正试图在包含搜索条件的另一个磁贴的ajax调用的帮助下,将结果加载到磁贴上 提前感谢。您需要重新配置: <bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver"> <property name="viewClass" value="org.

我在集成SpringMVC3、AJAX和ApacheTiles时遇到了一些问题。特别是AJAX。 请提供一些链接

我正试图在包含搜索条件的另一个磁贴的ajax调用的帮助下,将结果加载到磁贴上


提前感谢。

您需要重新配置:

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

/WEB-INF/layouts/layouts.xml
/WEB-INF/views/**/views.xml
其中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);
        }
    }
}
package org.springframework.js.ajax.tiles3;
/*
*版权所有2004-2008原作者。
*
*根据Apache许可证2.0版(以下简称“许可证”)获得许可;
*除非遵守许可证,否则不得使用此文件。
*您可以通过以下方式获得许可证副本:
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*/
导入java.util.Enumeration;
导入java.util.HashMap;
导入java.util.HashSet;
导入java.util.Iterator;
导入java.util.Map;
导入java.util.Set;
导入javax.el.ELContext;
导入javax.servlet.ServletContext;
导入javax.servlet.ServletException;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入javax.servlet.jsp.JspContext;
导入javax.servlet.jsp.JspWriter;
导入javax.servlet.jsp.el.ExpressionEvaluator;
导入javax.servlet.jsp.el.VariableResolver;
导入org.apache.tiles.Attribute;
导入org.apache.tiles.AttributeContext;
导入org.apache.tiles.Definition;
导入org.apache.tiles.tileContainer;
导入org.apache.tiles.access.tileAccess;
导入org.apache.tiles.context.tileRequestContextHolder;
导入org.apache.tiles.request.ApplicationContext;
导入org.apache.tiles.request.request;
导入org.apache.tiles.request.jsp.JspUtil;
导入org.apache.tiles.request.servlet.ServletRequest;
导入org.apache.tiles.request.servlet.ServletUtil;
导入org.springframework.js.ajax.AjaxHandler;
导入org.springframework.js.ajax.SpringJavascriptAjaxHandler;
导入org.springframework.util.Assert;
导入org.springframework.util.StringUtils;
导入org.springframework.web.servlet.support.JstlUtils;
导入org.springframework.web.servlet.support.RequestContext;
导入org.springframework.web.servlet.view.tiles3.TilesView;
/**
*能够为Spring处理部分渲染的平铺视图实现
*Javascript Ajax请求。
* 
*
*默认情况下,此实现使用{@link SpringJavascriptAjaxHandler}
*确定当前请求是否为Ajax请求。关于Ajax
*请求时,将按顺序从请求中提取“fragments”参数
*确定要从当前平铺视图渲染的属性。
*

* *@作者杰里米·格雷尔 *@作者David Winterfeldt */ 公共类AjaxTileView扩展了TileView{ 私有静态最终字符串片段\u PARAM=“FRAGMENTS”; 私有tileRequestContextHolder tileRequestContextFactory; 私有AjaxHandler AjaxHandler=新的SpringJavascriptAjaxHandler(); public void afterPropertieSet()引发异常{ super.afterPropertiesSet(); TileRequestContextFactory=新的TileRequestContextHolder(); } 公共AjaxHandler getAjaxHandler(){ 返回ajaxHandler; } 公共无效setAjaxHandler(AjaxHandler AjaxHandler){ this.ajaxHandler=ajaxHandler; } 受保护的void renderMergedOutputModel(映射模型、HttpServletRequest请求、HttpServletResponse响应) 抛出异常{ ServletContext=getServletContext(); if(ajaxHandler.isAjaxRequest(请求,响应)){ String[]fragmentsToRender=getRenderFragments(模型、请求、响应); if(fragmentsToRender.length==0){ warn(“检测到Ajax请求,但未指定要重新呈现的片段。” +“返回到整页呈现。这可能会在处理时导致不可预测的结果” +“客户端上的ajax响应。”); super.renderMergedOutputModel(模型、请求、响应); 返回; } ApplicationContext TileRequestContext=org.apache.tiles.request.servlet.ServletUtil .getApplicationContext(getServletContext()); ServletRequest ServletRequest=新的ServletRequest(TileRequestContext, 请求、答复); TileContainer container=TileAccess.getContainer(TileRequestContext); if(容器==null){ 抛出新的ServletException(“Tiles容器未初始化。” +“是否已将TileConfiguration添加到web应用程序上下文?”; } exposeModelAsRequestAttributes(模型、请求); exposeLocalizationContext(新的RequestContext(request,servletContext)); 定义compositeDefinition=container.getDefinition(getUrl(),servletRequest); Map FlattedAttributeMap=新HashMap(); FlatteAttributeMap(容器、TileRequestContext、FlattedAttributeMap、复合定义、, 服务请求); addRuntimeAttributes(容器、FlattedAttributeMap、servletRequest); 如果(fragmentsToRender.length>1){ setAttribute(ServletRequest.FORCE_INCLUDE_ATTRIBUTE_NAME,true); }
<bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.webflow.mvc.view.FlowAjaxTiles3View"/>
</bean>
<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>