我已经使用 Maven 创建了一个基本的 Spring MVC 网络应用程序。但是,当我访问/login URL 时,我一直在努力解决不断出现的 404 错误。 /login URL 应该映射到 AuthController servlet 和 createLoginForm() 方法,但不幸的是它失败了,我以 404 错误结束。
我试图找出导致 404 错误的问题以及为什么/login URL 无法映射到 servlet。
问题是否出在阻止映射 URL 的 web.xml 或 spring-dispatcher-servlet.xml 文件的错误配置?
当我访问/URL 时,index.jsp 文件被映射并且工作正常 login.jsp文件放在WEB-INF目录外,在webapp目录下,index.jsp文件也一样。
提前致谢。
AuthController.java
package com.github.wjoz.springmvcreview.auth;
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 AuthController {
@RequestMapping(value="/login", method=RequestMethod.GET)
public ModelAndView createLoginForm() {
ModelAndView model = new ModelAndView("login");
return model;
}
}
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>springmvcreview</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:default-servlet-handler/>
<context:component-scan base-package="main.java.com.github.wjoz.springmvcreview.auth" />
<mvc:annotation-driven />
<!-- Tells the location of the view in the project -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
登录.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<head>
<title>Welcome to our application. Sign in.</title>
</head>
<body>
<form action="/springmvcreview/login" method="post">
<div>
<label for="username">Username</label>
<input type="text" name="username" id="username">
<label for="password">Password</label>
<input type="password" name="password" id="password">
<button type="submit">Sign in</button>
</div>
</form>
</body>
</html>
最佳答案
这是对 Shazin's answer 的补充
首先,将您的 JSP 文件放入 WEB-INF
中。没有理由将它们放在它之外,因为您是在故意破坏 View 渲染功能。
其次,
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
表示 URL www.somedomain.com/
将加载索引页面,所有其他 URL 将以此开头。但是,您的表格
<form action="/springmvcreview/login" method="post">
正在访问 URL www.somedomain.com/springmvcreview/login
。这与 Controller URL 映射不匹配
@RequestMapping(value="/login", method=RequestMethod.GET)
public ModelAndView createLoginForm() {
不包含表单操作的映射。因此,要么从表单操作中删除 /springmvcreview
,要么将 @RequstMapping
值修改为 /springmvcreview/login
关于java - Spring MVC - 在 Tomcat 上不断出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27052402/