这是一个非常简单的代码,我正在努力开始使用Spring MVC,我已经尝试了解决这个问题的所有选项,但我无法做到。 Code运行正常以显示index.jsp(它告诉我DispatcherServlet和ViewResolver都正常工作),但是当我单击“LoginForm”以显示Login Form(loginform.jsp)以便用户可以提交用户名时和密码我得到以下异常。
Here is what I am getting when I run my code
"Sep 02, 2014 2:56:49 PM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpRequestMethodNotSupported
WARNING: Request method 'GET' not supported"
我非常感谢任何帮助。
以下是我的代码的一些部分
这是我的控制器
@Controller
public class LoginController {
@RequestMapping(value = "/loginForm", method = RequestMethod.GET)
public String showForm(Map model) {
LoginForm loginForm = new LoginForm();
model.put("loginForm", loginForm);
return "/login/loginform";
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMvc</display-name>
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>firstMvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/firstMvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>firstMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 3, MVC Examples</title>
</head>
<body>
<h1>Spring 3, MVC Examples</h1>
<ul>
<li style="width: 130px;"><a href="<spring:url value="/loginform.html" htmlEscape="true" />"> Login Form</a></li>
</ul>
</body>
</html>
loginForm.jsp中
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring3Example</title>
</head>
<body>
<h3>Login Form</h3>
<FONT color="blue">
<h6>User Name="UserName" and password="password"</h6>
</FONT>
<form:form action="loginform.html" commandName="loginForm">
<table>
<tr>
<td>User Name:<FONT color="red"><form:errors
path="userName" /></FONT></td>
</tr>
<tr>
<td><form:input path="userName" /></td>
</tr>
<tr>
<td>Password:<FONT color="red"><form:errors
path="password" /></FONT></td>
</tr>
<tr>
<td><form:password path="password" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
答案 0 :(得分:0)
春天你不必* .html。只需使用控制器值。在views.xml中,您必须将值设置为jsp文件。
<form:form action="/loginform" commandName="loginForm">
<table>
<tr>
<td>User Name:<FONT color="red"><form:errors
path="userName" /></FONT></td>
</tr>
<tr>
<td><form:input path="userName" /></td>
</tr>
<tr>
<td>Password:<FONT color="red"><form:errors
path="password" /></FONT></td>
</tr>
<tr>
<td><form:password path="password" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>