2017-11-11 173 views
2

我使用科特林開發Spring MVC的應用。
我有一個簡單的表格,當我提交時,我得到錯誤404不好請求。我正在使用Jetty服務器和Intellij社區版。我試過調試,但因爲我從未調試過一個Web應用程序,所以它沒有幫助。Spring MVC的錯誤404錯誤的請求科特林

的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()是完全得到映射到視圖(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> 
+1

打開瀏覽器的開發工具並跟蹤用於發送請求的URL。你的表單的'action'是'。/ submit',可能映射到'/ student/form/submitted',而Spring的映射是'/ student/submitted'。爲了緩解將來嘗試使用'c:url'或'spring:form'標籤的類似問題:它們是上下文感知的。順便說一句,404是「找不到」,而不是「壞請求」,這很奇怪,你得到的錯誤。 – madhead

回答

0

檢查你的鏈接404指沒有發現,可以通過未指向這裏生根./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 
+0

我試過這個,但它給了我這樣的網址** http:// localhost:8080/$%7BpageContext.request.contextPath%7D/**。此外,我目前正在採取的行動是指向正確的網址。 – Manya

+0

另外我想補充一點,它顯示** 404錯誤請求**而不是** 404沒有找到「。也許它們之間有差別。 – Manya

+0

在3例春天發生錯誤請求:1-服務器需要一些道具但是在後端對象2中不存在 - 如果類型不匹配,例如發佈但服務器定義爲GET 3類型的數據,例如發送json但服務器消耗或生成html –

0

我發現在我的代碼的錯誤,這是一個小錯誤,但我,因爲我沒有在網絡領域的知識沒有意識到這一點。

在form.jsp,形式爲我給ID屬性對所有的輸入元件。我將這些更改爲名稱,它工作正常。