【问题标题】:Spring MVC Error 404 Bad Request KotlinSpring MVC 错误 404 错误请求 Kotlin
【发布时间】:2017-11-11 08:11:10
【问题描述】:

我正在使用 Kotlin 开发 Spring MVC 应用程序。
我有一个简单的表单,当我提交时,我收到 Error 404 bad Request。我正在使用 Jetty 服务器和 Intellij 社区版。我尝试过调试,但由于我从未调试过 Web 应用程序,所以没有那么有用。

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>frontDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>frontDispatcher</servlet-name>
    <url-pattern>/springkotlinmvc/*</url-pattern>
  </servlet-mapping>
</web-app>

frontDispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context = "http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <context:annotation-config/>
    <mvc:annotation-driven/>
    <context:component-scan base-package="org.manya.kotlin"/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

DataClasses.kt

package org.manya.kotlin

data class Address (val city : String, val state : String)

data class Student ( val name : String , val age : Int, val address : Address)

StudentController.kt

package org.manya.kotlin

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*
import org.springframework.web.servlet.ModelAndView


@Controller
@RequestMapping("/student")
class StudentController
{
    //@GetMapping("/student/form")
    @GetMapping("form")
    fun studentForm() : ModelAndView{
        println("called from studentForm()")
        return ModelAndView("form")
    }

    //@PostMapping("springkotlinmvc/student/submitted")
    //@RequestMapping(value = "/student/submitted" , method = arrayOf(RequestMethod.POST))
    //@RequestMapping("/submitted")
    @PostMapping("/submitted")
    fun submitted(@ModelAttribute("student") stud : Student) : ModelAndView {
        println("called from submitted()")
        return ModelAndView("submitted")
    }
}

这里的 studentForm() 方法被完美地映射到了 view(form.jsp),但是提交的方法没有被映射。

form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="./submitted" method="post">
        NAME : <input id="name"/>
        AGE : <input id="age"/>
        CITY : <input id="address.city"/>
        STATE : <input id="address.state"/>
        <input type="submit"/>
    </form>
</body>
</html>

【问题讨论】:

  • 打开浏览器的开发工具并跟踪用于发送请求的 URL。您的表单的action./submitted,它可能映射到/student/form/submitted,而Spring 一侧的映射是/student/submitted。为了在未来缓解类似问题,请尝试使用c:urlspring:form 标签:它们是上下文感知的。顺便说一句,404 是“未找到”,而不是“错误请求”,这很奇怪,您会收到该错误。

标签: spring spring-mvc intellij-idea kotlin jetty


【解决方案1】:

检查你的链接404表示未找到,可能是这里没有指向root./submitted,改成${pageContext.request.contextPath}/foo或者WEB-INF位置的配置。

 4×× Client Error
    400 Bad Request
    401 Unauthorized
    402 Payment Required
    403 Forbidden
    404 Not Found
    405 Method Not Allowed
    406 Not Acceptable
    407 Proxy Authentication Required
    408 Request Timeout
    409 Conflict
    410 Gone
    411 Length Required
    412 Precondition Failed
    413 Payload Too Large
    414 Request-URI Too Long
    415 Unsupported Media Type
    416 Requested Range Not Satisfiable
    417 Expectation Failed
    418 I'm a teapot
    421 Misdirected Request
    422 Unprocessable Entity
    423 Locked
    424 Failed Dependency
    426 Upgrade Required
    428 Precondition Required
    429 Too Many Requests
    431 Request Header Fields Too Large
    444 Connection Closed Without Response
    451 Unavailable For Legal Reasons
    499 Client Closed Request

【讨论】:

  • 我试过了,但它给了我这样的网址 localhost:8080/$%7BpageContext.request.contextPath%7D/submitted 。我目前正在执行的操作也是指向正确的 url。
  • 另外我想补充一点,它显示的是 404 Bad Request 而不是 **404 not found”。也许它们之间存在差异。
  • Bad req 发生在春季的 3 种情况下:1-服务器需要一些道具,但后面的对象中不存在 2-在类型不匹配的情况下,例如您正在发布但服务器被定义为 GET 3 - 数据类型,例如发送 json 但服务器消耗或生成 html
【解决方案2】:

我在我的代码中发现了错误,这是一个小错误,但由于我缺乏网络领域的知识,我没有意识到这一点。

在 form.jsp 中,我在表单中为所有输入元素赋予了 id 属性。我将它们更改为 名称,它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    相关资源
    最近更新 更多