프로젝트 참고 정보
- 빌드관리도구 : Maven
- 스프링 버전 : 4.1.6
- IDE : Eclipse
의존성 파일작성하기
src > main > resources 폴더에 xml 파일을 생성합니다.
- spring-mvc.xml
- spring-controller.xml
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<mvc:view-resolvers>
<mvc:jsp prefix="/WEB-INF/view/" />
</mvc:view-resolvers>
</beans>
spring-controller.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"
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">
<bean class="manage.TestController"></bean>
</beans>
web.xml 파일 작성하기
src > main > webapp > WEB-INF 폴더에 web.xml 파일을 작성합니다
참고로 IDE 에서 작성하는 경우 자동완성 기능이 있으므로 적극 활용합니다. 예를 들어 dispatcherServlet 에서 ctrl + space 입력하면 자동으로 활성화됩니다. 누구처럼 시간 많이 쏟지 않으시길..
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-mvc.xml
classpath:spring-controller.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
이제 자바코드를 작성해보겠습니다.
src > main > java 폴더에 manage 패키지 생성하고 TestController.java 작성합니다
TestController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/test")
public String test() {
return "test";
}
}
뷰 페이지를 작성하겠습니다
src > main > webapp > WEB-INF > viewe(폴더생성) 에 test.jsp 파일을 작성합니다
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>
서버 가동하고 URL 을 넣어 접속해봅니다.
http://localhost:8080/student_management/test
'웹 개발 > Spring' 카테고리의 다른 글
[Spring] Eclipse 에서 Maven 프로젝트 생성하기(+pom.xml) (0) | 2021.11.19 |
---|---|
[Spring] @RequestMapping 애노테이션으로 뷰페이지 매핑하기 (0) | 2021.11.12 |
[Spring] maven - pom.xml 파일 dependency 주입코드 작성 (0) | 2021.11.11 |
[Spring] web.xml (sample) (0) | 2021.11.11 |
[Spring] pom.xml (sample) (0) | 2021.11.11 |