在Spring表单中重复的URL。

huangapple go评论162阅读模式
英文:

duplicate url in spring form

问题

我有一个控制器,它的角色是将客户添加到我的列表中,我有两个方法 showFormForAdd() 用于接收参数, saveCustomer() 用于保存客户并返回列表页面,我有一个父请求映射名为 /c 。问题是,当我点击提交按钮时,它会重复映射URL,如 /c/customer/c/customer/savecustomer,我无法保存我的客户。我发现,如果我将 action="c/customer/savecustomer" 更改为 action="savecustomer",它就能正常工作,但我不知道为什么以及一开始出了什么问题。我正在使用JSP和Spring 5 MVC。

控制器:

@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页面:

<%@ 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="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 list
        </a>
    </p>
</div>
</body>
</html>

日志:

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'

希望这些帮助你解决问题。如果你有更多问题,请随时提问。

英文:

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=&quot;c/customer/savecustomer&quot; to action=&quot;savecustomer&quot; 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(&quot;/c&quot;)
public class CustomerController {
 @GetMapping(&quot;/customer/showFormForAdd&quot;)
    public String showFormForAdd(Model themodel){
        Customer thecustomer=new Customer();
        themodel.addAttribute(&quot;customer&quot;,thecustomer);
        return &quot;customer-form&quot;;
    }
    @PostMapping(&quot;/customer/savecustomer&quot;)
    public String saveCustomer(@ModelAttribute(&quot;customer&quot;) Customer thecustomer){
        customerService.saveCustomer(thecustomer);
        return &quot;redirect:/c/customer/list&quot;;
    }
}

jsp page

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

            &lt;/tbody&gt;
        &lt;/table&gt;
    &lt;/form:form&gt;
&lt;div style=&quot;clear: both&quot;&gt;&lt;/div&gt;
    &lt;p&gt;
        &lt;a href=&quot;${pageContext.request.contextPath}/customer/list&quot;&gt;
            back to the form &lt;/a&gt;
    &lt;/p&gt;
&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;

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 &#39;dispatcher&#39;

答案1

得分: 0

当您在URL开头不使用斜杠时,它会将URL添加到当前URL中(这称为相对URL)。

&lt;form:form action=&quot;c/customer/savecustomer&quot;更改为&lt;form:form action=&quot;/c/customer/savecustomer&quot;以使用绝对URL。

英文:

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

change &lt;form:form action=&quot;c/customer/savecustomer&quot;
to &lt;form:form action=&quot;/c/customer/savecustomer&quot; in order to use absolute url

huangapple
  • 本文由 发表于 2020年7月31日 23:01:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63194354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定