|
| 1 | +--- |
| 2 | +title: Intellij에서 Spring MVC 시작하기 |
| 3 | +tags: |
| 4 | + - Spring |
| 5 | + - Intellij |
| 6 | +date: 2017-02-05 |
| 7 | +aliases: |
| 8 | + - /articles/2017-02/SpringMVC |
| 9 | +--- |
| 10 | + |
| 11 | +![[assets/logo/spring.png]] |
| 12 | + |
| 13 | +> Intellij에서 Spring MVC를 이용하여 Hello World를 띄우기 까지 매우 많은 삽질을 통해 해결했다. 많은 이들이 포스팅을 통해서 삽질을 그나마 줄일 수 있다면 좋겠다. |
| 14 | +
|
| 15 | +## Intellij Maven Project 만들기 |
| 16 | + |
| 17 | +- New Project - Maven - Create from archetype(해제하기) |
| 18 | + |
| 19 | +![[assets/posts/20170204/1.PNG]] |
| 20 | + |
| 21 | +## Project SpringMVC Framework 추가하기 |
| 22 | + |
| 23 | +- Add Framework Support |
| 24 | + |
| 25 | +![[assets/posts/20170204/2.PNG]] |
| 26 | + |
| 27 | +- Spring - Spring MVC 체크하기 |
| 28 | + |
| 29 | +![[assets/posts/20170204/3.PNG]] |
| 30 | + |
| 31 | +## Spring MVC Library 다운로드 완료 후 Project 모습 |
| 32 | +![[assets/posts/20170204/4.PNG]] |
| 33 | + |
| 34 | +## web.xml 변경하기 |
| 35 | +- servlet-mapping의 url-pattern변경하기 : ``` *.form -> / ``` |
| 36 | +- 변경 전 web.xml |
| 37 | + |
| 38 | +![[assets/posts/20170204/5.PNG]] |
| 39 | + |
| 40 | +- 변경 후 web.xml |
| 41 | + |
| 42 | +```xml |
| 43 | +<?xml version="1.0" encoding="UTF-8"?> |
| 44 | +<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" |
| 45 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 46 | + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" |
| 47 | + version="3.1"> |
| 48 | + <context-param> |
| 49 | + <param-name>contextConfigLocation</param-name> |
| 50 | + <param-value>/WEB-INF/applicationContext.xml</param-value> |
| 51 | + </context-param> |
| 52 | + <listener> |
| 53 | + <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> |
| 54 | + </listener> |
| 55 | + <servlet> |
| 56 | + <servlet-name>dispatcher</servlet-name> |
| 57 | + <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
| 58 | + <load-on-startup>1</load-on-startup> |
| 59 | + </servlet> |
| 60 | + <servlet-mapping> |
| 61 | + <servlet-name>dispatcher</servlet-name> |
| 62 | + <url-pattern>/</url-pattern> <!-- *.form -> / 바꾸기--> |
| 63 | + </servlet-mapping> |
| 64 | +</web-app> |
| 65 | +``` |
| 66 | + |
| 67 | +## 라이브러리 추가해주기 |
| 68 | + |
| 69 | +- Project Structure - Artifacts에 들어간다. |
| 70 | + |
| 71 | +![[assets/posts/20170204/18.PNG]] |
| 72 | + |
| 73 | +### Available Elements 아래에 있는 Library를 더블 클릭한다. |
| 74 | +- Spring-4.3.6-RELEASE to artifact |
| 75 | +- Spring MVC-4.3.6-RELEASE to artifact |
| 76 | + |
| 77 | +### 옮겨진 모습 |
| 78 | + |
| 79 | +![[assets/posts/20170204/19.PNG]] |
| 80 | + |
| 81 | + |
| 82 | +## dispatcher-servlet.xml에 추가하기 |
| 83 | + |
| 84 | +- Annotation 활성화 & component-scan 파일 지정하기 |
| 85 | + |
| 86 | +```xml |
| 87 | +<?xml version="1.0" encoding="UTF-8"?> |
| 88 | +<beans xmlns="http://www.springframework.org/schema/beans" |
| 89 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 90 | + xmlns:context="http://www.springframework.org/schema/context" |
| 91 | + xmlns:mvc="http://www.springframework.org/schema/mvc" |
| 92 | + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
| 93 | + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd |
| 94 | + http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> |
| 95 | + |
| 96 | + <mvc:annotation-driven></mvc:annotation-driven> <!-- Annotation 활성화 --> |
| 97 | + <context:component-scan base-package="Controller"></context:component-scan> <!-- Component 패키지 지정 --> |
| 98 | + |
| 99 | + <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
| 100 | + <property name="prefix" value="/WEB-INF/views/"></property> |
| 101 | + <property name="suffix" value=".jsp"></property> |
| 102 | + </bean> |
| 103 | + |
| 104 | +</beans> |
| 105 | +``` |
| 106 | +- Annotation 활성화를 안해주면 bean을 하나하나 다 등록 해야 하는 어려움이 있다. |
| 107 | +- component-scan 패키지를 지정해주지 않으면 spring-framework가 scan을 못하여 작동하지 않게 된다. |
| 108 | + |
| 109 | +## views 디렉토리 파일 만들기 & index.jsp 이동시키기 |
| 110 | + |
| 111 | +- New - Directory |
| 112 | + |
| 113 | +![[assets/posts/20170204/6.PNG]] |
| 114 | + |
| 115 | +- index.jsp |
| 116 | + |
| 117 | +```html |
| 118 | +<%-- |
| 119 | + Created by IntelliJ IDEA. |
| 120 | + User: NESOY |
| 121 | + Date: 2017-02-04 |
| 122 | + Time: 오후 11:41 |
| 123 | + To change this template use File | Settings | File Templates. |
| 124 | +--%> |
| 125 | +<%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| 126 | +<html> |
| 127 | + <head> |
| 128 | + <title>$Title$</title> |
| 129 | + </head> |
| 130 | + <body> |
| 131 | + Hello Spring World |
| 132 | + </body> |
| 133 | +</html> |
| 134 | +``` |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +## Controller 디렉토리 만들기 & 간단한 Controller code |
| 139 | + |
| 140 | +- New - Directory |
| 141 | + |
| 142 | +![[assets/posts/20170204/7.PNG]] |
| 143 | + |
| 144 | +```java |
| 145 | +package Controller; |
| 146 | + |
| 147 | +import org.springframework.stereotype.Controller; |
| 148 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 149 | + |
| 150 | +/** |
| 151 | + * Created by NESOY on 2017-02-04. |
| 152 | + */ |
| 153 | +@Controller |
| 154 | +public class controller { |
| 155 | + |
| 156 | + @RequestMapping(value = "/") |
| 157 | + public String test(){ |
| 158 | + return "index"; |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +## Run Edit Configuration으로 Tomcat Server 추가하기 |
| 164 | + |
| 165 | +- Run - Edit Configuration |
| 166 | + |
| 167 | +![[assets/posts/20170204/8.PNG]] |
| 168 | + |
| 169 | +- Tomcat Server 추가하기 |
| 170 | + |
| 171 | +![[assets/posts/20170204/9.PNG]] |
| 172 | + |
| 173 | +- Warning 나오면 Fix 버튼누르기 |
| 174 | + |
| 175 | +![[assets/posts/20170204/10.PNG]] |
| 176 | + |
| 177 | +- war exploded로 추가하여 설정한 모습 |
| 178 | + |
| 179 | +![[assets/posts/20170204/11.PNG]] |
| 180 | + |
| 181 | +## Error 1 : web.xml의 ApplicationContext.xml 빨간 글씨가 발생하여 tomcat 실행 X |
| 182 | +### 원인 |
| 183 | +- tomcat의 context root 기본 파일 이름명이 `webapps` 때문에 에러가 발생하였다. |
| 184 | +### 해결방법 |
| 185 | +- 경로 재설정 / 파일 이름 바꾸기 두 가지 해결방법이 있다. |
| 186 | +- 아래는 경로 재설정을 설명한 것이다. |
| 187 | +- 만약 이 에러가 보인다면 아래와 같이 따라하자. |
| 188 | + |
| 189 | +![[assets/posts/20170204/12.PNG]] |
| 190 | + |
| 191 | +### WEB-INF 경로 잡아주기 |
| 192 | + |
| 193 | +- Open Module Settings에 들어간다. |
| 194 | + |
| 195 | +![[assets/posts/20170204/13.PNG]] |
| 196 | + |
| 197 | +- Modules - web - Web Resource Directory가 빨간 글씨가 된 걸 확인할 수 있다. |
| 198 | + |
| 199 | +![[assets/posts/20170204/14.PNG]] |
| 200 | + |
| 201 | +- 경로 다시 잡아 주기 |
| 202 | + |
| 203 | +![[assets/posts/20170204/15.PNG]] |
| 204 | + |
| 205 | +- 해결 된 모습 |
| 206 | + |
| 207 | +![[assets/posts/20170204/16.PNG]] |
| 208 | + |
| 209 | +## Error 2 : Tomcat 오류 - RMI TCP Connection |
| 210 | + |
| 211 | +- 만약 이 에러가 보인다면 아래와 같이 따라하자. |
| 212 | + |
| 213 | +![[assets/posts/20170204/17.PNG]] |
| 214 | + |
| 215 | +### 해결 방법 : Library 집어넣기. |
| 216 | + |
| 217 | +- Project Structure - Artifacts에 들어간다. |
| 218 | + |
| 219 | +![[assets/posts/20170204/18.PNG]] |
| 220 | + |
| 221 | +- **Available Elements 아래에 있는 Library를 더블 클릭한다.** |
| 222 | + |
| 223 | +- 옮겨진 모습 |
| 224 | + |
| 225 | +![[assets/posts/20170204/19.PNG]] |
| 226 | + |
| 227 | +## 모든 에러를 통과한 후 실행하기 |
| 228 | + |
| 229 | +> 깔끔하게 성공된 모습을 볼 수 있다. |
| 230 | +
|
| 231 | +![[assets/posts/20170204/20.PNG]] |
| 232 | + |
0 commit comments