Spring MVC和Ajax请求

时间:2012-05-19 08:54:45

标签: ajax spring-mvc

我在使用Spring MVC的ajax时遇到了一些问题。我想使用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>

1 个答案:

答案 0 :(得分:0)

您的cart.jsp必须是内页文件夹。您必须删除@ResponseBody注释