Q:

How to render ajax response to jsf page using springmvc as controller

I am on a project which uses Spring-MVC as a controller and JSF is responsible for rendering. I need to now implement some Ajax functionality into my page. What I am after is something like this:  

User clicks on button or "something" other clickable  
Ajax call is made to controller and until the response is returned some sort of panel with text "loading..." is rendered underneath the button  
The controller will process the request and returns the result as HTML. The result will be rendered into similar panel as the "loading ..." information was.

Step 3 is when I am lost. So far what I found is that I have to use plain JSP and tiles (Handling Ajax Requests with Spring MVC Controllers) for achieving such functionality "out of the box". But I need to use JSF and one of my XHTML fragments as a "template".
I already have AJAX resolver configured in my spring configuration file but that is used for returning JSON objects. Now I need to return the whole HTML. I want to use server to do the rendering rather then mocking it up from JSON object and paste the HTML to the correct place on the page. Is there a way how to "convince" org.springframework.faces.mvc.JsfView to work in AJAX request? This is my normal JSF resolver configuration:
<bean id="jsfViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.faces.mvc.JsfView" />   
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".xhtml" />
</bean>

And this is my JSON Ajax resolver configuration:
<bean id="ajaxViewResolver" class="com.myproject.web.springmvc.AjaxViewResolver">
    <property name="ajaxView">
        <bean class="com.myproject.web.springmvc.AjaxView" />
    </property>
    <property name="ajaxPrefix" value="ajax_"></property>
</bean>

Here is the code in my JSON AjaxViewResolver :
public class AjaxViewResolver extends AbstractCachingViewResolver {

private Logger logger = Logger.getLogger(AjaxViewResolver.class);

private String ajaxPrefix;

private View ajaxView;

@Override
protected View loadView(String viewName, Locale locale) throws Exception {
    logger.debug("loadView - enter");
    logger.debug("loadView - viewName : " + viewName);
    logger.debug("loadView - locale : " + locale);

    View view = null;
    if (viewName.startsWith(this.ajaxPrefix)) {
        view = ajaxView;
    }

    logger.debug("loadView - returns : " + view);
    return view;
}

public String getAjaxPrefix() {
    return ajaxPrefix;
}

public void setAjaxPrefix(String ajaxPrefix) {
    this.ajaxPrefix = ajaxPrefix;
}

public View getAjaxView() {
    return ajaxView;
}

public void setAjaxView(View ajaxView) {
    this.ajaxView = ajaxView;
}

}

Here is my JSON AjaxView class:
public class AjaxView extends AbstractView {
private Logger logger = Logger.getLogger(AjaxView.class);

/**
 * Serialises the modelMap into a JSON string and writes the string to the outputStream
 */
@Override
protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response) throws Exception {
    logger.debug("renderMergedOutputModel - enter");
    logger.debug("renderMergedOutputModel - map : " + map);
    logger.debug("renderMergedOutputModel - request : " + request);
    logger.debug("renderMergedOutputModel - response : " + response);

    JSONSerializer serializer = new JSONSerializer();
    serializer.exclude("*.class");
    String jsonString = serializer.deepSerialize(map);
    response.setContentType("text/plain; charset=UTF-8");
    response.getOutputStream().write(jsonString.getBytes());

    logger.debug("renderMergedOutputModel - response : " + response);
    logger.debug("renderMergedOutputModel - exit");
}

}
Any ideas?

A:

It looks like nobody ever needed this. I have spent last couple hours going through JSF and Spring MVC code and I came with one solution which is working for me. I would like to present it here and if you guys can do some peer review. Thanks.
The solution is to create new bean based on com.myproject.web.springmvc.AjaxViewResolver and into property ajaxView inject newly created class nz.co.bnz.olb.ib.web.springmvc.AjaxJsfView which extends org.springframework.faces.mvc.JsfView.
Here is the Spring configuration snipped:
<bean id="ajaxJsfViewResolver" class="nz.co.bnz.olb.ib.web.springmvc.AjaxViewResolver">
    <property name="ajaxView">
        <bean class="nz.co.bnz.olb.ib.web.springmvc.AjaxJsfView">
            <property name="prefix" value="/WEB-INF/pages/" />
            <property name="suffix" value=".xhtml" />
        </bean>
    </property>
    <property name="ajaxPrefix" value="ajaxJsf_"></property>
</bean>

As you can see I had to pass in prefix and suffix attribute to define how the URL for my XHTML files will be formed (necessary for JsfView). I have also created new prefix ajaxJsf_ for this resolver. Here is the new class:  
public class AjaxJsfView extends JsfView {
private Logger logger = Logger.getLogger(AjaxJsfView.class);

private String prefix;

private String suffix;

private String viewName;

@Override
protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response) throws Exception {
    logger.debug("renderMergedOutputModel - enter");
    logger.debug("renderMergedOutputModel - map : " + map);
    logger.debug("renderMergedOutputModel - request : " + request);
    logger.debug("renderMergedOutputModel - response : " + response);

    viewName = (String)map.get("view");

    super.renderMergedOutputModel(map, request, response);

    logger.debug("renderMergedOutputModel - response : " + response);
    logger.debug("renderMergedOutputModel - exit");
}

public String getPrefix() {
    return prefix;
}

public void setPrefix(String prefix) {
    this.prefix = prefix;
}

public String getSuffix() {
    return suffix;
}

public void setSuffix(String suffix) {
    this.suffix = suffix;
}

@Override
public String getUrl() {
    return getPrefix() + viewName + getSuffix();
}

@Override
public void setUrl(String arg0) {
    super.setUrl(arg0);
}
}

Definition of the actual file used as a 'view' can be seen in renderMergedOutputModel method. It is taken from our "model" from map under the key "view". The full path to the 'view' is implemented in overridden method getUrl(). So as a last step I had to implement my controller's method to return the map with expected value in it:
public ModelAndView getSomeTest(HttpServletRequest request, HttpServletResponse response){
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("view", "pathToMyXHTMLFile/someTest");
    map.put("name", "tomik");

    ModelAndView returnModelAndView = new ModelAndView("ajaxJsf_increaseLimit", map);

    return returnModelAndView;
}

And finally this is the XHTML used and eventually returned as HTML with populated values:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jstl/core">

    <p>Hello <h:outputText value="#{name}"/></p>
</ui:composition>

