실습 강의 내용은 현재 컨트롤러가 없고, 설정 파일인 WebMvcContextConfiguration 클래스 파일에서 @EnableWebMvc 어노테이션을 사용하여 default url "/" 을 "/WEB-INF/views/main.jsp"로 연결하는 부분입니다.
그런데 계속 아래와 같은 에러가 납니다... 그냥 실습 코드를 복사 붙여넣기 한 거라 오타가 있기 힘들기도 하고, 혹시 몰라 두 번 해봤는데 같은 에러입니다.
org.springframework.web.servlet.PageNotFound noHandlerFound
경고: No mapping found for HTTP request with URI [/mvcexam/] in DispatcherServlet with name 'mvc'
이유를 도저히 모르겠네요 ㅠㅠ 리뷰어님들 혹은 이 에러를 해결하신 분들의 도움 부탁드립니다.
먼저 web.xml입니다.--------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " xmlns="http://xmlns.jcp.org/xml/ns/javaee "xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd " version="3.1" metadata-complete="true"> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>kr.or.connect.mvcexam.config.WebMvcContextConfiguration</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
이번엔 web.xml에서 contextConfigLocation으로 참조하고 있는 WebMvcContextConfiguration입니다.-----------------------------------
package kr.or.connect.mvcexam.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc @ComponentScan(basePackages = { "kr.or.connect.mvcexam.controller" }) public class WebMvcContextConfiguration extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/META-INF/resources/webjars/").setCachePeriod(31556926); registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(31556926); registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(31556926); registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926); } // default servlet handler를 사용하게 합니다. @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void addViewControllers(final ViewControllerRegistry registry) { System.out.println("addViewControllers가 호출됩니다. "); registry.addViewController("/").setViewName("main"); } @Bean public InternalResourceViewResolver getInternalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } }
사실 이 에러와 연관은 없다고 생각되지만 혹시몰라서 폴더 트리와 pom.xml도 올립니다.
폴더 트리 입니다.
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>kr.or.connect</groupId> <artifactId>mvcexam</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mvcexam Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.3.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>mvcexam</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
++++++++++08.19
registry.addViewController("/").setViewName("/WEB-INF/views/main.jsp");
경로를 직접 참조 하니 되긴 합니다만.. 근본적으로 강의에서 InternalResourceViewResolver를 사용한 의도로 본다면 기본 '/' 경로가 internalResourceViewResolver를 거쳐서 /WEB-INF/views/main.jsp 경로로 가야하는 게 맞는 것 같은데요...왜 안 되는 지 의문이네요.
문제없이 되시는 분들은 댓글로 좀 알려주시면 감사하겠습니다. 원인을 찾아야하는데 근본적인 이유를 모르겠네요.
comment