我使用科特林开发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>
打开浏览器的开发工具并跟踪用于发送请求的URL。你的表单的'action'是'。/ submit',可能映射到'/ student/form/submitted',而Spring的映射是'/ student/submitted'。为了缓解将来尝试使用'c:url'或'spring:form'标签的类似问题:它们是上下文感知的。顺便说一句,404是“找不到”,而不是“坏请求”,这很奇怪,你得到的错误。 – madhead