从jsp页面向spring Controller发送Ajax请求

时间:2015-12-10 17:35:44

标签: ajax jsp spring-mvc arraylist

我正在尝试使用spring MVC框架从jsp页面调用ajax来从服务器获取一些数据,但是我收到以下错误。

10-Dec-2015 11:53:05.349 WARNING [http-nio-8084-exec-14] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/EIMEnterprise/[object%20Object]] in DispatcherServlet with name 'spring-dispatcher'

无论我的$ .get调用的URL参数是什么,它似乎都附加了[object%20Object]]值。这是我的Ajax控制器类

@Controller
public class AjaxController {

@RequestMapping(value="/viewhiers.html", method=RequestMethod.GET)
public @ResponseBody ArrayList<HierarchyBean> getHiersViaAjax(@RequestParam("versionID") int versionID) {
    //return an arraylist of HierarchyBean

}

}

下面是我的web.xml文件

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee               http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 version="3.1">

<display-name>EIMEnterprise</display-name>

<servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

以下是我的jsp页面,其中包含以下代码

<script type="text/javascript">
        $(document).ready(function() {
            $('#versiontab tr td').click(function() {
                //alert("Hello");
                //highlight the current row and disable highlight on the other rows
                $(this).closest("tr").addClass('highlightblue').siblings().removeClass('highlightblue');

                //make an ajax call to populate the hierarchies
                $.get({
                    url: '/EIMEnterprise/viewhiers.html?versionID=1',
                    type: 'GET',
                    success: function(data) {
                        alert("success");
                        /*
                        $.each(data, function (index, value) {
                            alert(index + " " + value);
                        });*/
                    }
                });
            });

        });

    </script>

这是我的spring-dispatcher-servlet.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!--Scan this package for any controller annotations-->
<context:component-scan base-package="com.eimenterprise.controllers" />

<!--Used for using pathvariable with map-->


<mvc:annotation-driven />
<mvc:resources location="/resources/" mapping="/resources/**" />

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

请让我知道我做错了什么,因为它似乎在urL的末尾添加了一个对象值,而不是将它发送到正确的控制器,因此无法找到映射。

1 个答案:

答案 0 :(得分:1)

我在$ .get中传递了错误的参数。这已经解决了。

应以下列方式使用$ .get。

$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});