views:

466

answers:

2

Hi,

I would like to add ajax to an existing spring mvc 2.5 webapps. But i dont know where to start.

I think spring does not support ajax integration.

Does someone know how can I accomplish this? I was thinking that my ajaxrequest should be catch by the controller interface but I dont know where to start. I dont want to use any ajax library at this point but just plain old ajax approach

Kindly send me links or tutorials if what I am thinking is possible please

+2  A: 

Spring MVC does not provide out of the box Ajax support. However it provides suitable extension points through its template based design to enable Ajax support very easily. Still you can implement it using jquery library.

Nirmal
Hi Nirmal,Thanks for giving time on this. I am a very newbie here, can you point me to a link where I could read about those topics related to the extension points.Would surely appreciate it. -- Mark
Mark Estrada
Check this http://www.jtraining.com/blogs/ajax-with-spring-mvc-and-jquery.html may be this can help you in much better way....
Nirmal
A: 

i am also facing the same issue. I have created the code as shown in the link above, but its returning whole HTML content...

Basically i have 2 view Resolvers now,

<bean id="ajaxViewResolver"
    class="com.infocast.iweb.mvc.view.ajax.AjaxViewResolver" >
    <property name="ajaxView">
        <bean class="com.infocast.iweb.mvc.view.ajax.AjaxView" />
    </property>
    <property name="ajaxPrefix" value="ajax_"></property>
    <property name="order" value="1"></property>       
</bean>

<!-- Default View Resolver for Spring MVC Portlet -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="exposeContextBeansAsAttributes" value="true"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

Now what happens is, when i call ajax using jquery i am getting whole http stuff in callback function.

var url = "<portlet:renderURL><portlet:param name='action' value='totalCountJson' /></portlet:renderURL>";
//var url = "stockscreener/total_match_count";
url +="&"+jQuery('input:hidden').serialize();
alert(url);
jQuery.ajax({ url: url,
            data: {},
            success: function(message) {
                alert(message);
                jQuery('input:hidden[@name="total_stock_items"]').val(message.totalMatches);                    
                jQuery('div.secondary-nav').children('div.viewMatches').
                    html('<a href="#" class="black_button rfloat" onclick="javascript:loadDetailsPage();">VIEW '+message.totalMatches+' MATCHES</a>');
            }, 
            error: function(xhr, ajaxOptions, thrownError) {
                alert(xhr.status + " - " + xhr.statusText);
            }
        }
    );  

I want to get it using JSON object, as per the AjaxView class form the link. So it should return the JSON object in the JS callback.

Is it regarding the view resolvers OR the proper content type not set? I use text/javascript as a contentType,

public class AjaxView extends AbstractView {
private static final Log log = LogFactory.getLog(AjaxView.class); 
/* (non-Javadoc)
 * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
@Override
protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response) 
throws Exception {
    log.info("Resolving ajax request view - "+map);        
    JSONObject jsonObj = new JSONObject(map);
    response.reset();
    response.setContentType( "text/javascript; charset=UTF-8" );
    response.getWriter().write(jsonObj.toString());
}

}

Please help..

Paarth