Ajax调用Dispatcher servlet

时间:2016-09-17 06:46:18

标签: ajax spring-mvc

我正在尝试从JSP对调度程序servlet进行简单的AJAX调用并获取字符串作为响应。但是,我能够在jsp中使用表单标记调用调度程序servlet并且成功页面被调用。如果我使用ajax调用则是甚至没有到达调度员servlet。请找到参考代码。我只是想知道我是否正确地做到了。我的目标是获得AJAX响应

test.jsp的

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tabs - Default functionality</title>
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/jquery.jqGrid.min.js"></script>
<script>
 $(document).ready(function() {
  $("#btn1").click(function(){
        $.ajax({
            type: "Post",  
            url: "hello",
            success: function(resp){
             alert(resp)
            }
      });
    });
  });
</script>
</head>
<body>
<input type ="button" id="btn1" />
</body>
</html>

HelloController.java

package test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{

@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public String printHello(ModelMap model) {
  return "success";
}
}

HelloWeb-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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">

<context:component-scan base-package="test" />
<bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/" />
  <property name="suffix" value=".jsp" />
</bean>
</beans>

1 个答案:

答案 0 :(得分:0)

如果您正在使用Ajax呼叫,这无关紧要,请求应该到达所需的控制器。

话虽如此,以及您写的请求甚至无法到达目的地的事实,那么您的问题就在您的网址中。

我建议使用 Spring Url标记 ,它是Spring标记库的一部分。它将帮助您在JSP文件中构建URL。

在您的网址示例中,您显然错过了完整的网址,我认为应该是:http://localhost:8080/your_project_name/hello

所以就这样做:

  1. 将下一个指令添加到JSP文件中:

    <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

  2. 在Ajax调用中使用spring url标记:
  3. $.ajax ({ type: "Post", url: '<spring:url value="/hello"/>', success: function(resp){ alert(resp) } });

    所以JSP的最终结果应该是:

        <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
            pageEncoding="ISO-8859-1"%>
        <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
        <!doctype html>
        <html lang="en">
        <head>
        <meta charset="utf-8">
        <title>jQuery UI Tabs - Default functionality</title>
        <script src="js/jquery-1.10.2.js"></script>
        <script src="js/jquery-ui.js"></script>
        <script src="js/jquery.jqGrid.min.js"></script>
        <script>
         $(document).ready(function() {
          $("#btn1").click(function(){
                $.ajax
                  ({
                      type: "Post",
                      url: '<spring:url value="/hello"/>',
                      success: function(resp){
                         alert(resp)
                      }
                   });
            });
          });
        </script>
        </head>
        <body>
        <input type ="button" id="btn1" />
        </body>
        </html>