Javascript 如何从java脚本函数中调用spring服务(RequestParam)(错误400:必需的字符串参数';标志';不存在)
我想从脚本函数调用spring服务,但我无法为下面的代码调用我的服务。你能告诉我我做的是对的还是错的吗Javascript 如何从java脚本函数中调用spring服务(RequestParam)(错误400:必需的字符串参数';标志';不存在),javascript,java,Javascript,Java,我想从脚本函数调用spring服务,但我无法为下面的代码调用我的服务。你能告诉我我做的是对的还是错的吗 Controller class: @RequestMapping(value = "/download", method = { RequestMethod.GET, RequestMethod.POST }) @ResponseStatus(value = HttpStatus.OK) public String downloadFile(@RequestParam
Controller class:
@RequestMapping(value = "/download", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseStatus(value = HttpStatus.OK)
public String downloadFile(@RequestParam String flag, HttpServletResponse response) throws IOException {
return flag;
}
<script>
function downloadPdf(flag) {
if (flag == 'PRINT') {
redirectUrl = "download.htm/flag=" + flag;
var myWindow = window.open(redirectUrl , '',
'width=200,height=100');
myWindow.focus();
myWindow.print();
}
}
</script>
控制器类:
@RequestMapping(value=“/download”,method={RequestMethod.GET,RequestMethod.POST})
@ResponseStatus(值=HttpStatus.OK)
公共字符串下载文件(@RequestParam String标志,HttpServletResponse)引发IOException{
返回标志;
}
函数下载PDF(标志){
如果(标志=‘打印’){
redirectUrl=“download.htm/flag=”+flag;
var myWindow=window.open(重定向URL“”,
‘宽度=200,高度=100’;
myWindow.focus();
myWindow.print();
}
}
当运行我的代码时,我会遇到这样的问题
错误400:所需的字符串参数“flag”不存在
@RequestParam
标记为required=false
(默认值为true)download.html?flag=value
notdownload.html/flag=value
。在这个示例中,您看到的是@RequestParam
和@PathVariable
之间的奇怪混合您没有在重定向url中提供名为flag的字符串参数。这可能有助于理解如何向url添加查询字符串。