ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 자바 프로젝트를 Spring으로 만들기(전체 처음부터 다시 보기)
    JAVA/웹 프로그래밍 - Spring 2020. 12. 11. 14:29

    아래의 내용에 이어서 이번에는 다시 처음부터 구현해보도록 하자.

    designatedroom87.tistory.com/337?category=909022

     

    자바 프로젝트를 Spring으로 만들기(Interface 다시 보기)

    아래의 내용에 이어서 진행해보자. designatedroom87.tistory.com/336?category=909022 프로젝트를 Maven 프로젝트로 변경하기 아래의 내용을 참고해보자. designatedroom87.tistory.com/334?category=909022 Mave..

    designatedroom87.tistory.com

     

    웹 개발할때 DI를 사용하는 방법을 알아보자.

    아래와 같이 일반 자바 프로젝트를 하나 만든다. Next를 선택한다.

     

    그리고 아래와 같이 module-info.java file은 체크 해제한다.

     

    그리고 이를 Maven Project로 변경하자.

    프로젝트를 선택하고 우클릭해서 Configure 탭에서 Convert to Maven Project를 선택한다.

    아래와 같이 설정을 한다.

    Packaging을 war로 둔 이유는 Dynamic Web Project로 만들 것이라서 그렇다.

    Finish를 선택하자.

     

    pom.xml파일에서 packaging태그에 다음과 같이 에러가 발생함을 볼 수 있다.

    이는 web.xml 파일이 없기 때문이다. 에러를 해결해 보자.

    main.webapp패키지에 WEB-INF폴더를 만들고 web.xml 파일을 하나 만든다.

    그리고 내용을 다른 프로젝트에서 복사해오자.

    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_4_0.xsd" id="WebApp_ID" version="4.0">
      <display-name>El</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

     

    웹이 되는지 확인하자.  서버부터 연결한다.

    아래의 그림과 같이 Servers탭을 선택해서 Tomcat v9.0을 선택하고 우클릭해서 Add and Remove를 선택한다.

     

    아래와 같이 현재 프로젝트만 오른쪽으로 놓고 나머지는 왼쪽으로 둔다.

    그리고 Finish를 선택한다.

    그리고 다시 Tomcat v9.0을 더블 클릭하자.

    열린 페이지의 맨 왼쪽 하단에서 다음과 같은 카테고리가 나오는데, Modules를 선택하자.

    아래와 같이 현재 프로젝트를 선택하고 Edit 버튼을 선택한다.

    Path를 다음과 같이 / 로 둔다. 그리고 OK 버튼을 누른다.

    그리고 변경된 사항을 저장한다.

    main.webapp패키지에 index.html 파일을 하나 만든다.

    그리고 실행해서 정상적인 동작을 확인하자. 에러가 발생할 것이다. 이는 아래에서 처리를 한다.

    index.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    index.html
    </body>
    </html>

     

    아래와 같이 index.jsp파일을 추가 main.webapp패키지에 추가하면 에러가 난다.

    index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    index.jsp
    </body>
    </html>

    그리고 index.jsp 파일에서 에러가 나는데 그 이유는 Tomcat 라이브러리가 없어서 그렇다.

    pom.xml에서 설정을 하자.

    웹 페이지에서 maven tomcat api을 검색한다. 

    그리고 기존에 설치한 Tomcat 서버의 버전에 맞게 찾는다.

    나의 Tomcat 버전은 9.0.39이다. 아래의 내용을 복사한다.

    그리고 복사해서 pom.xml로 와서 <dependencies>태그에 붙여넣자.

    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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.iot</groupId>
      <artifactId>springmvc</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
              <release>13</release>
            </configuration>
          </plugin>
        </plugins>
      </build>
      
      <dependencies>
      	<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-api -->
    	<dependency>
    	    <groupId>org.apache.tomcat</groupId>
    	    <artifactId>tomcat-api</artifactId>
    	    <version>9.0.39</version>
    	</dependency>
      </dependencies>
      
    </project>

     

    그리고, index.jsp파일을 실행해보자.

    그리고 프로젝트를 업데이트를 하자.

    현재의 프로젝트를 선택하고 우클릭을 해서 Maven 선택하고 Update Project를 선택한다.

     

    그리고 위의 접속 웹 페이지에서 maven spring framework web mvc 를 검색한다.

    아래를 선택한다.

    5.2.9 버전을 선택한다. 그 이유는 많은 사람들이 다운 받았다.

    아래를 복사하자.

    다시 pom.xml에 복사하자.

    아래는 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.iot</groupId>
      <artifactId>springmvc</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
              <release>13</release>
            </configuration>
          </plugin>
        </plugins>
      </build>
      
      <dependencies>
      	<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-api -->
    	<dependency>
    	    <groupId>org.apache.tomcat</groupId>
    	    <artifactId>tomcat-api</artifactId>
    	    <version>9.0.39</version>
    	</dependency>
    	
    	<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    	<dependency>
    	    <groupId>org.springframework</groupId>
    	    <artifactId>spring-webmvc</artifactId>
    	    <version>5.2.9.RELEASE</version>
    	</dependency>
      </dependencies>
      
    </project>

     

    메이븐 프로젝트 업데이트를 해주자.

    아래와 같이 tomcat과 spring 두 개가 있으면 된다.

     

    그리고, web.xml 파일에서 url 맵핑을 해주면 된다. (어노테이션 설정)

    아래는 web.xml의 전체 내용이다.

    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_4_0.xsd" id="WebApp_ID" version="4.0">
      <display-name>El</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
      	<servlet-name>dispatcher</servlet-name>
      	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
      	<servlet-name>dispatcher</servlet-name>
      	<url-pattern>/</url-pattern>
      </servlet-mapping>
      
    </web-app>

     

    서버를 실행하고 웹 페이지를 하나 열자.

    주소를 다음과 같이 적자. 

    http://localhost:9090/index.jsp 라고 적자. 그러면 무사히 실행이 된다.

     

    그리고 http://localhost:9090/ 라고 입력하면 500 에러가 뜨는데,

    아래와 같이 중간쯤에 다음과 같은 문구를 발견할 수 있다.

     

    내용 중간 쯤에 dispatcher-servlet.xml 관련 내용이 나오는데

    이에 대한 처리를 하고자 WEB-INF폴더에 dispatcher-servlet.xml 파일을 하나 만들자.

    dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>

     

    그리고 다시 웹 브라우저를 열고, http://localhost:9090/ 를 입력하자.

    그러면 다음과 같이 문구에 dispatcher-servlet.xml 파일의 내용이 없어짐을 볼 수 있다.

     

    그리고 spring ide를 설치해보자. 이클립스의 Help 탭에서 Eclipse Marketplace를 선택한다.

     

    spring을 검색한다. spring tool add를 install한다. (2번째)

     

    그리고 confirm을 클릭한다. 에러뜨면 설치안해도 된다.

     

    spring이 잘 설치가 되어있는지 확인을 해보자.

    아래와 같이 새 프로젝트를 생성해보자.

    New -> Other를 선택한다.

     

    그리고 아래와 같이 spring이라 적어보면, 아래와 같이 Spring관련 내용이 나오면 설치가 정상적임을 의미한다.

     

     

    만약, 설치가 정상적이지 않다면, 이클립스에서 Help를 선택하고 Install New Software를 선택한다.

     

    그리고 아래의 웹 페이지로 접속한다.

    https://dist.springsource.com/snapshot/STS/nightly-distributions.html

     

    Spring Tool Suite 3 - Nightly builds

     

    dist.springsource.com

     

    위의 웹 페이지에 접속해서 4.17버전의 주소를 복사한다.

     

    Add버튼을 클릭하고, 복사한 주소를 붙여넣는다.

     

    아래와 같이 Add버튼을 선택한다.

     

    아래와 같이 모두 선택하고 Next를 선택한다.

    그리고 그 이후부터는 Next를 선택하고 끝내면 된다.

     

    댓글

Designed by Tistory.