在 jsp 到 Spring 控制器的 GET 操作上,删除了 url 查询字符串参数



我没有将查询字符串参数从jsp获取到控制器。

以下是我的上传成功.jsp代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>File Upload Success</title>
</head>
<body>
<c:url var="formActionURL" value="http://localhost:8080/scanpipeline/readQRCode">
<c:param name="fileName" value="${fileName}" />
</c:url>
<form method="GET"  action="${formActionURL}" > 
<h3>
File Uploaded Successfully!
</h3>
<strong>File name is :<%= request.getAttribute("fileName") %> !!</strong> 
<strong>Total number of data read from file: <%=request.getAttribute("filedata") %> !! 
</strong><br>
<p></p> 
If you want to see QR Code information in the upload file, click 'Ok'   <br> 
<input type="submit" value="Ok">
</form>
</body>
</html>

弹簧控制器的方法为: http://localhost:8080/pipeline/readQRCode

@RequestMapping(value = "/readQRCode",  method = RequestMethod.GET)
public String readQRCode(Model model, @RequestParam(value = "fileName", required = true) String fileName)
{
........................
}

我在 jsp 的视图源代码中看到的 url 查询字符串是:操作="http://localhost:8080/pipeline/readQRCode?fileName=Paper+Scan.pdf">

但是,当我提交 jsp 表单时,它给了我 Http 状态 400 错误"必需的字符串参数'文件名'不存在"。 它正在从 ? 中删除查询字符串向前。 请指导。 提前谢谢。

正如

我从您发布的代码中看到的那样,这部分可能是错误的:

<c:url var="formActionURL" value="http://localhost:8080/scanpipeline/readQRCode">

因为当您提交时,您将被重定向到此 URL,因此您没有任何查询字符串。

尝试通过向表单添加具有查询字符串值的隐藏输入来进行更改。因此,作为一个 get 方法,此输入的值将作为查询字符串在您的 URL 中。

喜欢这个:

<form method="GET"  action="${formActionURL}" > 
<input type="hidden" value=${fileName} />
<h3>
File Uploaded Successfully!
</h3>

上次编辑:

在回答您的评论时,您的原始代码不正确,因为您以错误的方式使用标签。

这是正确的形式:

<c:url value="/index.jsp" var="myURL">
<c:param name="trackingId" value="1234"/>
<c:param name="reportType" value="summary"/>
</c:url>
<form method="GET"  action="<c:import url="${myURL}"/>" > 

因此,您也可以尝试将此解决方案应用于您的代码,当然您需要删除第一个解决方案。

最新更新