Sergei Mikhailovskii
Sergei Mikhailovskii

Reputation: 2465

How to pass string from controller to form's post method as parameter?

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

Upvotes: 0

Views: 373

Answers (1)

Vikrant
Vikrant

Reputation: 340

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

Upvotes: 1

Related Questions