1

我正在使用 Spring MVC、Tiles 2.2 和 jquery 来进行 ajax 调用,但是我的 js 文件中的 ajax 没有击中我的控制器。我如何正确配置它,因为这是我第一次尝试使用注释使用 spring mvc 进行 ajax 调用..

我的 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>

4

1 回答 1

1

为什么你使用@ResponseBodywithModelAndView

@ResponseBody如果您想根据内容类型将响应对象作为 JSON 或 XML 获取,则使用此选项。

ModelAndView如果您想使用视图框架 jsp 磁贴之一或其他任何东西来显示视图,则使用此选项。

对于 ajax 调用,您必须使用@ResponseBody并制作另一种方法来呈现您的瓷砖页面

于 2013-04-28T03:45:32.640 回答