【问题标题】:My Call Spring Controller failed我的呼叫弹簧控制器失败
【发布时间】:2016-09-08 08:02:33
【问题描述】:

在我的 HTML 中,我有这个按钮:

<button id="download_button">Confirmer</button>

在我的 Js 中我有这个方法:

$("#download_button").click(function() {
   downloadMes();
});

    downloadMes: function(){
        var sources = this.map.getLayers().getArray().map(function(layer){
            var source_url = layer.getSource().getUrls()[0];
            var layer_name = layer.getSource().getParams()['LAYERS'];
            var request = '?VERSION=1.1.0&OUTPUTFORMAT=CSV';
            if(_.includes(this.downloadSources, source_url)){
                return source_url + request + '&TYPENAME=' + layer_name;
            }
        },this);
        var sourcesToCall = _.compact(sources);
        var jsonfile= {json:JSON.stringify(sourcesToCall)};
        $.ajax({
            type:'POST',
            url: "/downloadCSV",
            data: jsonfile,
            dataType: "json"
        });
    }

我已经创建了一个 Spring Controller --> DownloadCSVController :

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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


@Controller
public class DownloadCSVController {



    @RequestMapping(value = "/downloadCSV", method = RequestMethod.GET)
    public void getCSV(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException{

        System.out.println("DEBUT TELECHARGEMENT");
        // SOME LOGIC
        System.out.println("FIN TELECHARGEMENT");

    }
}

当我点击我的按钮时出现此错误:

POST http://localhost:8080/downloadCSV 404 (Introuvable)

怎么了?

【问题讨论】:

  • 在 js 中更改 $ajax 类型以在 spring 中获取或发布
  • 由于是发布数据,需要将控制器方法改为method=RequestMethod.POST

标签: javascript java spring spring-mvc controller


【解决方案1】:

将您的 ajax 调用更改为像这样的 GET 方法

$.ajax({
            type:'GEt',
            url: "/downloadCSV",
            data: jsonfile,
            dataType: "json"
        });

或更改弹簧控制器方法以发布

 @RequestMapping(value = "/downloadCSV", method = RequestMethod.POST)
    public void getCSV(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException{

        System.out.println("DEBUT TELECHARGEMENT");
        // SOME LOGIC
        System.out.println("FIN TELECHARGEMENT");

    }

PS:如果您的项目有一些根文件夹并且基于调度程序 servlet url 映射也指定了它的名称,同时请求类似 localhost:8080/yourproject/downloadCSV

【讨论】:

  • 是的,我有一个 root 傻瓜,我在我的 ajax 或我的控制器根文件夹中指定了这个:project-web
  • 在 ajax 调用 /yourfolder/downloadCSV
  • 在此之前您的调度程序 servlet url 映射是什么?
  • 我不明白
  • 在你的 web.xml 中 org.springframework.web.servlet.DispatcherServlet 的 url 映射是什么
【解决方案2】:

您尝试发布数据以获取 url。所以换个控制器试试这个

    import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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


@Controller
public class DownloadCSVController {



    @RequestMapping(value = "/downloadCSV", method = RequestMethod.POST)
    public void getCSV(HttpServletRequest request, HttpServletResponse response, @RequestBody ModelMap model) throws IOException{

        System.out.println("DEBUT TELECHARGEMENT");
        // SOME LOGIC
        System.out.println("FIN TELECHARGEMENT");

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 2014-10-28
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    相关资源
    最近更新 更多