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

On GET action in jsp to Spring Controller drops the url query string parameters

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

下面是我的 uploadSuccess.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>

Spring控制器方法是: //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 Status 400 错误 "Required String parameter 'fileName' is not present"。它正在从 ?向前。请指导。提前致谢。

正如我从您的代码中看到的那样 post 这部分可能是错误的:

<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}"/>" > 

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