Spring MVC 3 、AJAX、および apache タイルを統合する際に問題に直面しています。特に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>
ここで、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);
}
}
}
これがお役に立てば幸いです。
昨年の Spring WebFlow 2.4 には、org.springframework.js.ajax.tiles3.AjaxTilesView を拡張し、まだ Tiles 3 で動作する新しい FlowAjaxTiles3View が含まれています。「フラグメント」リクエスト パラメータを使用する以外に、フロー定義でレンダリング フラグメントを定義できます。
<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>