0

I got this index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="ISO-8859-1">
    <title>Store</title>
</head>
<body>
    <form action="#" th:action="@{/redirectToAddProd}" method="post">
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

And I got this StoreController.java

@Controller
@RequestMapping("/")
public class StoreController {
    @GetMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/addProduct")
    public String addProduct() {
        return "addProduct";
    }

    @PostMapping("/redirectToAddProd")
    public String redirectToAddProd() {
        return "redirect:/addProduct";
    }
}

My problem is, when I press the submit button nothing happens (really nothing) not a get request not even a 404. Do you have a solution? Thank you

EDIT: My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>MyStore</display-name>

    <servlet>
        <servlet-name>store</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/store-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>store</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

EDIT: Now I'm getting a new error. Now the button does something but is giving me the error:

sept. 20, 2017 10:08:30 AM org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
AVERTISSEMENT: Request method 'POST' not supported
frankdart
  • 59
  • 10

1 Answers1

1

Your form should have a method of POST, not GET.

<form action="#" th:action="@{/redirectToAddProd}" method="post">

And your controller needs to be annotated to process the post request, however you see fit:

@PostMapping("/redirectToAddProd")
public String redirectToAddProd() {
    return "redirect:/addProduct";
}

Finally, you can remove @RequestMapping("/") from the top of your controller since it is redundant and use @GetMapping instead of the longer form you have in your code.

You can enable debug logging on classes like these to help you diagnose your routing issues:

<logger name="org.springframework.web.servlet.handler.AbstractUrlHandlerMapping" level="DEBUG" /> <logger name="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" level="DEBUG" />

vphilipnyc
  • 6,607
  • 6
  • 44
  • 70
  • Hello @bphilipnyc sorry but I don't know where to use the annotation GetMapping. I made all the other changes you asked me to but now I'm having this error message: Request method 'POST' not supported Thanks for your answer by the way – frankdart Sep 20 '17 at 08:54
  • sept. 20, 2017 9:48:06 AM org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported AVERTISSEMENT: Request method 'POST' not supported – frankdart Sep 20 '17 at 08:59
  • Oh, I understood the GetMapping you were talking about. Thanks for your tip. – frankdart Sep 20 '17 at 09:15
  • check that whatever method is getting called in your form is supported with an annotated `@PostMapping` method. Add a logging statement as the first line in the method to see whether it's getting called. – vphilipnyc Sep 20 '17 at 18:32
  • Also, best not to change the original question when a person provides an answer - your first issue was using POST vs. GET – vphilipnyc Sep 20 '17 at 18:37
  • the method is not getting called. ok, my bad changing the question, won't do it again. thankyou – frankdart Sep 21 '17 at 09:17
  • By the way, where should I place the debug logging code that you provided? web.xml? or should I have another configuration file? the only configuration files that I have are web.xml and store-servlet.xml – frankdart Sep 21 '17 at 09:32