英文:
How to pass string from controller to form's post method as parameter?
问题
以下是翻译好的内容:
我面临了以下问题,这是我的控制器方法:
@GetMapping("/items/show/{id}")
public ModelAndView showExtendedInfo(@PathVariable int id) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/extendedInfo.html");
modelAndView.addObject("itemId", id);
return modelAndView;
}
在这里,我将 itemId
传递到视图中。同时,我在视图中有这样一个表单:
<form th:action="@{/items/buy?itemId=${id}}" method="post">
<input id="cardId" type="text" name="cardId"/>
<br/>
<input id="bonusesAmount" type="text" name="bonusesAmount"/>
<br/>
<button type="submit">购买</button>
</form>
所以,我需要将从控制器传递过来的参数再次传递给表单操作,然后再次将数据传递给控制器。但是,当我运行代码时,我遇到了以下错误:
java.lang.IllegalArgumentException: 请求目标[/items/buy?itemId=${id}]中发现无效字符。有效字符已在 RFC 7230 和 RFC 3986 中定义
org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:486)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:261)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:834)
据我所了解,它无法找到我的变量。但是,正确的方法是如何将变量值传递给表单操作呢?非常感谢您提前的帮助。
英文:
I faced the following problem, there's my controller's method:
@GetMapping("/items/show/{id}")
public ModelAndView showExtendedInfo(@PathVariable int id) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/extendedInfo.html");
modelAndView.addObject("itemId", id);
return modelAndView;
}
Here I pass itemId
to view. Also, I have such a form in the view:
<form th:action="@{/items/buy?itemId=${id}}" method="post">
<input id="cardId" type="text" name="cardId"/>
<br/>
<input id="bonusesAmount" type="text" name="bonusesAmount"/>
<br/>
<button type="submit">Buy</button>
</form>
So, I need to pass the parameter, which I have already passed from controller to form action, which after passes data again to the controller. But when I ran the code I got the following error:
java.lang.IllegalArgumentException: Invalid character found in the request target [/items/buy?itemId=${id}]. The valid characters are defined in RFC 7230 and RFC 3986
org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:486)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:261)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:834)
As I understand, it cannot find my variable. But what is the right way to pass the variable value to the form action? Thanks in advance for any help
答案1
得分: 1
你正在尝试使用GET映射来接收POST请求(我猜你试图在同一端点上执行此操作。这是不可能的。每个端点只能做一件事)。正确的方式(据我所知)是在SpringBoot中有一个带有'cardId'和'bonusesAmount'的模型,像这样:
public class CardRequestModel {
private Integer cardId;
private String bonusesAmount;
// 添加这里的getter和setter
}
然后一个请求映射如下:
@PostMapping("/items/buy/")
public ModelAndView showExtendedInfo(@RequestBody CardRequestModel cardRequestModel) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/extendedInfo.html");
modelAndView.addObject("itemId", cardRequestModel.cardId);
modelAndView.addObject("bonusesAmount", cardRequestModel.bonusesAmount);
return modelAndView;
}
此外,将表单选项设置为如下方式(POST请求没有像'?itemId=${id}'这样的查询参数):
<form th:action="@{/items/buy}" method="post">
英文:
Hi You're trying to have a POST request be received by a GET mapping(I am guessing you tried to use the same endpoint to do this. Not possible. Each endpoint does one thing). The correct way(AFAIK) is to have a model in SpringBoot with 'cardId' and 'bonusesAmount'. Like this
public class CardRequestModel{
private Integer cardId;
private String bonusesAmount;
// add getters and setters here
}
and then a request mapping as such
@PostMapping("/items/buy/")
public ModelAndView showExtendedInfo(@RequestBody CardRequestModel cardRequestModel) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/extendedInfo.html");
modelAndView.addObject("itemId", cardRequestModel.id);
modelAndView.addObject("bonusesAmount", cardRequestModel.bonusesAmount);
return modelAndView;
}
Additionally have the form option as such.(Post requests do not have query parameters like '?itemId=${id}')
<form th:action="@{/items/buy}" method="post">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论