【问题标题】:Not able to hit spring controller(Web Service) from AJAX+JQuery+Jsp无法从 AJAX+JQuery+Jsp 访问 spring 控制器(Web 服务)
【发布时间】:2015-06-25 06:15:42
【问题描述】:

我遇到的问题是标题中提到的,也尝试在 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
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 请求时,它会击中控制器并捕获数据(使用方法 = RequestMethod.GET 和所有)。但是当我请求 method=POST 时, 没有击中

【问题讨论】:

    标签: spring spring-mvc controller


    【解决方案1】:

    在servlet映射中,有没有试过把/改成*

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

    【讨论】:

    • Server Tomcat v8.0 Server at localhost failed to start. 原因:java.lang.IllegalArgumentException: Invalid * in servlet mapping
    • 尝试更改票证 /* 是我在 web.xml 上的配置方式
    • 原因-HTTP 状态 404,顺便说一句,我认为这个 与我的问题无关
    • 很多人有这个问题是因为 servlet-mapping。抱歉帮不了你,祝你好运
    【解决方案2】:

    我有答案,它是硬编码的,但可以工作

     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 文件到 Greeting Class 。如果有人得到完美的答案,请在此处发布。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 2013-03-30
      • 2014-08-25
      • 1970-01-01
      相关资源
      最近更新 更多