Maven을 사용하여 기본 스프링 MVC 웹 앱을 만들었습니다. 그러나 나는/login URL에 액세스 할 때 상수 404 오류가 발생하는 데 어려움을 겪고 있습니다. /login URL은 AuthController 서블릿과 createLoginForm() 메소드에 매핑되어야하지만, 불행히도 실패하고 404 오류가 발생합니다.스프링 MVC - Tomcat에서 상수 404 오류 발생
404 오류를 일으키는 문제와/로그인 URL을 서블릿에 매핑 할 수없는 이유를 파악하려고합니다.
URL을 매핑하지 못하게하는 web.xml 또는 spring-dispatcher-servlet.xml 파일의 구성이 잘못 되었습니까?
/URL에 액세스 할 때 index.jsp 파일이 매핑되고 올바르게 작동합니다. login.jsp 파일은 WEB-INF 디렉토리, 웹 응용 프로그램 디렉토리 및 인덱스에 저장됩니다 .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 디렉토리 바깥에있다. 그런데'InternalResourceViewResolver'는 쓸모가 없다. –
답장을 보내 주셔서 감사합니다. 그러나 login.jsp 파일을 WEB-INF 디렉토리로 옮기면 404 오류가 발생합니다. – Wjoz
무엇이든'default-servlet-handler'를 사용할 계획입니까? 그렇지 않은 경우 제거하십시오. –