-Moving AjaxHandler to spring-js

-Polishing Spring.js API
-Refactoring spring-faces components to match Spring.js API changes.
This commit is contained in:
Jeremy Grelle
2008-04-22 22:28:11 +00:00
parent 0f3d983587
commit 7d3e1d611b
23 changed files with 308 additions and 215 deletions

View File

@@ -0,0 +1,77 @@
package org.springframework.js.ajax.view;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
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.Definition;
import org.apache.tiles.Attribute.AttributeType;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.impl.BasicTilesContainer;
import org.springframework.js.mvc.servlet.AjaxHandler;
import org.springframework.js.mvc.servlet.SpringJavascriptAjaxHandler;
import org.springframework.web.servlet.support.JstlUtils;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.view.tiles2.TilesView;
public class TilesAjaxView extends TilesView {
public static final String FRAGMENTS_PARAM = "fragments";
private AjaxHandler ajaxHandler = new SpringJavascriptAjaxHandler();
protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
throws Exception {
ServletContext servletContext = getServletContext();
if (ajaxHandler.isAjaxRequest(servletContext, request, response)) {
BasicTilesContainer container = (BasicTilesContainer) TilesAccess.getContainer(servletContext);
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));
TilesRequestContext tilesRequestContext = container.getContextFactory().createRequestContext(
container.getApplicationContext(), new Object[] { request, response });
Definition compositeDefinition = container.getDefinitionsFactory().getDefinition(getUrl(),
tilesRequestContext);
Map flattenedAttributeMap = new HashMap();
flattenAttributeMap(container, tilesRequestContext, flattenedAttributeMap, compositeDefinition);
String attrName = request.getParameter(FRAGMENTS_PARAM);
Attribute attributeToRender = (Attribute) flattenedAttributeMap.get(attrName);
container.render(attributeToRender, response.getWriter(), new Object[] { request, response });
} else {
super.renderMergedOutputModel(model, request, response);
}
}
private void flattenAttributeMap(BasicTilesContainer container, TilesRequestContext requestContext, Map resultMap,
Definition compositeDefinition) throws Exception {
Iterator i = compositeDefinition.getAttributes().keySet().iterator();
while (i.hasNext()) {
Object key = i.next();
Attribute attr = (Attribute) compositeDefinition.getAttributes().get(key);
if (attr.getType() == AttributeType.DEFINITION) {
Definition nestedDefinition = container.getDefinitionsFactory().getDefinition(
attr.getValue().toString(), requestContext);
flattenAttributeMap(container, requestContext, resultMap, nestedDefinition);
} else {
resultMap.put(key, attr);
}
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.
*/
package org.springframework.js.mvc.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Strategy interface that encapsulates knowledge about a client-side ajax system and how to communicate with that
* system.
* @author Keith Donald
*/
public interface AjaxHandler {
/**
* Is the current request an Ajax request?
* @param request the current request
*/
public boolean isAjaxRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response);
/**
* Send a redirect request to the Ajax client. This should cause the client-side agent to send a new request to the
* specified target url.
* @param response the response object
* @param targetUrl the target url to redirect to
* @param popup wheter the redirect should be sent from a new popup dialog window
*/
public void sendAjaxRedirect(ServletContext context, HttpServletRequest request, HttpServletResponse response,
String targetUrl, boolean popup) throws IOException;
}

View File

@@ -0,0 +1,72 @@
/*
* 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.
*/
package org.springframework.js.mvc.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;
/**
* Ajax handler for Spring Javascript (Spring.js).
*
* @author Jeremy Grelle
* @author Keith Donald
*/
public class SpringJavascriptAjaxHandler implements AjaxHandler {
/**
* The response header to be set on an Ajax redirect
*/
public static final String REDIRECT_URL_HEADER = "Spring-Redirect-URL";
/**
* The response header to be set on an redirect that should be issued from a popup window.
*/
public static final String POPUP_VIEW_HEADER = "Spring-Modal-View";
/**
* The accept header value that signifies an Ajax request.
*/
public static final String AJAX_ACCEPT_CONTENT_TYPE = "text/html;type=ajax";
/**
* Alternate request parameter to indicate an Ajax request for cases when control of the header is not available.
*/
public static final String AJAX_SOURCE_PARAM = "ajaxSource";
public boolean isAjaxRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
String acceptHeader = request.getHeader("Accept");
String ajaxParam = request.getParameter(AJAX_SOURCE_PARAM);
if (AJAX_ACCEPT_CONTENT_TYPE.equals(acceptHeader) || StringUtils.hasText(ajaxParam)) {
return true;
} else {
return false;
}
}
public void sendAjaxRedirect(ServletContext context, HttpServletRequest request, HttpServletResponse response,
String targetUrl, boolean popup) throws IOException {
if (popup) {
response.setHeader(POPUP_VIEW_HEADER, "true");
}
response.setHeader(REDIRECT_URL_HEADER, response.encodeRedirectURL(targetUrl));
}
}