2012-05-19 99 views
1

我在Spring MVC中使用ajax時遇到了一些問題。我只想使用ajax刷新頁面(並從控制器中獲取頁面)。所以我在做什麼。Spring MVC和Ajax請求

$.ajax({          
    url : '/getCartProducts', 
    type : 'GET', 
    async: true, 
    data : {}, 
    success : function(data) { 
    //data shoud be rendered jsp with model from the controller 
    }, 
    error: function (jqXHR, textStatus, errorThrown) { 
    alert(jqXHR + " : " + textStatus + " : " + errorThrown); 
    } 
})` 

此功能應該給我從控制器的查看頁面。這是我的控制器。

@RequestMapping(value = "/getCartProducts", method = RequestMethod.GET) 
@ResponseBody String ajaxGetProdCart(HttpServletRequest request) { 
    LOG.trace("We are in the controller");  
    return "cart"; //this is jsp page 
} 

嘗試這樣做沒有@ResponseBody,但它不工作。它提醒我的錯誤:Not found

<bean id="validator" 
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> 
<bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/pages/" /> 
    <property name="suffix" value=".jsp" /> 
    <property name="contentType" value="text/html; charset=UTF-8" /> 
</bean> 

<bean id="messageSource" 
     class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="messages" /> 
    <property name="useCodeAsDefaultMessage" value="true" /> 
</bean> 

<bean id="jacksonMessageConverter" 
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
</bean> 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
    <property name="messageConverters"> 
    <list> 
     <ref bean="jacksonMessageConverter" /> 
    </list> 
    </property> 
</bean> 
+0

安置自己的servlet上下文XML爲好。檢查你的視圖解析器配置。你是否得到日誌跟蹤? –

+0

是的,這就是爲什麼我寫它,以檢查ajax是否去這個方法。我應該在解析器中寫什麼? –

+0

您的cart.jsp必須位於頁面文件夾內。如果您想要提供該jsp頁面,則必須刪除@ResponseBody。 –

回答

0

cart.jsp必須是內頁folder.You必須刪除@ResponseBody註釋

+0

嗯,它沒有這個註釋。但它使用JSTL標籤返回jsp頁面。但是我想要編譯頁面。 –

+0

此頁面已在頁面文件夾中。我做錯了什麼,或者我不知道什麼關於jstl渲染。 –

+0

修正了這個!我忘了在jsp頁面的頂部添加jstl庫 –