0

i have a controller that has a role to add the customer to my list and i have two methods showFormForAdd() to take a parameter and saveCustomer() for saving a customer and comeback to the list page and i have a parent request mapping named /c . the problem is that when i click on my submit button it duplicate the mapping url like this /c/customer/c/customer/savecustomer and i am unable to save my customer.i found out it works fine if i change action="c/customer/savecustomer" to action="savecustomer" but i have no idea why and what was wrong with that in the first place.i am using jsp and spring 5 mvc.

contoller

@Controller
@RequestMapping("/c")
public class CustomerController {
 @GetMapping("/customer/showFormForAdd")
    public String showFormForAdd(Model themodel){
        Customer thecustomer=new Customer();
        themodel.addAttribute("customer",thecustomer);
        return "customer-form";
    }
    @PostMapping("/customer/savecustomer")
    public String saveCustomer(@ModelAttribute("customer") Customer thecustomer){
        customerService.saveCustomer(thecustomer);
        return "redirect:/c/customer/list";
    }
}

jsp page

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/style.css"/>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/add-customer-style.css"/>
</head>
<body>
<div id="wrapper">
    <div id="header">
        <h2>CRM CUSOMER MANAGER</h2>
    </div>
</div>
<div id="container">
<%--    <form:form action="/customer/savecustomer" --%>
    <form:form action="c/customer/savecustomer" modelAttribute="customer" method="post" >
        <form:hidden path="id"/>
        <table>
            <tbody>
            <tr>
                <td><label>First Name</label></td>
                <td><form:input path="firstName"/></td>
            </tr>
            <tr>
                <td><label>last Name</label></td>
                <td><form:input path="lastName"/></td>
            </tr>
            <tr>
                <td><label>email </label></td>
                <td><form:input path="email"/></td>
            </tr>
            <tr>
                <td><label> </label></td>
                <td><input type="submit" value="save" class="save"></td>
            </tr>

            </tbody>
        </table>
    </form:form>
<div style="clear: both"></div>
    <p>
        <a href="${pageContext.request.contextPath}/customer/list">
            back to the form </a>
    </p>
</div>

</body>
</html>

log

31-Jul-2020 09:42:23.928 WARNING [http-nio-8080-exec-4] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/javacongif_war/] in DispatcherServlet with name 'dispatcher'
pooya
  • 115
  • 3
  • 13

1 Answers1

0

when you use url without / in the beginning it adds the url to the current url (it is called relative url)

change <form:form action="c/customer/savecustomer" to <form:form action="/c/customer/savecustomer" in order to use absolute url

J Asgarov
  • 2,526
  • 1
  • 8
  • 18
  • thanks for your answer but still i have a 404 error and this is a URL after submit the data http://localhost:8080/c/customer/savecustomer – pooya Jul 31 '20 at 16:16
  • maybe because you haven't implemented `return "redirect:/c/customer/list";` yet? Speaking of - it would help to see the stacktrace of your error – J Asgarov Jul 31 '20 at 16:28
  • your error log clearly says that there is no handler (no controller method) implemented for the last request before error. I suspect that you didn't yet create a @GetMapping("customer/list") – J Asgarov Jul 31 '20 at 16:51
  • i change URL in action to ``action="/javacongif_war/c/customer/savecustomer" `` and it work ! consider javacongif_wa its name of my project. can you explain to me what was a problem and why adding that fix it? – pooya Jul 31 '20 at 16:52
  • `javacongif_war` must be the context path that you deployed your project under, in spring boot you could have just used embedded deployment and wouldn't have that but I guess you are using normal Spring with your own Tomcat for deployment which uses a context path - you can configure context path like shown here: https://stackoverflow.com/questions/9522054/changing-tomcat-context-path-of-web-project-in-eclipse – J Asgarov Jul 31 '20 at 16:56