2014-09-02 160 views
0

這是一個非常簡單的代碼,我正在着手開始使用Spring MVC,我嘗試了每個選項來解決這個問題,但我無法做到。該代碼運行正常顯示index.jsp(它告訴我,DispatcherServlet和ViewResolver工作正常),但是當我點擊「LoginForm」顯示登錄表單(loginform.jsp),以便用戶可以提交用戶名和密碼我收到以下例外。SpringMVC請求方法'GET'不支持?

 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

在春天,你不必*。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> 
相關問題