我已經用Maven創建了一個基本的Spring MVC Web應用程序。然而,當我訪問/ login URL時,我一直在努力處理404錯誤。 應該將/ login URL映射到AuthController servlet和createLoginForm()方法,但不幸的是它失敗了,我最終遇到了404錯誤。Spring MVC - 在Tomcat上獲得不斷的404錯誤
我想弄清楚是什麼問題導致404錯誤,爲什麼/登錄URL不能映射到servlet。
該問題位於web.xml或spring-dispatcher-servlet.xml文件的錯誤配置中,導致該URL無法映射?
當我訪問/ URL時,index.jsp文件被映射,並且工作正常 login.jsp文件放在WEB-INF目錄之外,在webapp之一以及索引.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>
彈簧調度-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>
的login.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>
_登錄。jsp文件放在WEB-INF directory_之外,那麼你的'InternalResourceViewResolver'就沒用了。 – 2014-11-21 00:53:46
感謝您的回覆。但是,當我將login.jsp文件移到WEB-INF目錄中時,我仍然收到404錯誤。 – Wjoz 2014-11-21 00:56:08
你打算使用'default-servlet-handler'做什麼?如果沒有,請將其刪除。 – 2014-11-21 00:57:13