无法从AJAX + JQuery + Jsp中获得弹簧控制器(Web服务)

时间:2015-06-25 06:15:42

标签: spring spring-mvc controller

我得到的问题就像在Title中提到的那样,尝试在stackoverflow上搜索但是我还没有得到任何解决方案。

我有一个Jsp页面

<!DOCTYPE html>
<html>
<head>
<title>CreateAccount</title>
<script src="https://ajax.googleapis.com/ajax/libs/ext-core/3.1.0/ext-core.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
    var delay = (function(){
          var timer = 0;
          return function(callback, ms){
          clearTimeout (timer);
          timer = setTimeout(callback, ms);
         };
        })();

    $(document).ready(function(){
        $("input.uname").keyup(function() {
              delay(function(){
                //alert('Hi, func called');
                 checkUsername();
              }, 1000 );
            });

    });
    function checkUsername() {

        alert('go');
        var requrl = "http://localhost:8082/VendorWebApplication/reqq3";

        var greeting = {
                  "id" : 1,
                  "content" :"prasad"
               }

        $.ajax({
                    type: 'POST',
                    url: requrl,
                    data:JSON.stringify(greeting),
                    dataType: "text",
                    //dataType: "json", //tried with this also
                    contentType: 'application/json',
                    success: function (data) {
                        debugger;
                        var json = $.parseJSON(data);
                        alert(json.content);

                            },
                    error: function (data) {
                        debugger;
                        alert("error"+data); 
                    }
                        }); 

               }
    </script>
</head>
<body>
   //body content
</body>
</html>

我想将Id,Content发送给spring controller

这是我的控制器类

@Controller
public class WebController {

    @RequestMapping(value ="/reqq3",method=RequestMethod.POST)
    public @ResponseBody Greeting reqq3(@RequestBody Greeting greeting) {
     System.out.println("omgcheck");
     System.out.println(greeting.getContent());
     System.out.println(greeting.getId());
        return new Greeting(1,"checkboss");
    }
}

但是我的ajax请求甚至没有打到它

我的web.xml

 <web-app id="WebApp_ID" version="2.4"
       xmlns="http://java.sun.com/xml/ns/j2ee" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
       http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

       <display-name>Spring MVC Application</display-name>
        <servlet>
         <servlet-name>VendorWebApplication</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
     </servlet>

      <servlet-mapping>
        <servlet-name>VendorWebApplication</servlet-name>
        <url-pattern>/</url-pattern>
        </servlet-mapping>

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

       </welcome-file-list>

    </web-app>

我的VendorWebApplication-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="com.arsenal.vendorappserver.controller"/>

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

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonConverter" />
        </list>
    </property>
</bean>

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json"></property>
</bean> 

我的Greeting.class

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

让我告诉你,当我发送带有数据的GET请求时它会触及控制器并捕获数据(使用method = RequestMethod.GET和all)。但是当我请求 method = POST 时, 没有点击

2 个答案:

答案 0 :(得分:0)

在servlet映射中,您是否尝试更改/ by *

  String sting1 = "IDs = \\";
    String sting2 = "11:22:33:44:55:66:77,\\";
    String sting3 = "11:22:33:44:55:66,\\";
    String sting4 = "1C:BA:8C:20:C5:0A";

    File file = new File("{filePath}");

    FileUtils.writeStringToFile(sting1, file, true);
    FileUtils.writeStringToFile(sting2, file, true);
    FileUtils.writeStringToFile(sting3,file,true);
    FileUtils.writeStringToFile(sting4,file,true);

答案 1 :(得分:0)

我有答案,它是硬编码的,但有效

 function checkUsername() {

        alert('go');
        var requrl ="http://localhost:8082/VendorWebApplication/checkUsername";

        var greeting = {
              "content" : "bhanu",
              "id" :15
           }

        $.ajax({
                    type: 'POST',
                    url: requrl,
                    data:search,
                    dataType:'text',
                    success: function (data) {

                        alert(data);

                            },
                    error: function (data) {

                        alert("error"+data); 
                    }
                        }); 


    } 

意味着我们在接收

时不会转换为JSON
@RequestMapping(value ="/checkUsername",method=RequestMethod.POST)
    public @ResponseBody String checkUsername(HttpServletRequest request) {
     String content = request.getParameter("content");
     int content = request.getParameter("id");
     return "heyya";
    }

这解决了我的问题,但它是硬编码的,它没有将Json 文件映射/转换为问候类。如果有人得到了完美答案,请在此处发布。