我在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>
安置自己的servlet上下文XML爲好。檢查你的視圖解析器配置。你是否得到日誌跟蹤? –
是的,這就是爲什麼我寫它,以檢查ajax是否去這個方法。我應該在解析器中寫什麼? –
您的cart.jsp必須位於頁面文件夾內。如果您想要提供該jsp頁面,則必須刪除@ResponseBody。 –