-1

My spring mvc controller has a method:

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public ModelAndView search(@RequestParam("cn") String cn) {
    //do stuff
    return mav;
}

And I have a JSP page, where I'd like to send the value of the string cn:

<html>
 <head><title>Search Entry</title></head>
 <body>
  <h3>Search entry</h3>
  <a href="search"><input type="text" name="cn" value="search">
   <button type="submit">Search</button>
  </a>
  </body>
</html>

I'm sure the jsp has something wrong, now the status message says:

"Request method 'GET' not supported".

If I delete method = RequestMethod.POST it says:

"Required String parameter 'cn' is not present; description: The request sent by the client was syntactically incorrect"

MdC
  • 107
  • 1
  • 4
  • 16

3 Answers3

3

I can see that your are using Spring annotations so what you need to do is to modify your JSP to have a form instead of a link like this:

<html>
<head><title>Search Entry</title></head>
<body>
<h3>Search entry</h3>
<form action="search" method="post">
    <input type="text" name="cn" value="search">
    <button type="submit">Search</button>
</form>
</body>
</html>

If you want a GET method to work you need to modify your controller's method to accept GET and change your JSP like this:

<html>
<head><title>Search Entry</title></head>
<body>
<h3>Search entry</h3>
  <a href="search?cn=search">
     Search
  </a>
</body>
</html>

If you want your parameter to be not mandatory you can modify your @RequestParam annotation like this:

@RequestParam(value = "cn", required = false) String cn
Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
0

you need to Define action="search"inside form tag that should be work

-2

Change your HTML :

<html>
<head><title>Search Entry</title></head>
<body>
<h3>Search entry</h3>
<a href="search"><input type="text" name="cn" value="search" action="search">
 <button type="submit">Search</button>
</a>
</body>
 </html>

or else follow this link:

Community
  • 1
  • 1
Anand Dwivedi
  • 1,452
  • 13
  • 23