我在 Spring 开发了演示登录应用程序。这是我的 LoginController.java:
package com.pran.pal.controller;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController extends HttpServlet{
@RequestMapping(value="/login" ,method=RequestMethod.GET)
public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
String userName=request.getParameter("userName");
String password=request.getParameter("password");
String message;
if(userName != null && !userName.equals("") && userName.equals("admin")
&& password != null && !password.equals("") && password.equals("123")){
message = "Welcome " +userName + ".";
return new ModelAndView("index", "message", message);
}
else{
message = "Wrong username or password.";
return new ModelAndView("index", "message", message);
}
}
我用过 eclipse Neon,Tomcat v9。
这是我的 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pran.pal</groupId>
<artifactId>LoginSystemPAL</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>LoginSystemPAL Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>LoginSystemPAL</finalName>
</build>
</project>
这是我的 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>LoginController</servlet-name>
<servlet-class>com.pran.pal.controller.LoginController</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
It has shown that input username and password that:
HTTP Status 405 - HTTP method POST is not supported by this URL
type Status report
message HTTP method POST is not supported by this URL
description The specified HTTP method is not allowed for the requested resource.
我该如何解决这个问题?
最佳答案
你的 <servlet-class>
应该是 com.pran.pal.controller.LoginController
,但您还应该从 here 依赖于 Spring Web .
此外,您没有正确使用 Spring @Controller
注解。请按照类似 here 的教程一步一步进行操作.
关于java - 为什么显示 HTTP 状态 405 - 此 URL 不支持 HTTP 方法 POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41137678/