2013-04-28 88 views
1

我正在使用Spring MVC,瓷砖2.2和jQuery使ajax调用,但我的AJAX在我的js文件没有击中我的控制器。我如何正确地配置它,因为这是我第一次试图与使用Spring MVC的注解Ajax调用..ajax调用不打我弹簧mvc控制器使用它与瓷砖2.2

我的JavaScript文件,使jQuery的AJAX调用

function showSubCategory(foodCategoryId){ 
    var params = {foodCategoryId : foodCategoryId}; 
    $.ajax({ 
     type : GET, 
     url : "subMenu.json", 
     dataType : "json", 
     contentType : "application/json", 
     data : params, 
     success : 
      alert("success" + data), 
     error : showError() 
    }); 
} 

function showError(){ 
    alert("An error occured."); 
} 

这是我的控制器

@Controller 
@SessionAttributes 
public class MenuController { 

    private static Logger logger = Logger.getLogger("MenuController.class"); 

    @Autowired 
    private MenuService menuService; 

    @RequestMapping(value="/subMenu") 
    public @ResponseBody ModelAndView showSubCategories(HttpServletRequest req, HttpServletResponse res){ 
     String foodCategoryId = req.getParameter("foodCategoryId"); 
     logger.info("hitting showSubcatregories controller " + foodCategoryId); 
     ModelAndView mav = new ModelAndView("ajax.menu"); 

     mav.addObject("subCategories", menuService.getMenuByFoodCategory(1L, Long.parseLong(foodCategoryId))); 
     return mav; 
    } 

} 

我tiles.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE tiles-definitions PUBLIC 
     "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN" 
     "http://tiles.apache.org/dtds/tiles-config_2_1.dtd"> 
<tiles-definitions> 
    <definition name="base.definition" template="/jsp/layout.jsp"> 
     <put-attribute name="title" value="" /> 
     <put-attribute name="header" value="/jsp/header.jsp" /> 
     <put-attribute name="menu" value="/jsp/menubar.jsp" /> 
     <put-attribute name="body" value="" /> 
     <put-attribute name="footer" value="/jsp/footer.jsp" /> 
    </definition> 

    <definition name="menu" extends="base.definition"> 
     <put-attribute name="title" value="Menu"/> 
     <put-attribute name="body"> 
      <definition template="/jsp/body/menu.jsp"> 
       <put-attribute name="body.subcategory" value="/jsp/body/subcategory.jsp"/> 
      </definition> 
     </put-attribute> 
    </definition> 

    <definition name="ajax.menu" template="/jsp/body/menu.jsp"> 
     <put-attribute name="body.subcategory" value="/jsp/body/subcategory.jsp"/> 
    </definition> 

</tiles-definitions> 

的web.xml

<?xml version="1.0" encoding="ISO-8859-1"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 

    <servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 

    <servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>*.json</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

我的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springframework.org/schema/jee 
     http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 

    <mvc:annotation-driven/> 
    <context:annotation-config/> 
    <context:component-scan base-package="com.res"/> 

    <bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver"> 
     <property name="viewClass" value="org.springframework.js.ajax.tiles2.AjaxTilesView"/> 
    </bean> 

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> 
     <property name="definitions"> 
      <list> 
       <value>/WEB-INF/spring/spring-tiles.xml</value> 
      </list> 
     </property> 
    </bean> 

回答

1

,如果你想根据内容类型来获得响应对象为JSON或XML为什么你正在使用@ResponseBody与使用ModelAndView

@ResponseBody

ModelAndView如果您想使用视图框架jsp图块或任何其他东西中的一个来显示视图,请使用它。

对于ajax调用你必须使用@ResponseBody并使另一种方法来渲染你的瓷砖页面