【问题标题】:Springboot tomcat 405 but it works fine within eclipse tomcatSpringboot tomcat 405,但在eclipse tomcat中工作正常
【发布时间】:2021-11-13 22:05:01
【问题描述】:

单击更新或创建时出现 405 错误,如下面的屏幕截图所示。本地主机应用程序加载正常,但更新或创建按钮不起作用。通过 eclipse tomcat (spring-boot:run) 运行应用程序时它工作正常,但不起作用,我们将 war 文件部署到本地 tomcat。页面加载,但功能不起作用。

这是我正在尝试运行的应用程序https://github.com/mokarakaya/spring-boot-multi-module-maven

知道为什么会出现此错误吗?

按照 Piotr P. Karwasz 的建议修改代码。但是单击按钮时它仍然不起作用。它转到 localhost 并跳过 contextPath

function deleteRow(index,id) {
    const contextPath = "/temp";
    storageTypeUrl= $('select[name=storageType]').val() == "session"  ? "sessionOperations" : "operations";
    $.ajax({
        async: false,
        type: "DELETE",
        url: contextPath+"/"+storageTypeUrl+"/?index="+index,
        success: function(data){
            console.log("delete Row successful");
        },
        error : function(request,error) {
            alert(error);
        }});
}

【问题讨论】:

  • 您能否在收到此错误时添加应用程序日志。
  • 没有日志设置。控制台没有显示任何错误。顺便说一句,Piotr P. Karwasz 答案有效,但是当单击更新或删除时它不起作用。为答案添加评论

标签: java eclipse spring-boot tomcat


【解决方案1】:

您的应用程序没有考虑使用非空context path 进行部署的可能性。您的 XHR 调用的 URI 路径应该是 /<context-path>/sessionOperations 而不是 /sessionOperations,您可以在其中检索上下文路径:

  • 在 index.jsp 中使用 EL 表达式:

    const contextPath = "${pageContext.request.contextPath}";
    
  • 或使用 JavaScript 计算:

    // The URI path or this page is '<context-path>/' or '<context-path>/index.jsp'
    const uri = location.pathname;
    const idx = uri.lastIndexOf('/');
    const contextPath = idx != -1 ? uri.substring(0, idx) : uri;
    

备注:为防止此类错误,您可以添加:

server.servlet.context-path=/something-random

到您用于测试的application.properties。这样,您的应用程序就可以使用嵌入式和外部 Tomcat 以相同的方式工作。

编辑:您的应用程序中还有一个点缺少上下文路径。替换:

<form:form method="GET" modelAttribute="accountListWrapper" action="/">

与

<form:form method="GET" modelAttribute="accountListWrapper" action="${pageContext.request.contextPath}/">

【讨论】:

  • const contextPath = ${pageContext.request.contextPath}; 是否应该添加到所有 3 个函数中?函数创建、更新和删除?
  • 可以定义一次,使用contextPath + '/' + storageTypeUrl 三次。 BTW:应该是"${pageContext.request.contextPath}",我忘记引用了。
  • 似乎它不起作用.. 当您单击 udpate 或删除或创建时,它会转到 localhost:8080/?storageType。我测试了application.properties 和const contextPath。并且 URL 修改仍然是在跳过 contextPath localhost:8080/?storageType=session&accountLi
  • 您可以将修改后的 JavaScript 添加到您的问题中吗?
  • 已添加到问题中。我测试了pageContext.request.contextPath 和静态条目
猜你喜欢
  • 2021-01-10
  • 2012-11-01
  • 2012-03-30
  • 2013-12-12
  • 2012-05-31
  • 1970-01-01
  • 2015-07-10
  • 2013-07-09
  • 1970-01-01
相关资源
最近更新 更多