복붙노트

[SPRING] Spring 루트 컨텍스트 경로를 설정하는 방법

SPRING

Spring 루트 컨텍스트 경로를 설정하는 방법

나는 봄의 초보자이며 Spring 웹 애플리케이션 (Spring-boot가 아닌 - 이것이 얼마나 다른 점인가?)을 함께 모으고있다. 배포는 Tomcat 7 서버에 있습니다.

앱이 실행 중입니다. 내 문제는 표준 URL을 통해서만 액세스 할 수 있다는 것입니다.

http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/index.html

다음은 작동하지 않습니다. http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/

내 web.xml은 index.html을 시작 페이지로 나열하지만.

문제의 또 다른 증상은 응용 프로그램 내의 모든 종류의 링크를 앞에 붙인 '/'대신 상대적으로 지정해야한다는 것입니다.

그러나 이러한 URL조차도 내가 정말로 원하는 것은 아닙니다.

나는 오히려 구체적으로 설명하고자한다.

http://mycompany.com:8081/cwing

index.html을 불러 오도록하십시오.

Spring Boot 애플리케이션에 컨텍스트 경로 추가의 리드에 이어

다음 내용으로 src / main / resources에 application.xml 파일을 만들었습니다.

server.contextPath=/cwing
server.port=8081

이것은 작동하지 않습니다. http://mycompany.com:8081/cwing은 http://mycompany.com:8081/cwing/index.html과 마찬가지로 서버에서 404 오류와 함께 빈 페이지를 표시합니다.

나는 뭔가를 놓쳤을 것입니다. 이 기능을 작동 시키려면이게 무엇이 필요합니까?

업데이트 : 묻는대로 여기에 web.xml (관련없는 세부 정보가 삭제됨)이 있습니다.

    <?xml version="1.0"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
              version="3.0">
      <display-name>cwing</display-name>

        <!-- Creates the Spring Container shared by all Servlets and Filters -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- The definition of the Root Spring Container shared by all Servlets 
            and Filters -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            /WEB-INF/spring/context.xml
            /WEB-INF/spring/datasource.xml
            /WEB-INF/spring/security.xml
            </param-value>
        </context-param>

        <servlet>
            <servlet-name>d</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    /WEB-INF/spring/context.xml
                    /WEB-INF/spring/datasource.xml
                    /WEB-INF/spring/security.xml
                </param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
            <async-supported>true</async-supported>
        </servlet>

        <servlet-mapping>
            <servlet-name>d</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>

        <!-- Disables Servlet Container welcome file handling. Needed for compatibility 
            with Servlet 3.0 and Tomcat 7.0 -->
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>

        <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        </filter>

        <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping> 
...
</web-app>

해결법

  1. ==============================

    1.이것은 Spring MVC와는 관련이 없지만 특정 서버에는 기본적으로 Tomcat이 WAR 이름을 컨텍스트 루트로 가져옵니다. 이것을 바꾸고 싶다면 항상 WAR 파일의 이름을 바꿀 수 있습니다. 또는 pom.xml을 변경하여 최종 이름으로 WAR 파일을 만들 것을 권장합니다.

    이것은 Spring MVC와는 관련이 없지만 특정 서버에는 기본적으로 Tomcat이 WAR 이름을 컨텍스트 루트로 가져옵니다. 이것을 바꾸고 싶다면 항상 WAR 파일의 이름을 바꿀 수 있습니다. 또는 pom.xml을 변경하여 최종 이름으로 WAR 파일을 만들 것을 권장합니다.

    <build>
      <finalName>finalName</finalName>
     . . .
    </build>
    
  2. from https://stackoverflow.com/questions/31005066/how-to-set-spring-root-context-path by cc-by-sa and MIT license