【问题标题】:duplicate url in spring form春季形式的重复网址
【发布时间】:2020-07-31 15:01:49
【问题描述】:

我有一个控制器,负责将客户添加到我的列表中,我有两种方法 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="/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>

日志

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'

【问题讨论】:

    标签: java spring jsp


    【解决方案1】:

    当您在开头使用不带 / 的 url 时,它会将 url 添加到当前 url(称为相对 url)

    更改&lt;form:form action="c/customer/savecustomer"&lt;form:form action="/c/customer/savecustomer" 以使用绝对网址

    【讨论】:

    • 感谢您的回答,但我仍然有 404 错误,这是提交数据后的 URL localhost:8080/c/customer/savecustomer
    • 可能是因为你还没有实现return "redirect:/c/customer/list";?说到 - 这将有助于查看错误的堆栈跟踪
    • 您的错误日志清楚地表明没有为错误前的最后一个请求实现处理程序(无控制器方法)。我怀疑你还没有创建 @GetMapping("customer/list")
    • 我将操作中的 URL 更改为 action="/javacongif_war/c/customer/savecustomer" 并且它可以工作!考虑 javacongif_wa 它是我项目的名称。你能向我解释一下是什么问题以及为什么要添加它来修复它吗?
    • javacongif_war 必须是您在其中部署项目的上下文路径,在 Spring Boot 中,您可能刚刚使用嵌入式部署,但我猜您使用的是普通 Spring 和您自己的Tomcat 用于使用上下文路径的部署 - 您可以配置上下文路径,如下所示:stackoverflow.com/questions/9522054/…
    猜你喜欢
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    相关资源
    最近更新 更多