퐝재
10
2021-04-07 17:43:14
1
113

/WEB-INF/views/ 를 인식하지 못하는것 같아요..


안녕하세요. 현재 스프링으로 웹사이트를 하나 만들어볼까 하는데요.

지금 문제는 views/home.jsp 폴더에 들어가려고 하면 계속 아래 메시지가 나오는데요.


메시지 요청된 리소스 [/hello]은(는) 가용하지 않습니다.

설명 Origin 서버가 대상 리소스를 위한 현재의 representation을 찾지 못했거나, 그것이 존재하는지를 밝히려 하지 않습니다.


아무리봐도 왜 views폴더안의 jsp로 접근이 안되는지 모르겠습니다. intellij 로 개발중이고요. 


[1] Project Structure - Artifacts 에서 Elements는 전부 WEB-INF 안으로 옮겨놓은 상태입니다.

[2] Run/Debug Configurations 에서 tomcat local 설정의 application context 는 "/" 로 해놓았고, Deployment 에서 war exploded도 되어있습니다.

[3] <welcome-file>/WEB-INF/home.jsp</welcome-file> 도 설정해봤는데 뜨지 않습니다.

[4] 실행했을때 index.jsp 는 잘 나옵니다. 물론 index.jsp를 지웠을때는 여전히 같은 애러가 나옵니다.

[5] applicationContext.xml 파일에는 아무것도 쓰여있지 않습니다.

[6] 현재 스프링버전 4.3.20, 톰캣 9.0.44 입니다.


왜이럴까요 ㅠㅠ


폴더구조.



HelloController.java

package com.springword;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

    @RequestMapping(value = "/hello")
    public ModelAndView hello() {

        ModelAndView andView = new ModelAndView();
        andView.addObject("message", "hello World! ");
        andView.setViewName("/hello/hello");
        return andView;

    }

}


1. pom.xml

# pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.springWord</groupId>
    <artifactId>springWord</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.framework.version>4.3.20.RELEASE</spring.framework.version>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.20.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.20.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.20.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.20.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>


2. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <absolute-ordering />

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    
    <!-- encoding UTF-8 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>


3. dispacher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">


    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
    <aop:aspectj-autoproxy/>

    <context:component-scan base-package="com.springword"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

<!--    <bean id="viewResolver"-->
<!--          class="org.springframework.web.servlet.view.UrlBasedViewResolver">-->
<!--        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->
<!--        <property name="prefix" value="/WEB-INF/views/"/>-->
<!--        <property name="suffix" value=".jsp"/>-->
<!--    </bean>-->

</beans>


4. index, hello jsp

# index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>

<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>index.jsp</h1>

<a href="${path}hello">home.jsp</a>

</body>
</html>


# hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h2>${message}</h2>
</body>
</html>



0
  • 답변 1

  • 제타건담
    7k
    2021-04-08 17:22:32

    해결하셨는지는 모르겠는데..마지막으로 생각해볼 부분은..

    tomcat 사용중이신가요..?

    그러면 거기서 context 이름이 /로 되어 있는지..확인해보시면 좋을듯 합니다..

    별도로 설정을 고치지 않으면 그게 /프로젝트명 으로 되기 때문에..

    브라우저에서 호출할려겨면 /프로젝트명/hello 라고 해줘야 하거든요..


  • 로그인을 하시면 답변을 등록할 수 있습니다.